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

            
              <footer class="footer v-dark">
 Made with ☕ by <a class="footer-anchor" target="_blank" href="https://twitter.com/zadvorsky">Szenia</a>
</footer>

<div id="three-container"></div>

            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Roboto');

$black: #343436;
$white: #fff;
$yellow: #fcd000;

*, *:before, *:after {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}

html, body {
 width: 100%;
 height: 100%;
 overflow: hidden;
}

.footer {
 position: fixed;
 right: 0;
 bottom: 0;
 left: 0;
 padding: 10px 10px;
 text-align: right;
 font-family: 'Roboto', sans-serif;
 font-size: 12px;
 
 &.v-dark {
  background-color: $black;
  color: $white;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
 }
 &.v-light {
  background-color: $white;
  color: $black;
 }
}

.footer-anchor {
 display: inline-block;
 margin-left: 5px;
 padding: 2px 4px;
 color: $black;
 text-decoration: none;
 background-color: $yellow;
 border-radius: 4px;
 opacity: 1;
 transition: opacity 0.2s;
 
 &:hover {
  opacity: 0.6;
 }
}
            
          
!
            
              function Animation(params) {
  const config = {
    hairLength: 2.0,
    hairBase: 0.1,
    maxDelay: 0.5,
    maxDuration: 1.0,
    minBounceAmp: 1.0,
    maxBounceAmp: 2.0,
    minBouncePer: 0.2,
    maxBouncePer: 0.4,
    fuzz: 1.5,
    minHairDrop: 2.0,
    maxHairDrop: 4.0,
  };
  
  const plusPositions = params.positions;
  // const prefab = new THREE.CylinderGeometry(0.0, config.hairBase, config.hairLength, 4, 16, false);
  const prefab = new THREE.ConeGeometry(config.hairBase, config.hairLength, 4, 16, true);
  
  prefab.translate(0, config.hairLength * 0.5, 0);
  
  const geometry = new BAS.PrefabBufferGeometry(prefab, plusPositions.length);
  
  // const max = new THREE.Vector3().setScalar(4).length();
  this.totalDuration = config.maxDuration + config.maxDelay;
  geometry.createAttribute('anim', 2, (data, i, count) => {
    const position = plusPositions[i];
    data[0] = THREE.Math.mapLinear(position.y, 4, -4, 0, config.maxDelay) * THREE.Math.randFloat(0.9, 1.0);
    // data[0] = THREE.Math.mapLinear(position.length(), 0, max, 0, maxDelay);
    data[1] = config.maxDuration;
  });
  
  // default easeBounceOut factor == 1.70158
  // default easeElasticOut == 1.0, 0.3
  geometry.createAttribute('bounce', 2, (data) => {
    // data[0] = THREE.Math.randFloat(1.7, 8.0);
    data[0] = THREE.Math.randFloat(config.minBounceAmp, config.maxBounceAmp);
    data[1] = THREE.Math.randFloat(config.minBouncePer, config.minBouncePer);
  });
  
  geometry.createAttribute('startPosition', 3, (data, i) => {
    plusPositions[i].toArray(data);
  });

  const up = new THREE.Vector3(0, 1, 0);
  const normal = new THREE.Vector3(); // not actually the normal, but based on position
  const quat = new THREE.Quaternion();
  geometry.createAttribute('rMin', 4, (data, i) => {
    normal.copy(plusPositions[i]);
    normal.x += THREE.Math.randFloatSpread(config.fuzz);
    normal.y += THREE.Math.randFloatSpread(config.fuzz);
    normal.z += THREE.Math.randFloatSpread(config.fuzz);
    // normal.x *= THREE.Math.randFloat(1.0, fuzz);
    // normal.z *= THREE.Math.randFloat(1.0, fuzz);

    normal.normalize();

    quat.setFromUnitVectors(up, normal);
    quat.toArray(data);
  });
  geometry.createAttribute('rMax', 4, (data, i) => {
    normal.copy(plusPositions[i]);
    normal.x += THREE.Math.randFloatSpread(config.fuzz);
    normal.y -= THREE.Math.randFloat(config.minHairDrop, config.maxHairDrop);
    normal.z += THREE.Math.randFloatSpread(config.fuzz);
    normal.normalize();
    
    quat.setFromUnitVectors(up, normal);
    quat.toArray(data);
  });
  
  const material = new BAS.StandardAnimationMaterial({
    flatShading: true,
    uniformValues: {
      metalness: 0.5,
      roughness: 1.0
    },
    uniforms: {
      time: {value: 0},
      hairLength: {value: config.hairLength}
    },
    vertexParameters: `
      uniform float time;
      uniform float hairLength;
      
      attribute vec2 anim;
      attribute vec2 bounce;

      attribute vec3 startPosition;
      
      attribute vec4 rMin;
      attribute vec4 rMax;
    `,
    varyingParameters: `
      varying float vProgress;
      varying float vY;
    `,
    vertexFunctions: [
      BAS.ShaderChunk.ease_back_out,
      BAS.ShaderChunk.ease_elastic_out,
      BAS.ShaderChunk.ease_expo_out,
      BAS.ShaderChunk.quaternion_rotation,
    ],
    vertexPosition: `
      float progress = clamp(time - anim.x, 0.0, anim.y) / anim.y;
      
      float bounceProgress = easeElasticOut(progress, bounce.x, bounce.y);
      // float bounceProgress = easeBackOut(progress, bounce.x * 2.0);
      float growProgress = easeExpoOut(progress);
      float frc = position.y / hairLength;
      
      vY = frc;
      vProgress = growProgress;
      
      transformed.xz *= growProgress;
      
      // doesn't work with fuzz + overshoot ease, but mey give better results
      // vec4 r = mix(rMin, rMax, frc * bounceProgress);
      // transformed = rotateVector(r, transformed);

      // other method
      vec3 pMin = rotateVector(rMin, transformed);
      vec3 pMax = rotateVector(rMax, transformed);
      transformed = mix(pMin, pMax, frc * bounceProgress);

      transformed += startPosition;
    `,
    fragmentInit: `
      if (vProgress == 0.0 || vY > vProgress) discard;
    `
  });

  THREE.Mesh.call(this, geometry, material);
  // this.customDepthMaterial = BAS.Utils.createDepthAnimationMaterial(material);
}

