Note that there are some explanatory texts on larger screens.

plurals
  1. POthree.js - fragment shader basics - color a fragment by it's position
    primarykey
    data
    text
    <p>I try to color the fragments in my shader according to their position between highest and lowest vertex. Here are the shaders:</p> <pre><code>&lt;script type="x-shader/x-vertex" id="vertexshader"&gt; 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; } &lt;/script&gt; &lt;script type="x-shader/x-fragment" id="fragmentshader"&gt; uniform float span; varying float color_according_to_z; void main() { gl_FragColor = vec4(color_according_to_z, 0.5, 0.5, 1.0); } &lt;/script&gt; </code></pre> <p>Here is my render function:</p> <pre><code>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 &gt;= 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); } </code></pre> <p>I change the displacement from frame to frame and the recalculate the span between the highest and the lowest vertices.</p> <p>The final piece, how to paint a fragment according to it's position towards highest or lowest, I couldn't figure out yet.</p> <p><strong><a href="http://jsfiddle.net/ichbinadrian/c2jry/" rel="nofollow">Here is a jsfiddle with the mentioned code.</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload