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

              
                <canvas id="c"></canvas>
              
            
!

CSS

              
                body {
  margin: 0;
}
#c {
  width: 100%;
  height: 100%;
  display: block;
}

              
            
!

JS

              
                import * as THREE  from '//unpkg.com/three@0.117.1/build/three.module.js'
import { EffectComposer } from '//unpkg.com/three@0.117.1/examples/jsm/postprocessing/EffectComposer.js'
import { RenderPass } from '//unpkg.com/three@0.117.1/examples/jsm/postprocessing/RenderPass.js'
import { Pass } from "//unpkg.com/three@0.117.1/examples/jsm/postprocessing/Pass.js";


class TestZPass extends Pass
{
  
  // TEST PASS
  // display previous pass (readBuffer.depthBuffer), with options.color 
  
  constructor(options)
  {
  
    super();

    this.uniforms = {     
      "cameraNear": { value: options.camera.near },
      "cameraFar": { value: options.camera.far },
      "color" : { value: options.color },
      "tDepth": { value: null }      
    };

    this.vertexShader = `    
      #include <common>
      varying vec2 vUv;

      void main()
      {          
        vUv = uv;           
        vec4 mvPosition =  modelViewMatrix * vec4( position, 1.0 );
        gl_Position = projectionMatrix * mvPosition;
      }`;

    this.fragmentShader = `       
  
      #include <common>
      #include <packing>
   
      uniform sampler2D tDepth;
     
      varying vec2 vUv;

      uniform vec3 color;   // color used to display tDepth

      uniform float cameraNear;
      uniform float cameraFar;

      void main() 
      {
        float depth = texture2D( tDepth, vUv ).x; 
        float viewZ = perspectiveDepthToViewZ( depth, cameraNear, cameraFar );                
        viewZ = viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
        vec3 v =  color - vec3( viewZ );  
        
        gl_FragColor =  vec4( v.x, v.y, v.z, 1.0);
    
      }`;

    this.material = new THREE.ShaderMaterial( {
        uniforms: this.uniforms,
        vertexShader: this.vertexShader,
        fragmentShader: this.fragmentShader,
        depthTest: true, 
        depthWrite: true,
        transparent: false
    } );
    
    this.fsQuad = new Pass.FullScreenQuad( this.material );
  
  }

  render( renderer, writeBuffer, readBuffer  )
  {
    this.uniforms[ "tDepth" ].value = readBuffer.depthTexture;  
    
		if ( this.renderToScreen ) {

			renderer.setRenderTarget( null );
			this.fsQuad.render( renderer );

		} else {

			renderer.setRenderTarget( writeBuffer );
			if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
			this.fsQuad.render( renderer );

    }
  }
}


function main() {
  const canvas = document.querySelector('#c');
  var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );  // init WebGL 2.0 context
  const renderer = new THREE.WebGLRenderer({canvas, context});
  renderer.setPixelRatio( window.devicePixelRatio );
  renderer.setSize(canvas.offsetWidth, canvas.offsetHeight , false);
  renderer.autoClear = false; 
  
  // Scene content from : https://threejsfundamentals.org/threejs/threejs-picking-raycaster.html
  
  const fov = 60;
  const aspect = 2;  // the canvas default
  const near = 0.1;
  const far = 200;
  const camera = new THREE.PerspectiveCamera(fov, canvas.offsetWidth / canvas.offsetHeight, near, far);
  camera.position.z = 30;

  const scene = new THREE.Scene();
  scene.background = new THREE.Color('white');

  // put the camera on a pole (parent it to an object)
  // so we can spin the pole to move the camera around the scene
  const cameraPole = new THREE.Object3D();
  scene.add(cameraPole);
  cameraPole.add(camera);
  {
    const color = 0xFFFFFF;
    const intensity = 1;
    const light = new THREE.DirectionalLight(color, intensity);
    light.position.set(-1, 2, 4);
    camera.add(light);
  }

  const boxWidth = 1;
  const boxHeight = 1;
  const boxDepth = 1;
  const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);

  function rand(min, max) {
    if (max === undefined) {
      max = min;
      min = 0;
    }
    return min + (max - min) * Math.random();
  }

  function randomColor() {
    return `hsl(${rand(360) | 0}, ${rand(50, 100) | 0}%, 50%)`;
  }

  const numObjects = 100;
  for (let i = 0; i < numObjects; ++i) {
    const material = new THREE.MeshPhongMaterial({
      color: randomColor(),
    });

    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);

    cube.position.set(rand(-20, 20), rand(-20, 20), rand(-20, 20));
    cube.rotation.set(rand(Math.PI), rand(Math.PI), 0);
    cube.scale.set(rand(3, 6), rand(3, 6), rand(3, 6));
  }
  
  // Init Composer and RenderTarget
     
  //const renderTarget = new THREE.WebGLMultisampleRenderTarget( canvas.clientWidth, canvas.clientHeight, {   // BAM ! WebGLMultisampleRenderTarget dont work .... why ? 
  const renderTarget = new THREE.WebGLRenderTarget( canvas.clientWidth, canvas.clientHeight, {
    format: THREE.RGBFormat,
    stencilBuffer: false,
    depthBuffer: true,
    depthTexture : new THREE.DepthTexture() 
  } );

  var clock = new THREE.Clock();
  clock.start()
  
  const composer = new EffectComposer(renderer, renderTarget );

  const renderPass = new RenderPass(scene, camera); 
  composer.addPass(renderPass); 
  
  // add first pass, with depthbuffer from renderPass, color to red
  const testZPass1 = new TestZPass( { camera, color: new THREE.Vector3(1,0,0) } );
  composer.addPass(testZPass1); 
  
  // add second pass, with depthbuffer from testZPass1, and color to green
  // ok with WebGLRenderTarget
  // !!! depthBuffer disappeared with WebGLMultisampleRenderTarget
  const testZPass2 = new TestZPass( { camera, color: new THREE.Vector3(0,1,0)  } );
  composer.addPass(testZPass2);   

  
  // resize fn
  
  function resizeRendererToDisplaySize(renderer) {
    const canvas = renderer.domElement;
    const width = canvas.clientWidth;
    const height = canvas.clientHeight;
    const needResize = canvas.width !== width || canvas.height !== height;
    if (needResize) {
      camera.aspect = canvas .offsetWidth / canvas.offsetHeight;
      camera.updateProjectionMatrix();
      composer.setSize(width, height, false);
    }
    return needResize;
  }

  // render fn

  function render(time) {
    time *= 0.001;  // convert to seconds;

    if (resizeRendererToDisplaySize(renderer)) {
      const canvas = renderer.domElement;
      camera.aspect = canvas.clientWidth / canvas.clientHeight;
      camera.updateProjectionMatrix();
    }

    cameraPole.rotation.y = time * .1;

    var delta = clock.getDelta();
    composer.render( delta );

    requestAnimationFrame(render);
  }
  requestAnimationFrame(render);



}

main();

              
            
!
999px

Console