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

              
                
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                const VERTEX_SHADER = `
    varying vec2 vUv;
    
    void main() {
        vUv = uv;
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
    }
`

const BUFFER_A_FRAG = `
    varying vec2 vUv;
    
    void main(){
        gl_FragColor = vec4(vUv,0.0,1.0);
    }
`

const BUFFER_FINAL_FRAG = `
    uniform sampler2D iChannel0;
    uniform sampler2D iChannel1;
    varying vec2 vUv;
    
    void main() {
        vec2 uv = vUv;
        vec2 a = texture2D(iChannel1,uv).xy;
        gl_FragColor = vec4(texture2D(iChannel0,a).rgb,1.0);
        // gl_FragColor = texture2D(iChannel0,vUv); // only iChannel0
        // gl_FragColor = texture2D(iChannel1,vUv); // only iChannel!
    }
`

class App {

    private width = 1024
    private height = 512

    private renderer = new THREE.WebGLRenderer()
    private loader = new THREE.TextureLoader()
    private mousePosition = new THREE.Vector4()
    private orthoCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1)
    private counter = 0

    constructor() {

        this.renderer.setSize(this.width, this.height)
        document.body.appendChild(this.renderer.domElement)

        this.renderer.domElement.addEventListener('mousedown', () => {
            this.mousePosition.setZ(1)
            this.counter = 0
        })

        this.renderer.domElement.addEventListener('mouseup', () => {
            this.mousePosition.setZ(0)
        })

        this.renderer.domElement.addEventListener('mousemove', event => {

            const x = (event.clientX / this.width) * 2 - 1
            const y = -(event.clientY / this.height) * 2 + 1

            this.mousePosition.setX(x)
            this.mousePosition.setY(y)

        })

    }

    private targetA = new BufferManager(this.renderer, { width: this.width, height: this.height })
    // private targetB = new BufferManager(this.renderer, { width: this.width, height: this.height })
    private targetC = new BufferManager(this.renderer, { width: this.width, height: this.height })

    private bufferA: BufferShader
    // private bufferB: BufferShader
    private bufferImage: BufferShader

    public start() {
      
        const resolution = new THREE.Vector3(this.width, this.height, window.devicePixelRatio)
        const channel0 = this.loader.load('https://res.cloudinary.com/di4jisedp/image/upload/v1523722553/wallpaper.jpg')
        this.loader.setCrossOrigin('')

        this.bufferA = new BufferShader(BUFFER_A_FRAG, {
            iFrame: { value: 0 },
            iResolution: { value: resolution },
            iMouse: { value: this.mousePosition },
            iChannel0: { value: null }
            // iChannel1: { value: null }
        })

        // this.bufferB = new BufferShader(require('./shaders/glsl/buffer-b.frag'), {
        //     iFrame: { value: 0 },
        //     iResolution: { value: resolution },
        //     iMouse: { value: this.mousePosition },
        //     iChannel0: { value: null }
        // })

        this.bufferImage = new BufferShader(BUFFER_FINAL_FRAG, {
            iResolution: { value: resolution },
            iMouse: { value: this.mousePosition },
            iChannel0: { value: channel0 },
            iChannel1: { value: null }
        })

        this.animate()

    }

    private animate() {
        requestAnimationFrame(() => {

            this.bufferA.uniforms[ 'iFrame' ].value = this.counter++

            this.bufferA.uniforms[ 'iChannel0' ].value = this.targetA.readBuffer.texture
            // this.bufferA.uniforms[ 'iChannel1' ].value = this.targetB.readBuffer.texture
            this.targetA.render(this.bufferA.scene, this.orthoCamera)

            // this.bufferB.uniforms[ 'iChannel0' ].value = this.targetA.readBuffer.texture
            // this.targetB.render(this.sceneB, this.orthoCamera)

            this.bufferImage.uniforms[ 'iChannel1' ].value = this.targetA.readBuffer.texture
            this.targetC.render(this.bufferImage.scene, this.orthoCamera, true)

            this.animate()

        })

    }

}

class BufferShader {

    public material: THREE.ShaderMaterial
    public scene: THREE.Scene

    constructor(fragmentShader: string, public uniforms: {} = {}) {
        this.material = new THREE.ShaderMaterial({
            fragmentShader,
            vertexShader: VERTEX_SHADER,
            uniforms
        })
        this.scene = new THREE.Scene()
        this.scene.add(
            new THREE.Mesh(new THREE.PlaneBufferGeometry(2, 2), this.material)
        )
    }

}

class BufferManager {

    public readBuffer: THREE.WebGLRenderTarget
    public writeBuffer: THREE.WebGLRenderTarget

    constructor(private renderer: THREE.WebGLRenderer, { width, height }) {

        this.readBuffer = new THREE.WebGLRenderTarget(width, height, {
            minFilter: THREE.LinearFilter,
            magFilter: THREE.LinearFilter,
            format: THREE.RGBAFormat,
            stencilBuffer: false
        })

        this.writeBuffer = this.readBuffer.clone()

    }

    public swap() {
        const temp = this.readBuffer
        this.readBuffer = this.writeBuffer
        this.writeBuffer = temp
    }

    public render(scene: THREE.Scene, camera: THREE.Camera, toScreen: boolean = false) {
        if (toScreen) {
            this.renderer.render(scene, camera)
        } else {
            this.renderer.render(scene, camera, this.writeBuffer, true)
        }
        this.swap()
    }

}


(new App()).start()

              
            
!
999px

Console