Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="content-canvas"></div>

              
            
!

CSS

              
                body{
  margin: 0;
}

canvas{
  width: 100%;
  height: 100vh;
  display: block;
}
              
            
!

JS

              
                // Carga modelos 3D externos o texturas dependiendo de la opcion
const textureLoader = opt =>
  opt === "obj" ? new THREE.OBJLoader() : new THREE.TextureLoader();

class Particles {
  constructor(maxCount, limit, radius, group) {
    this.maxCount = maxCount;
    this.segments = maxCount * maxCount;
    this.particlesCount = maxCount / 2;

    this.lineVertices = new Float32Array(this.segments * 3);
    this.colors = new Float32Array(this.segments * 3);
    this.particlesVertices = new Float32Array(maxCount * 3);

    this.limit = limit;
    this.radius = radius;

    this.group = group;
    this.loader = textureLoader();

    this.velocities = [];

    this.setParticles();
  }

  setParticles() {
    this._partiGeo = new THREE.BufferGeometry();
    this._lineGeo = new THREE.BufferGeometry();

    for (let i = 0; i < this.maxCount; i++) {
      this.particlesVertices[i * 3] = this.getRandomPosition();
      this.particlesVertices[i * 3 + 1] = this.getRandomPosition();
      this.particlesVertices[i * 3 + 2] = this.getRandomPosition();

      const velocity = {
        x: (Math.random() - 0.5) * 0.5,
        y: (Math.random() - 0.5) * 0.5,
        z: (Math.random() - 0.5) * 0.5
      };

      this.velocities.push(velocity);
    }

    this._partiGeo.setDrawRange(0, this.particlesCount);

    this._partiGeo.addAttribute(
      "position",
      new THREE.BufferAttribute(this.particlesVertices, 3).setDynamic(true)
    );

    this._lineGeo.addAttribute(
      "position",
      new THREE.BufferAttribute(this.lineVertices, 3).setDynamic(true)
    );

    this._lineGeo.addAttribute(
      "color",
      new THREE.BufferAttribute(this.colors, 3).setDynamic(true)
    );

    this._lineGeo.computeBoundingSphere();
    this._lineGeo.setDrawRange(0, 0);

    const _pointMat = new THREE.PointsMaterial({
      map: this.loader.load("https://i.ibb.co/2n4PkJ1/dot.png"),
      size: 1,
      transparent: true,
      depthWrite: false
    });

    const _lineMat = new THREE.LineBasicMaterial({
      color: 0xffffff,
      vertexColors: THREE.VertexColors,
      blending: THREE.AdditiveBlending,
      linewidth: 1,
      transparent: true
    });

    this.meshPoints = new THREE.Points(this._partiGeo, _pointMat);
    this.meshLine = new THREE.LineSegments(this._lineGeo, _lineMat);

    this.group.add(this.meshPoints);
    this.group.add(this.meshLine);
  }

  getRandomPosition() {
    return (Math.random() * 2 - 1) * this.limit;
  }

  getLimit(x, y, z, i) {
    if (x > this.limit || x < -this.limit) {
      this.velocities[i].x = -this.velocities[i].x;
    }

    if (y > this.limit || y < -this.limit) {
      this.velocities[i].y = -this.velocities[i].y;
    }

    if (z > this.limit || z < -this.limit) {
      this.velocities[i].z = -this.velocities[i].z;
    }
  }

  getPosition(i) {
    return [i * 3, i * 3 + 1, i * 3 + 2];
  }

  compareVector(i1, i2) {
    return i1.every(x => i2.includes(x));
  }

