Last active
June 19, 2018 20:48
-
-
Save neiltron/b5b924a60e36d86484cbdaef9bc8077f to your computer and use it in GitHub Desktop.
Frame differencing pass for THREE.EffectComposer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varying vec3 pos; | |
uniform sampler2D cameraBuffer; | |
uniform sampler2D lastCameraBuffer; | |
void main () { | |
vec2 texCoord = .5 + pos.xy * .5; | |
vec3 cameraColor = texture2D(cameraBuffer, texCoord).rgb; | |
vec3 lastCameraColor = texture2D(lastCameraBuffer, texCoord).rgb; | |
float diffColor = distance(cameraColor, lastCameraColor); | |
vec3 color = vec3(diffColor); | |
gl_FragColor = vec4(color, 1.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
varying vec3 pos; | |
void main () { | |
pos = position.xyz; | |
gl_Position = projectionMatrix * modelViewMatrix * vec4(position.xyz, 1.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pass, RenderPass } from 'postprocessing'; | |
const glslify = require('glslify'); | |
const path = require('path'); | |
export default class DifferencePass extends Pass { | |
constructor() { | |
super("DifferencePass"); | |
this.bufferTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter}); | |
this.renderPassLastBuffer = new RenderPass(this.scene, this.camera); | |
this.quad.material = new THREE.ShaderMaterial({ | |
vertexShader: glslify(path.resolve(__dirname, '../shaders/diff.vert')), | |
fragmentShader: glslify(path.resolve(__dirname, '../shaders/diff.frag')), | |
uniforms: { | |
time: { value: 0 }, | |
cameraBuffer: { value: null, type: 't' }, | |
lastCameraBuffer: { value: this.bufferTexture.texture, type: 't' } | |
} | |
}); | |
} | |
render(renderer, inputBuffer, outputBuffer, delta, stencilTest) { | |
this.material.uniforms.cameraBuffer.value = inputBuffer.texture; | |
renderer.render(this.scene, this.camera, this.renderToScreen ? null : outputBuffer); | |
this.renderPassLastBuffer.render(renderer, this.bufferTexture); | |
this.bufferTexture.needsUpdate = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment