ShaderUtils.js 10.3 KB
Newer Older
M
Mr.doob 已提交
1 2 3
var ShaderUtils = {

	lib: { 'fresnel': {
M
Mr.doob 已提交
4 5 6

			uniforms: {

M
Mr.doob 已提交
7
			"mRefractionRatio": { type: "f", value: 1.02 },
M
Mr.doob 已提交
8 9 10 11 12
			"mFresnelBias": { type: "f", value: 0.1 },
			"mFresnelPower": { type: "f", value: 2.0 },
			"mFresnelScale": { type: "f", value: 1.0 },
			"tCube": { type: "t", value: 1, texture: null }

M
Mr.doob 已提交
13
			},
M
Mr.doob 已提交
14

M
Mr.doob 已提交
15
			fragment_shader: [
M
Mr.doob 已提交
16

M
Mr.doob 已提交
17
			"uniform samplerCube tCube;",
M
Mr.doob 已提交
18

M
Mr.doob 已提交
19 20 21
			"varying vec3 vReflect;",
			"varying vec3 vRefract[3];",
			"varying float vReflectionFactor;",
M
Mr.doob 已提交
22

M
Mr.doob 已提交
23 24 25
			"void main() {",
				"vec4 reflectedColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
				"vec4 refractedColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
M
Mr.doob 已提交
26

M
Mr.doob 已提交
27 28 29 30
				"refractedColor.r = textureCube( tCube, vec3( -vRefract[0].x, vRefract[0].yz ) ).r;",
				"refractedColor.g = textureCube( tCube, vec3( -vRefract[1].x, vRefract[1].yz ) ).g;",
				"refractedColor.b = textureCube( tCube, vec3( -vRefract[2].x, vRefract[2].yz ) ).b;",
				"refractedColor.a = 1.0;",
M
Mr.doob 已提交
31

M
Mr.doob 已提交
32
				"gl_FragColor = mix( refractedColor, reflectedColor, clamp( vReflectionFactor, 0.0, 1.0 ) );",
M
Mr.doob 已提交
33 34
			"}"

M
Mr.doob 已提交
35
			].join("\n"),
M
Mr.doob 已提交
36

M
Mr.doob 已提交
37
			vertex_shader: [
M
Mr.doob 已提交
38

M
Mr.doob 已提交
39 40 41 42 43 44 45 46 47 48 49
			"uniform float mRefractionRatio;",
			"uniform float mFresnelBias;",
			"uniform float mFresnelScale;",
			"uniform float mFresnelPower;",

			"varying vec3 vReflect;",
			"varying vec3 vRefract[3];",
			"varying float vReflectionFactor;",

			"void main(void) {",
				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
50
				"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
M
Mr.doob 已提交
51

M
Mr.doob 已提交
52
				"vec3 nWorld = normalize ( mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal );",
M
Mr.doob 已提交
53

M
Mr.doob 已提交
54
				"vec3 I = mPosition.xyz - cameraPosition;",
M
Mr.doob 已提交
55

M
Mr.doob 已提交
56 57 58 59 60
				"vReflect = reflect( I, nWorld );",
				"vRefract[0] = refract( normalize( I ), nWorld, mRefractionRatio );",
				"vRefract[1] = refract( normalize( I ), nWorld, mRefractionRatio * 0.99 );",
				"vRefract[2] = refract( normalize( I ), nWorld, mRefractionRatio * 0.98 );",
				"vReflectionFactor = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( I ), nWorld ), mFresnelPower );",
M
Mr.doob 已提交
61

M
Mr.doob 已提交
62
				"gl_Position = projectionMatrix * mvPosition;",
M
Mr.doob 已提交
63 64
			"}"

M
Mr.doob 已提交
65
			].join("\n")
M
Mr.doob 已提交
66

67
		},
M
Mr.doob 已提交
68

69
		'normal' : {
M
Mr.doob 已提交
70 71 72

			uniforms: {

73 74 75 76
			"enableAO": { type: "i", value: 0 },
			"enableDiffuse": { type: "i", value: 0 },
			
			"tDiffuse": { type: "t", value: 0, texture: null },
M
Mr.doob 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
			"tNormal": { type: "t", value: 2, texture: null },
			"tAO": { type: "t", value: 3, texture: null },

			"tDisplacement": { type: "t", value: 4, texture: null },
			"uDisplacementBias": { type: "f", value: -0.5 },
			"uDisplacementScale": { type: "f", value: 2.5 },

			"uPointLightPos": { type: "v3", value: new THREE.Vector3() },
			"uPointLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },

			"uDirLightPos":	{ type: "v3", value: new THREE.Vector3() },
			"uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },

			"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },

			"uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
			"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
A
alteredq 已提交
94
			"uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
M
Mr.doob 已提交
95 96 97 98 99 100 101
			"uShininess": { type: "f", value: 30 }

			},

			fragment_shader: [

			"uniform vec3 uDirLightPos;",
102 103
			
			"uniform vec3 uAmbientLightColor;",
M
Mr.doob 已提交
104 105 106
			"uniform vec3 uDirLightColor;",
			"uniform vec3 uPointLightColor;",

A
alteredq 已提交
107
			"uniform vec3 uAmbientColor;",
M
Mr.doob 已提交
108 109 110 111
			"uniform vec3 uDiffuseColor;",
			"uniform vec3 uSpecularColor;",
			"uniform float uShininess;",

112 113 114 115
			"uniform bool enableDiffuse;",
			"uniform bool enableAO;",
			
			"uniform sampler2D tDiffuse;",
M
Mr.doob 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128
			"uniform sampler2D tNormal;",
			"uniform sampler2D tAO;",

			"varying vec3 vTangent;",
			"varying vec3 vBinormal;",
			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vPointLightVector;",
			"varying vec3 vViewPosition;",

			"void main() {",

129 130 131
				"vec3 diffuseTex = vec3( 1.0, 1.0, 1.0 );",
				"vec3 aoTex = vec3( 1.0, 1.0, 1.0 );",
				
M
Mr.doob 已提交
132
				"vec3 normalTex = normalize( texture2D( tNormal, vUv ).xyz * 2.0 - 1.0 );",
133 134 135 136 137 138
				
				"if( enableDiffuse )",
					"diffuseTex = texture2D( tDiffuse, vUv ).xyz;",
					
				"if( enableAO )",
					"aoTex = texture2D( tAO, vUv ).xyz;",
M
Mr.doob 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

				"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
				"vec3 finalNormal = tsb * normalTex;",

				"vec3 normal = normalize( finalNormal );",
				"vec3 viewPosition = normalize( vViewPosition );",

				// point light

				"vec4 pointDiffuse  = vec4( 0.0, 0.0, 0.0, 0.0 );",
				"vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",

				"vec3 pointVector = normalize( vPointLightVector );",
				"vec3 pointHalfVector = normalize( vPointLightVector + vViewPosition );",

				"float pointDotNormalHalf = dot( normal, pointHalfVector );",
				"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",

				"float pointSpecularWeight = 0.0;",
				"if ( pointDotNormalHalf >= 0.0 )",
					"pointSpecularWeight = pow( pointDotNormalHalf, uShininess );",

				"pointDiffuse  += vec4( uDiffuseColor, 1.0 ) * pointDiffuseWeight;",
				"pointSpecular += vec4( uSpecularColor, 1.0 ) * pointSpecularWeight;",

				// directional light

				"vec4 dirDiffuse  = vec4( 0.0, 0.0, 0.0, 0.0 );",
				"vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );",

				"vec4 lDirection = viewMatrix * vec4( uDirLightPos, 0.0 );",

				"vec3 dirVector = normalize( lDirection.xyz );",
				"vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",

				"float dirDotNormalHalf = dot( normal, dirHalfVector );",
				"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",

				"float dirSpecularWeight = 0.0;",
				"if ( dirDotNormalHalf >= 0.0 )",
					"dirSpecularWeight = pow( dirDotNormalHalf, uShininess );",

				"dirDiffuse  += vec4( uDiffuseColor, 1.0 ) * dirDiffuseWeight;",
				"dirSpecular += vec4( uSpecularColor, 1.0 ) * dirSpecularWeight;",

				// all lights contribution summation

186 187 188
				"vec4 totalLight = vec4( uAmbientLightColor * uAmbientColor, 1.0 );",
				"totalLight += vec4( uDirLightColor, 1.0 ) * ( dirDiffuse + dirSpecular );",
				"totalLight += vec4( uPointLightColor, 1.0 ) * ( pointDiffuse + pointSpecular );",
M
Mr.doob 已提交
189

190
				"gl_FragColor = vec4( totalLight.xyz * aoTex * diffuseTex, 1.0 );",
M
Mr.doob 已提交
191 192 193 194 195 196 197 198 199 200

			"}"
			].join("\n"),

			vertex_shader: [

			"attribute vec4 tangent;",

			"uniform vec3 uPointLightPos;",

201
			"#ifdef VERTEX_TEXTURES",
M
Mr.doob 已提交
202 203 204 205 206

				"uniform sampler2D tDisplacement;",
				"uniform float uDisplacementScale;",
				"uniform float uDisplacementBias;",

207
			"#endif",
M
Mr.doob 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

			"varying vec3 vTangent;",
			"varying vec3 vBinormal;",
			"varying vec3 vNormal;",
			"varying vec2 vUv;",

			"varying vec3 vPointLightVector;",
			"varying vec3 vViewPosition;",

			"void main() {",

				"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
				"vViewPosition = cameraPosition - mPosition.xyz;",

				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
				"vNormal = normalize( normalMatrix * normal );",

				// tangent and binormal vectors

				"vTangent = normalize( normalMatrix * tangent.xyz );",

				"vBinormal = cross( vNormal, vTangent ) * tangent.w;",
				"vBinormal = normalize( vBinormal );",

				"vUv = uv;",

				// point light

				"vec4 lPosition = viewMatrix * vec4( uPointLightPos, 1.0 );",
				"vPointLightVector = normalize( lPosition.xyz - mvPosition.xyz );",

				// displacement mapping

				"#ifdef VERTEX_TEXTURES",

					"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
					"float df = uDisplacementScale * dv.x + uDisplacementBias;",
					"vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
					"gl_Position = projectionMatrix * displacedPosition;",

				"#else",

					"gl_Position = projectionMatrix * mvPosition;",

				"#endif",

			"}"

			].join("\n")

		},
259
		/*
M
Mr.doob 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
		'hatching' : {

			uniforms: {

				"uSampler": { type: "t", value: 2, texture: null },

				"uDirLightPos":	{ type: "v3", value: new THREE.Vector3() },
				"uDirLightColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },

				"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) }

			},

			vertex_shader: [

				"varying vec3 vNormal;",

				"void main() {",

					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
					"vNormal = normalize( normalMatrix * normal );",

				"}"

			].join("\n"),

			fragment_shader: [

				"uniform vec3 uDirLightPos;",
				"uniform vec3 uDirLightColor;",

				"uniform vec3 uAmbientLightColor;",

				"uniform sampler2D uSampler;",

				"varying vec3 vNormal;",

				"void main() {",

					"float directionalLightWeighting = max(dot(normalize(vNormal), uDirLightPos), 0.0);",
					"vec3 lightWeighting = uAmbientLightColor + uDirLightColor * directionalLightWeighting;",

					"gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);",

					"if (length(lightWeighting) < 1.00) {",

						"if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) {",

							"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);",

						"}",

					"}",

					"if (length(lightWeighting) < 0.75) {",

						"if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) {",

							"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);",

						"}",
					"}",

					"if (length(lightWeighting) < 0.50) {",

						"if (mod(gl_FragCoord.x + gl_FragCoord.y - 5.0, 10.0) == 0.0) {",

							"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);",

						"}",
					"}",

					"if (length(lightWeighting) < 0.3465) {",

						"if (mod(gl_FragCoord.x - gl_FragCoord.y - 5.0, 10.0) == 0.0) {",

							"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);",

						"}",
					"}",

				"}"

			].join("\n")

		},
346 347
		*/
		'basic': {
M
Mr.doob 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

			uniforms: {},

			vertex_shader: [

				"void main() {",

					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

				"}"

			].join("\n"),

			fragment_shader: [

				"void main() {",

365
					"gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);",
M
Mr.doob 已提交
366 367 368 369

				"}"

			].join("\n")
370

371
		},
372

373 374 375 376 377 378 379 380 381
		'cube': {

			uniforms: { "tCube": { type: "t", value: 1, texture: null } },

			vertex_shader: [

				"varying vec3 vViewPosition;",

				"void main() {",
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
					"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
					"vViewPosition = cameraPosition - mPosition.xyz;",

					"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

				"}"

			].join("\n"),

			fragment_shader: [

				"uniform samplerCube tCube;",

				"varying vec3 vViewPosition;",

				"void main() {",

					"vec3 wPos = cameraPosition - vViewPosition;",
401
					"gl_FragColor = textureCube( tCube, vec3( - wPos.x, wPos.yz ) );",
402 403 404 405 406

				"}"

			].join("\n")

M
Mr.doob 已提交
407
		}
M
Mr.doob 已提交
408

M
Mr.doob 已提交
409 410 411
	}

};