Animation.prototype = Object.create(THREE.Mesh.prototype);
Animation.prototype.constructor = Animation;

Animation.prototype.animate = function() {
  return TweenMax.to(this, 2.0, {time: this.totalDuration, ease: Power0.easeOut, repeat: -1, repeatDelay: 1.0});
};
Object.defineProperty(Animation.prototype, 'time', {
  get() {
    return this.material.uniforms.time.value;    
  },
  set(v) {
    this.material.uniforms.time.value = v;
    // this.customDepthMaterial.uniforms.time.value = v;
  }
});

Animation.prototype.setColor = function(v) {
  this.material.uniforms.diffuse.value.set(v);
};

const colors = {
  turquoise: 0x47debd,
  darkPurple: 0x2e044e,
  purple: 0x7821ec,
  yellow: 0xfff95d,
  white: 0xffffff,
  black: 0x000000
};

const root = new THREERoot({
  createCameraControls: true,
  zNear: 0.01,
  zFar: 1000
});

root.renderer.shadowMap.enabled = true;
root.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
root.renderer.setClearColor(colors.darkPurple);
root.camera.position.set(-1, 2, 12);
root.controls.autoRotate = true;

const light = new THREE.DirectionalLight(colors.turquoise);
light.position.set(0.125, 1, 0);
root.add(light);

const light2 = new THREE.DirectionalLight(colors.yellow);
light2.position.set(-0.125, -1, 0);
root.add(light2);

root.add(new THREE.AmbientLight(colors.purple));

new THREE.JSONLoader().load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/304639/plus.json', (model) => {
  // test shapes, try different ones :D
  // model = new THREE.SphereGeometry(4, 256, 64);
  // model = new THREE.PlaneGeometry(8, 8, 100, 100);
  // model = new THREE.TorusGeometry(3, 1, 128, 128);
  
  const base = new THREE.Mesh(
    model,
    new THREE.MeshStandardMaterial({
      roughness: 1.0
    })
  );
  root.add(base);
  
  const animation = new Animation({
    positions: model.vertices,
    normals: model.normals
  });
  // animation.castShadow = true;
  // animation.receiveShadow = true;
  root.add(animation);
  
  animation.animate();
});

            
          
!
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