简体   繁体   English

Three.js 将 THREE.ShaderMaterial 用于 map 将 renderTarget.texture 用于四边形时出现纹理缩放问题

[英]Three.js Texture scaling issue when using THREE.ShaderMaterial to map a renderTarget.texture to a quad

A codepen demonstrating this issue is here: https://codepen.io/lilrooness/pen/QWjdjgP演示此问题的代码笔在这里: https://codepen.io/lilrooness/pen/QWjdjgP

I'm rendering noise to a render target and then using that render target to texture a quad that I render to the screen.我正在将噪声渲染到渲染目标,然后使用该渲染目标对渲染到屏幕的四边形进行纹理处理。

When using the render target as a texure I'm encoutering a problem with the textures size.当使用渲染目标作为纹理时,我遇到了纹理大小的问题。

It works fine if I use new THREE.MeshBasicMaterial({ map: renderTarget.texture })如果我使用new THREE.MeshBasicMaterial({ map: renderTarget.texture })它工作正常

在此处输入图像描述

but when I use my own material但是当我使用自己的材料时

        var renderMaterial = new THREE.ShaderMaterial({
            uniforms: {
                tex: { type: 'sampler2D', value: renderTarget.texture }
            },
            fragmentShader: pixelateFragmentShader(),
            vertexShader: standardVertexShader()
        })

I get a very small texture that's clamped我得到一个非常小的纹理,被夹住了在此处输入图像描述

This is the vertex shader that I use for both renders:这是我用于两种渲染的顶点着色器:

                varying lowp vec3 vUv;

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

this is the rendering function (im using the same camera to render both times)这是渲染 function (我使用同一个相机渲染两次)

        function animate() {
            time += 0.1;
            quad.material.uniforms.time.value = time;
            requestAnimationFrame(animate);
            renderer.setRenderTarget(renderTarget);
            renderer.render(bufferScene, camera);
            renderer.setRenderTarget(null);
            renderer.render(scene, camera);
        }

Again, this works fine if I use the MeshBasicMaterial.同样,如果我使用 MeshBasicMaterial,这会很好。 What am I doing wrong?我究竟做错了什么?

The problem was that, in my vertex shader I was setting vUv = position问题是,在我的顶点着色器中,我设置vUv = position

While this was desired behaviour for the perlin noise effect, I was re-using the same vertex shader to render the render target texture虽然这是 perlin 噪声效果的理想行为,但我重新使用相同的顶点着色器来渲染渲染目标纹理

I changed it to: vUv = uv;我将其更改为: vUv = uv;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM