Compile
Report
Toggle view
Save
// VertexPosition is a special function; whatever it returns is what glPosition ends up being. // In this toy example, you can just comment this out -- a reasonable one is hard coded in function VertexPosition() { // All varyings are specified via 'varyings.
'. Attributes with 'attributes.
' // // Currently, you can't use attributes in FragmentColor; you must create a varying for it. // in the future, it might auto-transfer used attributes in the fragment shader into // varyings; it remains to be seen if this is a good idea. varyings.pt = attributes.pt; // uniforms.
var t = uniforms.t; // You can return whatever size array/vector; it automatically completes the vector for // you with all 0's, except for 1 as the last element. return [Math.cos(t) * attributes.pt[0] + Math.sin(t) * attributes.pt[1], Math.sin(t) * attributes.pt[0] - Math.cos(t) * attributes.pt[1] ]; }; /** FragmentColor is a special function; it tries to resolve to a vector and that is what is used to set the fragment color */ function FragmentColor() { var x = varyings.pt[0]; var y = varyings.pt[1]; var t = (x * y > 0) ? uniforms.t : uniforms.t * 2; // A handful of Math.x functions are mapped to the GLSL equivalents var dist = Math.sqrt(x*x + y*y); var threshold = Math.abs(Math.cos(t)); var rim = .01; if(dist > (threshold-rim) && dist < (threshold+rim)) { // Returning nothing in a FragmentColor function // is equivalent to performing a GLSL 'discard' -- it // simply does nothing with the fragment return; } if(dist > threshold){ return [fmod(x, 1.0), fmod(y, 1.0), 0.5*(Math.cos(t)+1.0)]; } // FragmentColor, like VertexPosition, will fill out the vec4 for you if you // dont. You do have to be consistent though -- Since we returned a vector of 3 // above, we have to here. Otherwise it is a type error. For example: // return [1, 1, 1, 1]; // wont work. return [0.5*(x+1.0), 0.5*(y+1.0) , 0.5*(Math.cos(t)+1.0)]; }; /** Functions you define automatically get pulled in if they are used */ function fmod(a,b) { return a - (Math.floor(a / b) * b); };