Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="wrapper js-wrapper">
  <canvas class="js-canvas"></canvas>
</div>


<script id="particle-vertex" type="v-script">
  attribute vec3 position;
  attribute vec2 uv;
  uniform mat4 projectionMatrix;
  uniform mat4 modelViewMatrix;
  varying vec4 vViewPosition;
  void main() {
    vec4 p = projectionMatrix * modelViewMatrix * vec4(position, 1.);
    vViewPosition = modelViewMatrix * vec4(position, 1.);
    gl_Position = p;
  }
</script>

<script id="particle-fragment" type="f-script">
  precision highp float;
  
  #include <packing>
  
  varying vec4 vViewPosition;
  uniform sampler2D uDepthTexture;
  uniform float uCameraNear;
  uniform float uCameraFar;
  uniform float uDepthFade;
  uniform vec2 uResolution;
  
  float readDepth(sampler2D depthSampler, vec2 coord) {
    float fragCoordZ = texture2D(depthSampler, coord).x;
    float viewZ = perspectiveDepthToViewZ(fragCoordZ, uCameraNear, uCameraFar);
    return viewZToOrthographicDepth(viewZ, uCameraNear, uCameraFar);
  } 
  
  void main() {
    vec2 screenCoord = gl_FragCoord.xy / uResolution.xy;
    float sceneDepth = readDepth(uDepthTexture, screenCoord);
    
    float viewZ = vViewPosition.z;
    float currenuDepthTexture = viewZToOrthographicDepth(viewZ, uCameraNear, uCameraFar);
  
    float eps = .001;
    float fade = clamp(abs(currenuDepthTexture - sceneDepth) / max(uDepthFade, eps), 0., 1.);
 
    gl_FragColor = vec4(vec3(1.), fade);
  }
</script>
              
            
!

CSS

              
                .wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.wrapper canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}
              
            
!

JS

              
                
let width, height;

const params = {
  depthFade: 0.05,
};

const pane = new Tweakpane();
pane.addInput(params, "depthFade", {
  min: 0.001,
  max: 0.3,
  step: 0.001
});

const wrapper = document.querySelector(".js-wrapper");
const canvas = document.querySelector(".js-canvas");

const renderer = new THREE.WebGLRenderer({ canvas });
const ratio = Math.min(window.devicePixelRatio, 1.5);

renderer.setPixelRatio(ratio);

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10);
camera.position.set(-2, 1.5, 3);
camera.lookAt(new THREE.Vector3(0, 0, 0));

const renderTarget = new THREE.WebGLRenderTarget(1, 1);
renderTarget.texture.format = THREE.RGBAFormat;
renderTarget.texture.minFilter = THREE.NearestFilter;
renderTarget.texture.magFilter = THREE.NearestFilter;
renderTarget.texture.generateMipmaps = false;
renderTarget.stencilBuffer = false;
renderTarget.depthBuffer = true;
renderTarget.depthTexture = new THREE.DepthTexture();
renderTarget.depthTexture.type = THREE.UnsignedShortType;
renderTarget.depthTexture.format = THREE.DepthFormat;

const controls = new THREE.OrbitControls(camera, renderer.domElement);

const cube = new THREE.Mesh(
  new THREE.BoxGeometry(1, 1, 1),
  new THREE.MeshBasicMaterial({
    color: 0xff0000
  })
);

scene.add(cube);

const particleVertexShader = document.querySelector("#particle-vertex").textContent;
const particleFragmentShader = document.querySelector("#particle-fragment").textContent;

const softParticleMesh = new THREE.Mesh(
  new THREE.BoxGeometry(1, 1, 1),
  new THREE.RawShaderMaterial({
    vertexShader: particleVertexShader,
    fragmentShader: particleFragmentShader,
    uniforms: {
      uDepthTexture: {
        value: null
      },
      uCameraNear: {
        value: 0
      },
      uCameraFar: {
        value: 0
      },
      uDepthFade: {
        value: 0,
      },
      uResolution: {
        value: new THREE.Vector2(),        
      }
    },
    transparent: true
  })
);

softParticleMesh.position.copy(new THREE.Vector3(0.5, 0.5, 0.5));
scene.add(softParticleMesh);

const onWindowResize = () => {
  width = wrapper.offsetWidth;
  height = wrapper.offsetHeight;
  camera.aspect = width / height;
  camera.updateProjectionMatrix(); 
  renderer.setSize(width, height);
  renderTarget.setSize(width * ratio, height * ratio);
}

onWindowResize();
window.addEventListener("resize", () => onWindowResize());


const tick = () => {
  controls.update();
  
  const ctx = renderer.getContext();

  softParticleMesh.visible = false;
  
  renderer.setRenderTarget(renderTarget);
  
  ctx.colorMask(false, false, false, false);
  renderer.render(scene, camera);

  renderer.setRenderTarget(null);
  
  softParticleMesh.visible = true;

  softParticleMesh.material.uniforms.uDepthTexture.value = renderTarget.depthTexture;
  softParticleMesh.material.uniforms.uCameraNear.value = camera.near;
  softParticleMesh.material.uniforms.uCameraFar.value = camera.far;
  softParticleMesh.material.uniforms.uDepthFade.value = params.depthFade;
  softParticleMesh.material.uniforms.uResolution.value = new THREE.Vector2(
    width * ratio, height * ratio
  );

  ctx.colorMask(true, true, true, true);
  renderer.render(scene, camera);
  
  requestAnimationFrame(tick);
}

requestAnimationFrame(tick);

              
            
!
999px

Console