简体   繁体   English

THREE.js着色器的颜色纹理

[英]THREE.js Color texture by shader

i've added simple box to my scene, and I want to create shader that will add a texture to it and add color to this texture. 我在场景中添加了一个简单的盒子,我想创建一个着色器,为它添加纹理并为该纹理添加颜色。

This is my vertex shader(nothing special about it): 这是我的顶点着色器(没什么特别的):

<script id="vertexShader" type="x-shader/x-vertex">
        varying vec2 vUv;
        void main() {
        vUv = uv;

        gl_Position =   projectionMatrix * modelViewMatrix * vec4(position,1.0);
        }
</script>

And this is my fragment shader: 这是我的片段着色器:

<script id="fragmentShader" type="x-shader/x-fragment">
        uniform vec2 resolution;
        uniform float time;
        uniform sampler2D texture;
        varying vec2 vUv;
        uniform vec3 color;
        varying vec3 vColor;
        void mainImage( out vec4 fragColor, in vec2 fragCoord )
        {
        vec2 uv = fragCoord.xy / vec2(1920.0, 1920.0);
        fragColor = vec4(uv, 0.5 + 0.5 * cos(time) * sin(time) ,1.0);
        }
        void main( void ) {
        vec4 color = vec4(0.0,0.0,0.0,1.0);
        mainImage( color, gl_FragCoord.xy );

        color.w = 1.0;
        gl_FragColor = texture2D(texture, vUv);
        gl_FragColor = color;

        }
 </script>

As You can see, in two last lines i've sets gl_FragColor with texture and color. 如您所见,在最后两行中,我设置了具有纹理和颜色的gl_FragColor。 When last line is gl_FragColor = color; 当最后一行是gl_FragColor = color; The result is: 结果是: 颜色 When i change order and last line is gl_FragColor = texture2D(texture, vUv); 当我更改顺序并且最后一行是gl_FragColor = texture2D(texture, vUv); The result is: 结果是: 质地

This is the code that use shader and add a box to a scene: 这是使用着色器并向场景添加框的代码:

var geometry = new THREE.BoxGeometry(.5, 1, .5);    
uniforms = {
   time: { type: "f", value: 1.0 },
   resolution: { type: "v2", value: new THREE.Vector2() },
   texture: { type: "t", value: THREE.ImageUtils.loadTexture("../img/disc.png") }
            };

var material = new THREE.ShaderMaterial({
                transparent: true,
                uniforms: uniforms,
                vertexShader: document.getElementById('vertexShader').textContent,
                fragmentShader: document.getElementById('fragmentShader').textContent

            });

var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

Is there a possibility to color my texture(mix color and texture in object) by this shader in the same way like in the picture? 是否有可能以与图片中相同的方式通过此着色器为我的纹理着色(在对象中混合颜色和纹理)?

Just like any other variable setting it twice just makes it the 2nd result; 就像将其他变量设置两次一样,它只是第二个结果。

var foo;
foo = 123;
foo = 456;

foo now equals 456 foo现在等于456

If you want to combine the results you need to something yourself to combine them 如果要合并结果,则需要自己进行合并

 gl_FragColor = texture2D(texture, vUv);
 gl_FragColor = color;

Just means gl_FragColor equals color ; 只是意味着gl_FragColor等于color

I'm guessing you want something like 我猜你想要类似的东西

 gl_FragColor = texture2D(texture, vUv) * color;

But of course what math you use is up to you depending on what you're trying to achieve. 但是,当然,要使用哪种数学方法取决于您要达到的目标。

Given your example texture and color above multiplying the texture by the color you'll get a circle with a gradient color. 给定您的示例纹理和颜色(上面将纹理与颜色相乘),您将得到一个带有渐变颜色的圆。 It's not clear if that's the result you wanted. 尚不清楚这是否是您想要的结果。

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

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