|
| 1 | + |
| 2 | +#define PROCESSING_TEXLIGHT_SHADER |
| 3 | + |
| 4 | +uniform mat4 modelview; |
| 5 | +uniform mat4 transform; |
| 6 | +uniform mat3 normalMatrix; |
| 7 | +uniform mat4 texMatrix; |
| 8 | + |
| 9 | +uniform int lightCount; |
| 10 | +uniform vec4 lightPosition[8]; |
| 11 | +uniform vec3 lightNormal[8]; |
| 12 | +uniform vec3 lightAmbient[8]; |
| 13 | +uniform vec3 lightDiffuse[8]; |
| 14 | +uniform vec3 lightSpecular[8]; |
| 15 | +uniform vec3 lightFalloff[8]; |
| 16 | +uniform vec2 lightSpot[8]; |
| 17 | + |
| 18 | +attribute vec4 vertex; |
| 19 | +attribute vec4 color; |
| 20 | +attribute vec3 normal; |
| 21 | +attribute vec2 texCoord; |
| 22 | + |
| 23 | +attribute vec4 ambient; |
| 24 | +attribute vec4 specular; |
| 25 | +attribute vec4 emissive; |
| 26 | +attribute float shininess; |
| 27 | + |
| 28 | +varying vec4 vertColor; |
| 29 | +varying vec4 vertTexCoord; |
| 30 | + |
| 31 | +const float zero_float = 0.0; |
| 32 | +const float one_float = 1.0; |
| 33 | +const vec3 zero_vec3 = vec3(0); |
| 34 | + |
| 35 | +uniform float displaceStrength; |
| 36 | +uniform float time; |
| 37 | + |
| 38 | +float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) { |
| 39 | + vec3 lpv = lightPos - vertPos; |
| 40 | + vec3 dist = vec3(one_float); |
| 41 | + dist.z = dot(lpv, lpv); |
| 42 | + dist.y = sqrt(dist.z); |
| 43 | + return one_float / dot(dist, coeff); |
| 44 | +} |
| 45 | + |
| 46 | +float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) { |
| 47 | + vec3 lpv = normalize(lightPos - vertPos); |
| 48 | + vec3 nln = -one_float * lightNorm; |
| 49 | + float spotCos = dot(nln, lpv); |
| 50 | + return spotCos <= minCos ? zero_float : pow(spotCos, spotExp); |
| 51 | +} |
| 52 | + |
| 53 | +float lambertFactor(vec3 lightDir, vec3 vecNormal) { |
| 54 | + return max(zero_float, dot(lightDir, vecNormal)); |
| 55 | +} |
| 56 | + |
| 57 | +float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) { |
| 58 | + vec3 np = normalize(vertPos); |
| 59 | + vec3 ldp = normalize(lightDir - np); |
| 60 | + return pow(max(zero_float, dot(ldp, vecNormal)), shine); |
| 61 | +} |
| 62 | + |
| 63 | +// Source code for GLSL Perlin noise courtesy of: |
| 64 | +// https://github.com/ashima/webgl-noise/wiki |
| 65 | + |
| 66 | +vec3 mod289(vec3 x) { |
| 67 | + return x - floor(x * (1.0 / 289.0)) * 289.0; |
| 68 | +} |
| 69 | + |
| 70 | +vec2 mod289(vec2 x) { |
| 71 | + return x - floor(x * (1.0 / 289.0)) * 289.0; |
| 72 | +} |
| 73 | + |
| 74 | +vec3 permute(vec3 x) { |
| 75 | + return mod289(((x*34.0)+1.0)*x); |
| 76 | +} |
| 77 | + |
| 78 | +float snoise(vec2 v) |
| 79 | + { |
| 80 | + const vec4 C = vec4(0.211324865405187, // (3.0-sqrt(3.0))/6.0 |
| 81 | + 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) |
| 82 | + -0.577350269189626, // -1.0 + 2.0 * C.x |
| 83 | + 0.024390243902439); // 1.0 / 41.0 |
| 84 | +// First corner |
| 85 | + vec2 i = floor(v + dot(v, C.yy) ); |
| 86 | + vec2 x0 = v - i + dot(i, C.xx); |
| 87 | + |
| 88 | +// Other corners |
| 89 | + vec2 i1; |
| 90 | + //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0 |
| 91 | + //i1.y = 1.0 - i1.x; |
| 92 | + i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0); |
| 93 | + // x0 = x0 - 0.0 + 0.0 * C.xx ; |
| 94 | + // x1 = x0 - i1 + 1.0 * C.xx ; |
| 95 | + // x2 = x0 - 1.0 + 2.0 * C.xx ; |
| 96 | + vec4 x12 = x0.xyxy + C.xxzz; |
| 97 | + x12.xy -= i1; |
| 98 | + |
| 99 | +// Permutations |
| 100 | + i = mod289(i); // Avoid truncation effects in permutation |
| 101 | + vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 )) |
| 102 | ++ i.x + vec3(0.0, i1.x, 1.0 )); |
| 103 | + |
| 104 | + vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0); |
| 105 | + m = m*m ; |
| 106 | + m = m*m ; |
| 107 | + |
| 108 | +// Gradients: 41 points uniformly over a line, mapped onto a diamond. |
| 109 | +// The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) |
| 110 | + |
| 111 | + vec3 x = 2.0 * fract(p * C.www) - 1.0; |
| 112 | + vec3 h = abs(x) - 0.5; |
| 113 | + vec3 ox = floor(x + 0.5); |
| 114 | + vec3 a0 = x - ox; |
| 115 | + |
| 116 | +// Normalise gradients implicitly by scaling m |
| 117 | +// Approximation of: m *= inversesqrt( a0*a0 + h*h ); |
| 118 | + m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h ); |
| 119 | + |
| 120 | +// Compute final noise value at P |
| 121 | + vec3 g; |
| 122 | + g.x = a0.x * x0.x + h.x * x0.y; |
| 123 | + g.yz = a0.yz * x12.xz + h.yz * x12.yw; |
| 124 | + return 130.0 * dot(m, g); |
| 125 | +} |
| 126 | + |
| 127 | +void main() { |
| 128 | + // Calculating texture coordinates, with r and q set both to one |
| 129 | + vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0); |
| 130 | + |
| 131 | + vec2 p = texCoord; // put coordinates into vec2 p for convenience |
| 132 | + p.x += time; // add time to make the noise and the subsequent displacement move |
| 133 | + float df = snoise( p ); // create displacement float value from shader-based 2D Perlin noise |
| 134 | + vec4 newVertexPos = vertex + vec4(normal * df * displaceStrength, 0.0); // regular vertex position + direction * displacementMap * displaceStrength |
| 135 | + |
| 136 | + // Vertex in clip coordinates |
| 137 | + gl_Position = transform * newVertexPos; |
| 138 | + |
| 139 | + // Vertex in eye coordinates |
| 140 | + vec3 ecVertex = vec3(modelview * vertex); |
| 141 | + |
| 142 | + // Normal vector in eye coordinates |
| 143 | + vec3 ecNormal = normalize(normalMatrix * normal); |
| 144 | + |
| 145 | + if (dot(-one_float * ecVertex, ecNormal) < zero_float) { |
| 146 | + // If normal is away from camera, choose its opposite. |
| 147 | + // If we add backface culling, this will be backfacing |
| 148 | + ecNormal *= -one_float; |
| 149 | + } |
| 150 | + |
| 151 | + // Light calculations |
| 152 | + vec3 totalAmbient = vec3(0, 0, 0); |
| 153 | + vec3 totalDiffuse = vec3(0, 0, 0); |
| 154 | + vec3 totalSpecular = vec3(0, 0, 0); |
| 155 | + for (int i = 0; i < 8; i++) { |
| 156 | + if (lightCount == i) break; |
| 157 | + |
| 158 | + vec3 lightPos = lightPosition[i].xyz; |
| 159 | + bool isDir = zero_float < lightPosition[i].w; |
| 160 | + float spotCos = lightSpot[i].x; |
| 161 | + float spotExp = lightSpot[i].y; |
| 162 | + |
| 163 | + vec3 lightDir; |
| 164 | + float falloff; |
| 165 | + float spotf; |
| 166 | + |
| 167 | + if (isDir) { |
| 168 | + falloff = one_float; |
| 169 | + lightDir = -one_float * lightNormal[i]; |
| 170 | + } else { |
| 171 | + falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]); |
| 172 | + lightDir = normalize(lightPos - ecVertex); |
| 173 | + } |
| 174 | + |
| 175 | + spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i], |
| 176 | + spotCos, spotExp) |
| 177 | + : one_float; |
| 178 | + |
| 179 | + if (any(greaterThan(lightAmbient[i], zero_vec3))) { |
| 180 | + totalAmbient += lightAmbient[i] * falloff; |
| 181 | + } |
| 182 | + |
| 183 | + if (any(greaterThan(lightDiffuse[i], zero_vec3))) { |
| 184 | + totalDiffuse += lightDiffuse[i] * falloff * spotf * |
| 185 | + lambertFactor(lightDir, ecNormal); |
| 186 | + } |
| 187 | + |
| 188 | + if (any(greaterThan(lightSpecular[i], zero_vec3))) { |
| 189 | + totalSpecular += lightSpecular[i] * falloff * spotf * |
| 190 | + blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Calculating final color as result of all lights (plus emissive term). |
| 195 | + // Transparency is determined exclusively by the diffuse component. |
| 196 | + vertColor = vec4(totalAmbient, 0) * ambient + |
| 197 | + vec4(totalDiffuse, 1) * color + |
| 198 | + vec4(totalSpecular, 0) * specular + |
| 199 | + vec4(emissive.rgb, 0); |
| 200 | + |
| 201 | +} |
0 commit comments