  update() {
    let vertexPos = 0;
    let colorPos = 0;
    let numConnected = 0

    for (let i = 0; i < this.particlesCount; i++) {
      const velocity = this.velocities[i];

      const x = (this.particlesVertices[i * 3] += velocity.x);
      const y = (this.particlesVertices[i * 3 + 1] += velocity.y);
      const z = (this.particlesVertices[i * 3 + 2] += velocity.z);

      const a = new THREE.Vector3(x, y, z);
      const indexes1 = this.getPosition(i);

      this.getLimit(x, y, z, i);

      for (let j = 0; j < this.particlesCount; j++) {
        const indexes2 = this.getPosition(j);

        const isEqualTo = this.compareVector(indexes1, indexes2);

        if (isEqualTo) continue;

        const x2 = this.particlesVertices[j * 3];
        const y2 = this.particlesVertices[j * 3 + 1];
        const z2 = this.particlesVertices[j * 3 + 2];

        const d = a.distanceTo(new THREE.Vector3(x2, y2, z2));

        if (d < this.radius) {
          const alpha = 1 - d / this.radius;
          
          this.lineVertices[vertexPos++] = x;
          this.lineVertices[vertexPos++] = y
          this.lineVertices[vertexPos++] = z;

          this.lineVertices[vertexPos++] = x2;
          this.lineVertices[vertexPos++] = y2;
          this.lineVertices[vertexPos++] = z2;
          
          this.colors[colorPos++] = alpha
          this.colors[colorPos++] = alpha
          this.colors[colorPos++] = alpha
          
          this.colors[colorPos++] = alpha
          this.colors[colorPos++] = alpha
          this.colors[colorPos++] = alpha
          
          numConnected++
        }
      }
    }

    this.meshPoints.geometry.attributes.position.needsUpdate = true;
    this.meshLine.geometry.attributes.position.needsUpdate = true;
    this.meshLine.geometry.attributes.color.needsUpdate = true;
    
    this.meshLine.geometry.setDrawRange(0, numConnected * 2)
  }
}

class WebGL {
  constructor() {
    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    this.camera = new THREE.PerspectiveCamera(
      45,
      innerWidth / innerHeight,
      0.1,
      1000
    );
    this.scene = new THREE.Scene();
    this.group = new THREE.Group();
    this.clock = new THREE.Clock();

    this.loader = textureLoader();

    this.update = this.update.bind(this);
  }
  // Coloca el objeto renderer dentro del DOM
  // Instaciamos la clase OrbitControls para mover la camara
  // Agrega la camara y el objeto group dentro de la escena
  init() {
    const _contentCanvas = document.querySelector("#content-canvas");

    this.renderer.setPixelRatio(devicePixelRatio);
    this.renderer.shadowMap.enabled = true;
    this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;

    this.scene.add(this.camera);
    this.scene.add(this.group);

    this.controls = new THREE.OrbitControls(this.camera);

    this.camera.position.set(20, 20, 250);
    this.camera.lookAt(this.scene.position);

    _contentCanvas.appendChild(this.renderer.domElement);

    this.initFn();
  }

  // Inicia todos los metodos que serviran para crear nuestro espacio y objetos
  initFn() {
    //this.addLight();
    this.createMesh();
    this.update();
  }

  // Crea el objeto (geometria, material y malla) para luego agregarlo al escenario
  createMesh() {
    const MAX_COUNT = 600;
    const LIMIT = 50;
    const RADIUS = 20;

    this.particles = new Particles(MAX_COUNT, LIMIT, RADIUS, this.group);
  }

  // Agrega luz a la escena con la que estamos trabajando
  addLight() {
    const _directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    _directionalLight.position.set(10, 10, 10);
    _directionalLight.castShadow = true;
    this.helper.direction(_directionalLight, "directional");
    this.group.add(_directionalLight);
  }

  // Actualiza cualquier cambio, para luego representarlo en el canvas
  update() {
    this.render();
    //this.uniforms.uTime.value = this.clock.getElapsedTime();
    if (this.resizeRendererToDisplaySize(this.renderer)) {
      const canvas = this.renderer.domElement;
      this.camera.aspect = canvas.clientWidth / canvas.clientHeight;
      this.camera.updateProjectionMatrix();
    }
    
    const time = this.clock.getElapsedTime() * 0.2
    
    this.group.rotation.x = time
    this.group.rotation.y = time
    this.group.rotation.z = time
    
    this.particles.update();

    requestAnimationFrame(this.update);
  }

  // Rescala el canvas y escenario
  resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      renderer.setSize(width, height, false);
    }
    return needResize;
  }

  // Renderiza nuestro escenario
  render() {
    this.renderer.render(this.scene, this.camera);
  }
}

const webgl = new WebGL();
webgl.init();

              
            
!
999px

Console