【发布时间】: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);
}
我在帧之间更改位移并重新计算最高和最低顶点之间的跨度。
最后一块,如何根据片段的最高或最低位置来绘制片段,我还想不通。
【问题讨论】:
-
不尝试你的代码,在我看来你已经拥有了你需要的东西。你在看什么?可以发个截图吗?
-
我给你做了一个小屏幕录制:cl.ly/3I3D0D3l3C1Q。顶点移动得很好,但着色不起作用。
-
不确定我是否想在工作中打开一个名为“shagging.mov”的东西=O
-
对不起那个。我向你保证,这只是一些(虽然很性感)顶点在屏幕上移动。
-
好的...所以从颜色上看,片段着色器执行得很好,但是 color_according_to_z
标签: javascript glsl three.js webgl shader