cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              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
Loading ..................

Console