1 /*** 2 * Very simple shaders as strings. Catenate these in front of your 3 * WebGL code, use what you need, and minimize the result using 4 * Closure. Non-used shader versions will vanish from final output. 5 * 6 * The strategy is, of course, to select the smallest shader that 7 * suffices for what you want to do. If you have time to tinker, you 8 * can always remove or simplify features from these, as well, if you 9 * run out of space for artwork :). 10 */ 11 12 /** A very basic vertex shader: Just interpolate color (g) and 13 * position (v) projected by perspective (p) and modelview (mv) 14 * transforms. We transform normals by the modelview (not its 15 * inverse transpose) so free scale / skew cannot be done with 16 * correct normals. Orthonormal transformations should be OK. 17 */ 18 19 var vertex_shader_basic = 20 //"precision mediump float;"+ //Vertex shader has default prec. 21 "uniform mat4 mv,p;" + 22 "attribute vec4 g,v,N;" + 23 "varying vec4 c,n,r;" + 24 "void main(){" + 25 "gl_Position=p*mv*v;" + 26 "n=mv*N;" + // Normal (assuming mv is orthonormal) 27 "c=g;" + // "Color" (whatever in the fragment shader) 28 "r=-mv*v;" + // View diRection 29 "}"; 30 31 /** A vertex shader that uses normal matrix. It should be a 4x4 matrix 32 * whose upper left 3x3 part contains the inverse transpose of 33 * modelview. Otherwise as simple as the previous one. This allows 34 * free scale/skew of geometries with the expense of requiring you to 35 * compute and transfer the normal matrix. It is some heavy code, so 36 * you should really go and scale your sH* to make it look awesome, if 37 * you decide to compute inverses. 38 */ 39 var vertex_shader_minimal_with_normalmatrix = 40 //"precision mediump float;"+ //Vertex shader has default prec. 41 "uniform mat4 mv,nm,p;" + 42 "attribute vec4 g,v,N;" + 43 "varying vec4 c,n,r;" + 44 "void main(){" + 45 "gl_Position=p*mv*v;" + 46 "n=nm*N;" + // Normal 47 "c=g;" + // "Color" (whatever in the fragment shader) 48 "r=-mv*v;" + // View diRection 49 "}"; 50 51 52 // Fragment shader: Only one unidirectional light is used. 53 54 var fragment_shader_camspace_directed = 55 //"precision mediump float;"+ // one-by-one definitions are shorter 56 // Inputs: 57 // a: ambient color - now coming in as i[0] 58 // d: diffuse color - now coming in as i[1] 59 // s: specular color - now coming in as i[2] 60 // q: additional params [shininess, "par2", mesh_brightn] 61 // - now coming in as i[3] 62 // l: light direction in camera space, pre-normalized 63 // r: view direction, assume already normalized 64 // 65 // u: [time, window w, window h] - not used in this production 66 'uniform highp mat4 i;' + 67 'uniform highp vec4 l;' + 68 'varying highp vec4 c,n,r;' + 69 'void main(){' + 70 // win 10 bytes in packed space by re-normalizing n twice. 71 // As always, time we can spend, but space not so much. 72 'gl_FragColor=' + 73 // Locate triangle boundaries from "vertex coloring": 74 'i[3].bbbb*max(0.,1.-4.*min(c.b,min(c.g,c.r)))' + 75 // Clamp output at ambient color (incl. alpha): 76 '+max(i[0],'+ 77 // Diffuse reflection 78 'i[1]*max(0.,dot(normalize(l),normalize(n)))' + 79 // Specular reflection 80 '+i[2]*pow(max(0.,dot(normalize(r),'+ 81 'reflect(-normalize(l),normalize(n))' + 82 ')),' + 83 'i[3].r' + 84 ')' + 85 ')' + 86 ';' + 87 '}'; 88 89 var fragment_shader_camspace_directed_with_white_fog = 90 //"precision mediump float;"+ // one-by-one definitions are shorter 91 // Inputs: 92 // a: ambient color - now coming in as i[0] 93 // d: diffuse color - now coming in as i[1] 94 // s: specular color - now coming in as i[2] 95 // q: additional params [shininess, "par2", mesh_brightn] 96 // - now coming in as i[3] 97 // l: light direction in camera space, pre-normalized 98 // r: view direction, assume already normalized 99 // 100 // u: [time, window w, window h] - not used in this production 101 'uniform highp mat4 i;' + 102 'uniform highp vec4 l;' + 103 'varying highp vec4 c,n,r;' + 104 'void main(){' + 105 // win 10 bytes in packed space by re-normalizing n twice. 106 // As always, time we can spend, but space not so much. 107 'gl_FragColor= vec4(1.-clamp(exp(-.02*r.z), 0., 1.))+' + 108 // Locate triangle boundaries from "vertex coloring": 109 'i[3].bbbb*max(0.,1.-4.*min(c.b,min(c.g,c.r)))' + 110 // Clamp output at ambient color (incl. alpha): 111 '+max(i[0],'+ 112 // Diffuse reflection 113 'i[1]*max(0.,dot(normalize(l),normalize(n)))' + 114 // Specular reflection 115 '+i[2]*pow(max(0.,dot(normalize(r),'+ 116 'reflect(-normalize(l),normalize(n))' + 117 ')),' + 118 'i[3].r' + 119 ')' + 120 ')' + 121 ';' + 122 '}'; 123 124 125 /** Phong model with one point light given in camera space. */ 126 var fragment_shader_pointlight_cameraspace = 127 //"precision mediump float;"+ // one-by-one definitions are shorter 128 // Inputs: 129 // a: ambient color - now coming in as i[0] 130 // d: diffuse color - now coming in as i[1] 131 // s: specular color - now coming in as i[2] 132 // q: additional params [shininess, "par2", mesh_brightn] 133 // - now coming in as i[3] 134 // l: point light position in camera space 135 // r: view direction, assume already normalized 136 // 137 // u: [time, window w, window h] - not used in this production 138 'uniform highp mat4 i;' + 139 'uniform highp vec4 l;' + 140 'varying highp vec4 c,n,r;' + 141 'void main(){' + 142 // win 10 bytes in packed space by re-normalizing n twice. 143 // As always, time we can spend, but space not so much. 144 'gl_FragColor=' + 145 // Locate triangle boundaries from "vertex coloring": 146 'i[3].bbbb*max(0.,1.-4.*min(c.b,min(c.g,c.r)))' + 147 // Clamp output at ambient color (incl. alpha): 148 '+max(i[0],'+ 149 // Diffuse reflection 150 'i[1]*max(0.,dot(normalize(l),normalize(n)))' + 151 // Specular reflection 152 '+i[2]*pow(max(0.,dot(normalize(r),'+ 153 'reflect(-l,normalize(n))' + 154 ')),' + 155 'i[3].r' + 156 ')' + 157 ')' + 158 ';' + 159 '}'; 160