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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <canvas id="c"></canvas>
<footer>
  <p>Refresh for a new design!</p>
  <p>Play with the settings in the <a href="https://codepen.io/pimdewit/pen/OjdWog" target="_blank">JS editor</a></p>
</footer>
              
            
!

CSS

              
                body {margin: 0; overflow: hidden; color: white;}

footer {
  position: absolute;
  bottom: 32px;
  left: 32px;
  
  margin: 0;
  
  font-family: monospace;
  
  background-color: #1a1a1a;
}

p { margin: 0; }
a { color: navajowhite; }
              
            
!

JS

              
                const subject_cfg = {
  iterations: 40,
  start_iteration: 35,
  morphtime: 200,
  detail: Math.floor(Math.random() * 2) // 0 - 4 Change me! I recommend not going higher than 4 (perf reasons)
};

const scene_cfg = {
  floorY: -40 + subject_cfg.iterations,
  lightX: 10,
  lightY: 100,
  lightZ: 200,

  fogFar: 300,
  fogNear: 100,
  fogColor: '#1a1a1a',

  groundColor: '#111'
};

const camera_cfg = {
  x: -subject_cfg.iterations,
  y: subject_cfg.iterations,
  z: 120,
  fov: 65,
  near: 1,
  far: 1600
};

const render_cfg = {
  antialias: true,
  shadowMap: false,
  shadowSoft: false,
  clearColor: '#1a1a1a'
};


class Experiment {
  constructor(canvas, width, height) {
    this.canvas = canvas;
    
    this.width = width || window.innerWidth;
    
    this.height = height || window.innerHeight;
    
    this.scene = null;
    
    this.camera = null;
    
    this.renderer = null;
    
    this.group = null;
    
    this.time = Math.random() * 1000000;
    
    this.mode = {};
  }

  init() {
    this._createCamera();
    this._startScene();
    this._createEnvironment();
    this._createSubject();
    this._createRenderer();
    this.render();
    
    window.addEventListener('resize', this._onResize.bind(this));
  }

  _createEnvironment() {
    const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 1);
    hemiLight.position.set(0, 200, 0);
    this.scene.add(hemiLight);

    const dirLight = new THREE.DirectionalLight(0xffffff, 1);
    dirLight.position.set(scene_cfg.lightX, scene_cfg.lightY, scene_cfg.lightZ);
    dirLight.position.multiplyScalar(50);
    this.scene.add(dirLight);

    dirLight.castShadow = render_cfg.shadowSoft;
		dirLight.shadow.mapSize.width = 2048;
		dirLight.shadow.mapSize.height = 2048;

    const d = 50;
    dirLight.shadow.camera.left = -d;
    dirLight.shadow.camera.right = d;
    dirLight.shadow.camera.top = d;
    dirLight.shadow.camera.bottom = -d;
    dirLight.shadow.camera.far = 3500;
    dirLight.shadow.bias = -0.0001;

    // GROUND
    const groundGeo = new THREE.PlaneBufferGeometry(10000, 10000);
    const groundMat = new THREE.MeshPhongMaterial({ color: scene_cfg.groundColor, specular: 0x505050 });
    const ground = new THREE.Mesh(groundGeo, groundMat);
    ground.rotation.x = -Math.PI / 2;
    ground.position.y = scene_cfg.floorY;
    ground.receiveShadow = render_cfg.shadowMap;
    this.scene.add(ground);
  }

  _createSubject() {
    this.mode.clipIntersection = true;

    const clipPlanes = [
      new THREE.Plane(new THREE.Vector3(1,  0,  0), 0),
      new THREE.Plane(new THREE.Vector3(0, -1,  0), 0),
      new THREE.Plane(new THREE.Vector3(0,  0, -1), 0)
    ];

    this.group = new THREE.Object3D();
    let mesh = {};

    for (let i = subject_cfg.start_iteration; i < subject_cfg.iterations; i++)  {
      const random = Math.random();
      const a = ((subject_cfg.iterations - subject_cfg.start_iteration) / i) * 10;

      const color = new THREE.Color(Math.random(), Math.random(), Math.random());
      
      const geometry = new THREE.TetrahedronBufferGeometry(i, subject_cfg.detail);

      const material = new THREE.MeshStandardMaterial({
        color: color,
        roughness: 0,
        metalness: 0,
        shading:THREE.FlatShading,
        side: THREE.DoubleSide,
        clippingPlanes: clipPlanes,
        clipIntersection: true
      });

      mesh = new THREE.Mesh(geometry, material);
      this.group.add(mesh);
    }

    mesh.castShadow = render_cfg.shadowMap;
		mesh.receiveShadow = render_cfg.shadowMap;
    this.group.position.x = -subject_cfg.iterations;
    this.group.position.y = subject_cfg.iterations;

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

  _createCamera() {
    this.camera = new THREE.PerspectiveCamera(
        camera_cfg.fov,
        this.width / this.height,
        camera_cfg.near,
        camera_cfg.far);

    this.camera.position.set(camera_cfg.x, camera_cfg.y, camera_cfg.z);
    this.camera.lookAt(new THREE.Vector3(camera_cfg.x, camera_cfg.y, 0));
  }

  _startScene() {
    this.scene = new THREE.Scene();
  }

  _createRenderer() {
    this.renderer = new THREE.WebGLRenderer({canvas: this.canvas, antialias: render_cfg.antialias});
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(this.width, this.height);
    this.renderer.setClearColor(render_cfg.clearColor);
    this.renderer.shadowMap.enabled = render_cfg.shadowMap;
    this.renderer.shadowMap.soft = render_cfg.shadowSoft;
    this.renderer.localClippingEnabled = true;
  }

  render() {
    requestAnimationFrame(this.render.bind(this));
    this.time++;
    this.animate();
    this.renderer.render(this.scene, this.camera);
  }

  animate() {
    const openingSpeed = (Math.sin((this.time / subject_cfg.morphtime)) *
        (subject_cfg.iterations / 2) + 1);

    this.camera.position.z = camera_cfg.z - (Math.sin((this.time / 20)) * 2);
    

    this.mode.clipPosition = openingSpeed;
    const children = this.group.children;

    if (children) {
      for (let i = 0; i < children.length - 1; i++) {
        
        const current = children[i].material;
        children[i].rotation.z = this.time * (0.001 * i);
        
        
        for (let j = 0; j < current.clippingPlanes.length; j++) {
          const plane = current.clippingPlanes[j];
          plane.constant = (subject_cfg.iterations * plane.constant + this.mode.clipPosition) / (subject_cfg.iterations + 1);
        }
        
      }
    }
  }
  
  _onResize() {
    this.camera.aspect = window.innerWidth / window.innerHeight;
    this.camera.updateProjectionMatrix();

    this.renderer.setSize( window.innerWidth, window.innerHeight );
  }
}

window.onload = () => {
  const c = document.getElementById('c');

  const b = new Experiment(c);
  b.init();
};
              
            
!
999px

Console