4

我正在尝试制作一个模拟长时间曝光摄影的应用程序。这个想法是我从网络摄像头中抓取当前帧并将其合成到画布上。随着时间的推移,照片会“曝光”,变得越来越亮。(见http://www.chromeexperiments.com/detail/light-paint-live-mercury/?f=

我有一个完美运行的着色器。这就像 Photoshop 中的“添加”混合模式。问题是我无法让它回收前一帧。

我认为这将是一些简单的事情renderer.autoClear = false;,但在这种情况下,该选项似乎无济于事。

这是使用 THREE.EffectComposer 应用着色器的代码。

        onWebcamInit: function () {    
            var $stream = $("#user-stream"),
                width = $stream.width(),
                height = $stream.height(),
                near = .1,
                far = 10000;

            this.renderer = new THREE.WebGLRenderer();
            this.renderer.setSize(width, height);
            this.renderer.autoClear = false;
            this.scene = new THREE.Scene();

            this.camera = new THREE.OrthographicCamera(width / -2, width / 2, height / 2, height / -2, near, far);
            this.scene.add(this.camera);

            this.$el.append(this.renderer.domElement);

            this.frameTexture = new THREE.Texture(document.querySelector("#webcam"));
            this.compositeTexture = new THREE.Texture(this.renderer.domElement);

            this.composer = new THREE.EffectComposer(this.renderer);

            // same effect with or without this line
            // this.composer.addPass(new THREE.RenderPass(this.scene, this.camera));

            var addEffect = new THREE.ShaderPass(addShader);
            addEffect.uniforms[ 'exposure' ].value = .5;
            addEffect.uniforms[ 'frameTexture' ].value = this.frameTexture;
            addEffect.renderToScreen = true;
            this.composer.addPass(addEffect);

            this.plane = new THREE.Mesh(new THREE.PlaneGeometry(width, height, 1, 1), new THREE.MeshBasicMaterial({map: this.compositeTexture}));
            this.scene.add(this.plane);

            this.frameTexture.needsUpdate = true;
            this.compositeTexture.needsUpdate = true;

            new FrameImpulse(this.renderFrame);

        },
        renderFrame: function () {
            this.frameTexture.needsUpdate = true;
            this.compositeTexture.needsUpdate = true;
            this.composer.render();
        }

这是着色器。没有什么花哨。

        uniforms: {
            "tDiffuse": { type: "t", value: null },
            "frameTexture": { type: "t", value: null },
            "exposure": { type: "f", value: 1.0 }
        },

        vertexShader: [
            "varying vec2 vUv;",
            "void main() {",
            "vUv = uv;",
            "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

            "}"
        ].join("\n"),

        fragmentShader: [

            "uniform sampler2D frameTexture;",
            "uniform sampler2D tDiffuse;",
            "uniform float exposure;",
            "varying vec2 vUv;",

            "void main() {",
            "vec4 n = texture2D(frameTexture, vUv);",
            "vec4 o = texture2D(tDiffuse, vUv);",
            "vec3 sum = n.rgb + o.rgb;",
            "gl_FragColor = vec4(mix(o.rgb, sum.rgb, exposure), 1.0);",
            "}"

        ].join("\n")
4

3 回答 3

2

这在本质上等同于 posit labs 的答案,但我已经通过更简化的解决方案取得了成功——我创建了一个只有我想要回收的 ShaderPass 的 EffectComposer,然后在每次渲染时为该 Composer 交换 renderTargets。

初始化:

THREE.EffectComposer.prototype.swapTargets = function() {
    var tmp = this.renderTarget2;
    this.renderTarget2 = this.renderTarget1;
    this.renderTarget1 = tmp;
};

...

composer = new THREE.EffectComposer(renderer,  
    new THREE.WebGLRenderTarget(512, 512, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat })
);

var addEffect = new THREE.ShaderPass(addShader, 'frameTexture');
addEffect.renderToScreen = true;
this.composer.addPass(addEffect);

使成为:

composer.render();
composer.swapTargets();

然后,辅助 EffectComposer 可以采用两个 renderTargets 之一并将其推送到屏幕或进一步转换它。

另请注意,我在初始化 ShaderPass 时将“frameTexture”声明为 textureID。这让 ShaderPass 知道用上一个 Pass 的结果更新 frameTexture 统一。

于 2014-01-03T17:49:43.527 回答
1

要实现这种反馈效果,您必须交替编写不同的WebGLRenderTarget. 否则,帧缓冲区将被覆盖。不完全确定为什么会发生这种情况......但这是解决方案。

在里面:

    this.rt1 = new THREE.WebGLRenderTarget(512, 512, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat });
    this.rt2 = new THREE.WebGLRenderTarget(512, 512, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat });

使成为:

    this.renderer.render(this.scene, this.camera);
    this.renderer.render(this.scene, this.camera, this.rt1, false);

    // swap buffers
    var a = this.rt2;
    this.rt2 = this.rt1;
    this.rt1 = a;
    this.shaders.add.uniforms.tDiffuse.value = this.rt2;
于 2013-11-13T20:58:38.687 回答
0

试试这个:

this.renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );
于 2013-11-16T14:01:55.290 回答