css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              body {
  margin: 0;
  overflow: hidden;
}
            
          
!
            
              const renderer = new THREE.WebGLRenderer({
  antialias: false,
  alpha: false,
  canvas: document.querySelector('canvas')
});

let then = Date.now();
let time = 0;

const width = 512;
const height = 512;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
renderer.sortObjects = false;
renderer.setClearColor('#d66751', 1);

const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-width / 2, width / 2, -height / 2, height / 2, -10, 10);
camera.position.set(width / 2, height / 2, 0);

// How many total sprites
const count = 50000;

// We create N data textures large enough to fill our particle count
// This way we don't need to send 200k floats to the GPU each frame to animate it
// This is memory intensive! Ideally shouldn't be so many...
const frames = 50;

// needs to be large enough to fit all our data points
const dimension = Math.ceil(Math.sqrt(count));

// 1 cycle of animation, in seconds
const animationDuration = 5;

// circle position on screen
// vec3(x, y)
const offsetAttrib = new THREE.InstancedBufferAttribute(new Float32Array(count * 2), 2);

// UV coordinate into data texture
// vec2(u, v)
const dataUVAttrib = new THREE.InstancedBufferAttribute(new Float32Array(count * 2), 2);

const geometry = createGeometry();
const material = createMaterial();
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
scene.add(mesh);

const dataTextures = createDataTextures();

function createMaterial () {
   const vertex = `
    attribute vec3 offset;
    attribute vec2 dataUV;
    uniform float blend;
    uniform highp sampler2D textureA;
    uniform highp sampler2D textureB;
    varying vec3 color;

    void main () {
      vec4 particleDataA = texture2D(textureA, dataUV);
      vec4 particleDataB = texture2D(textureB, dataUV);
      vec4 particleData = mix(particleDataA, particleDataB, blend);
      float radius = particleData.a;
      vec3 p = position.xyz * radius + offset.xyz;
      color = particleData.rgb;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
    }
  `;
  const frag = `
    varying vec3 color;
    void main () {
      gl_FragColor = vec4(color, 1.0);
    }
  `
  return new THREE.ShaderMaterial({
    uniforms: {
      blend: { type: 'f', value: 0 },
      textureA: { type: 't', value: new THREE.Texture() },
      textureB: { type: 't', value: new THREE.Texture() }
    },
    side: THREE.BackSide,
    blending: THREE.NoBlending,
    vertexShader: vertex,
    fragmentShader: frag
  });
}

function createGeometry () {
  // how many sides for each circle (more = less perf)
  const circleSides = 5;
  // base geometry that we want to instance
  const baseGeometry = new THREE.CircleBufferGeometry(1, circleSides);

  // our actual geometry for instanced rendering
  const geometry = new THREE.InstancedBufferGeometry();
  geometry.maxInstancedCount = count;

  // copy over position + index attributes form the base goemetry
  const attributes = ['position'];
  attributes.forEach(key => {
    geometry.addAttribute(key, baseGeometry.getAttribute(key));
  });
  geometry.setIndex(baseGeometry.getIndex());
 
  // and dispose the base one now that we've copied it over
  baseGeometry.dispose();

  // add our two attributes
  geometry.addAttribute('offset', offsetAttrib);
  geometry.addAttribute('dataUV', dataUVAttrib);

  // setup default position, color & radius for all circles
  setupAttributes();

  return geometry;
}

function createDataTextures () {
  const textures = [];
  // Here you would use your open data to populate this array
  // For this example we will use random data over time
  for (let i = 0; i < frames; i++) {
    const t = i / (frames - 1) * animationDuration; // time

    // 4x data to allow for (r, g, b, radius)
    const data = new Float32Array(dimension * dimension * 4);
    for (let j = 0; j < count; j++) {
      data[j * 4 + 0] = (Math.sin(t * 1.0 + j * count) * 0.5 + 0.5) * 0.75;
      data[j * 4 + 1] = 0.2;
      data[j * 4 + 2] = 0.35 * (Math.sin(t * 1.5) * 0.5 + 0.5);
      data[j * 4 + 3] = (Math.sin(t * 2.0 + j * count) * 0.5 + 0.5) * 10;
    }
    const texture = new THREE.DataTexture(data, dimension, dimension, THREE.RGBAFormat, THREE.FloatType);
    texture.minFilter = THREE.NearestFilter;
    texture.magFilter = THREE.NearestFilter;
    texture.needsUpdate = true;
    textures.push(texture);
  }

  return textures;
}

function setupAttributes () {
  for (let i = 0; i < count; i++) {
    // set initial XY position
    offsetAttrib.setXY(i, Math.random() * width, Math.random() * height);

    // the (x, y) coordinate for this particle in the data texture
    const x = Math.floor(i % dimension);
    const y = Math.floor(i / dimension);
    dataUVAttrib.setXY(i, x / dimension, y / dimension);
  }
}

function render () {
  const now = Date.now();
  const delta = now - then;
  time += delta / 1000;
  if (time > animationDuration) time = 0;
  then = now;

  const t = time / animationDuration;
  const currentFrame = frames * t;
  const frame = Math.floor(currentFrame);
  const fract = currentFrame - frame;
  let nextFrame = frame + 1;
  if (nextFrame > frames - 1) nextFrame = 0;
  material.uniforms.blend.value = fract;
  material.uniforms.textureA.value = dataTextures[frame];
  material.uniforms.textureB.value = dataTextures[nextFrame];
  
  requestAnimationFrame(render);
  renderer.render(scene, camera);
}

requestAnimationFrame(render);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console