【问题标题】:three.js - fragment shader basics - color a fragment by it's positionthree.js - 片段着色器基础 - 根据片段的位置为片段着色
【发布时间】:2013-02-13 12:13:50
【问题描述】:

我尝试根据片段在最高和最低顶点之间的位置为着色器中的片段着色。以下是着色器:

<script type="x-shader/x-vertex" id="vertexshader">

    uniform     float   span;

    attribute   float   displacement;

    varying     vec3    vNormal;
    varying     float   color_according_to_z;

                float   z_actual;

    void main() {
        vNormal = normal;

        vec3 newPosition = position + normal * vec3(0.0, 0.0, displacement);
        gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);

        z_actual = gl_Position.z + span / 2.0;
        color_according_to_z = 1.0 / span * z_actual;
    }

</script>

<script type="x-shader/x-fragment" id="fragmentshader">

    uniform     float   span;

    varying     float   color_according_to_z;

    void main()
    {
        gl_FragColor = vec4(color_according_to_z, 0.5, 0.5, 1.0);
    }

</script>

这是我的渲染函数:

function render() {
    requestAnimationFrame(render);

    plane.rotation.x = global_x_rotation;
    plane.rotation.z = global_z_rotation;

    for(var i = attributes.displacement.value.length - 1; i >= 0; i--) {
        attributes.displacement.value[i] = attributes.displacement.value[i] - 0.5 + Math.random();
    };

    uniforms.lowest = attributes.displacement.value.min();
    uniforms.highest = attributes.displacement.value.max();
    uniforms.span = uniforms.highest - uniforms.lowest;

    attributes.displacement.needsUpdate = true;

    uniforms.lowest.needsUpdate = true;
    uniforms.highest.needsUpdate = true;
    uniforms.span.needsUpdate = true;

    renderer.render(scene, camera);
}

我在帧之间更改位移并重新计算最高和最低顶点之间的跨度。

最后一块,如何根据片段的最高或最低位置来绘制片段,我还想不通。

Here is a jsfiddle with the mentioned code.

【问题讨论】:

  • 不尝试你的代码,在我看来你已经拥有了你需要的东西。你在看什么?可以发个截图吗?
  • 我给你做了一个小屏幕录制:cl.ly/3I3D0D3l3C1Q。顶点移动得很好,但着色不起作用。
  • 不确定我是否想在工作中打开一个名为“shagging.mov”的东西=O
  • 对不起那个。我向你保证,这只是一些(虽然很性感)顶点在屏幕上移动。
  • 好的...所以从颜色上看,片段着色器执行得很好,但是 color_according_to_z

标签: javascript glsl three.js webgl shader


【解决方案1】:

您有几个我可以看到的错误。检查变量中的 NaN。

着色器逻辑看起来不错,但它可能无法产生您想要的颜色。

var max = -1.0e30;
var min = +1.0e30;
var x;
for(var i = attributes.displacement.value.length - 1; i >= 0; i--) {
    x = attributes.displacement.value[i] += - 0.5 + Math.random();
    if (x < min ) min = x;
    if (x > max ) max = x;
};

uniforms.lowest.value = min; // lowest.value !
uniforms.highest.value = max;
uniforms.span.value = max - min;

attributes.displacement.needsUpdate = true;

//uniforms.lowest.needsUpdate = true; // not needed for uniforms
//uniforms.highest.needsUpdate = true;
//uniforms.span.needsUpdate = true;

小提琴:http://jsfiddle.net/c2jry/2/

【讨论】:

  • uniforms.span 在我上次检查时计算得很好。您按照此处所述的方式进行操作是否有特殊原因?
  • 你离开了 .value。 -- 跨度:{ 类型:'f',值:0.0 }
  • 既然我在这里:有没有办法调试着色器?喜欢里面的console.log吗?
  • jsfiddle.net/c2jry/1 你现在只需要让你的着色器逻辑正确。那是你的工作:-)
  • 我的逻辑让我非常失望。非常感谢您的帮助。
猜你喜欢
  • 2018-02-04
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2013-09-17
  • 2020-04-05
相关资源
最近更新 更多