ShaderTerrain.js 9.0 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/**
 * @author alteredq / http://alteredqualia.com/
 *
 */

THREE.ShaderTerrain = {

	/* -------------------------------------------------------------------------
	//	Dynamic terrain shader
	//		- Blinn-Phong
	//		- height + normal + diffuse1 + diffuse2 + specular + detail maps
	//		- point, directional and hemisphere lights (use with "lights: true" material option)
	//		- shadow maps receiving
	 ------------------------------------------------------------------------- */

	'terrain' : {

		uniforms: THREE.UniformsUtils.merge( [

			THREE.UniformsLib[ "fog" ],
			THREE.UniformsLib[ "lights" ],

			{

M
r78  
Mr.doob 已提交
25 26 27 28
				"enableDiffuse1": { value: 0 },
				"enableDiffuse2": { value: 0 },
				"enableSpecular": { value: 0 },
				"enableReflection": { value: 0 },
M
Mr.doob 已提交
29

M
r78  
Mr.doob 已提交
30 31 32 33 34 35
				"tDiffuse1": { value: null },
				"tDiffuse2": { value: null },
				"tDetail": { value: null },
				"tNormal": { value: null },
				"tSpecular": { value: null },
				"tDisplacement": { value: null },
M
Mr.doob 已提交
36

M
r78  
Mr.doob 已提交
37
				"uNormalScale": { value: 1.0 },
M
Mr.doob 已提交
38

M
r78  
Mr.doob 已提交
39 40
				"uDisplacementBias": { value: 0.0 },
				"uDisplacementScale": { value: 1.0 },
M
Mr.doob 已提交
41

M
r78  
Mr.doob 已提交
42 43 44 45
				"diffuse": { value: new THREE.Color( 0xeeeeee ) },
				"specular": { value: new THREE.Color( 0x111111 ) },
				"shininess": { value: 30 },
				"opacity": { value: 1 },
M
Mr.doob 已提交
46

M
r78  
Mr.doob 已提交
47 48
				"uRepeatBase": { value: new THREE.Vector2( 1, 1 ) },
				"uRepeatOverlay": { value: new THREE.Vector2( 1, 1 ) },
M
Mr.doob 已提交
49

M
r78  
Mr.doob 已提交
50
				"uOffset": { value: new THREE.Vector2( 0, 0 ) }
M
Mr.doob 已提交
51 52 53 54 55 56 57

			}

		] ),

		fragmentShader: [

M
r65  
Mr.doob 已提交
58 59 60 61
			"uniform vec3 diffuse;",
			"uniform vec3 specular;",
			"uniform float shininess;",
			"uniform float opacity;",
M
Mr.doob 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

			"uniform bool enableDiffuse1;",
			"uniform bool enableDiffuse2;",
			"uniform bool enableSpecular;",

			"uniform sampler2D tDiffuse1;",
			"uniform sampler2D tDiffuse2;",
			"uniform sampler2D tDetail;",
			"uniform sampler2D tNormal;",
			"uniform sampler2D tSpecular;",
			"uniform sampler2D tDisplacement;",

			"uniform float uNormalScale;",

			"uniform vec2 uRepeatOverlay;",
			"uniform vec2 uRepeatBase;",

			"uniform vec2 uOffset;",

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

			"varying vec3 vViewPosition;",

M
r71  
Mr.doob 已提交
88
			THREE.ShaderChunk[ "common" ],
M
r74  
Mr.doob 已提交
89 90
			THREE.ShaderChunk[ "bsdfs" ],
			THREE.ShaderChunk[ "lights_pars" ],
M
Mr.doob 已提交
91 92 93
			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
			THREE.ShaderChunk[ "fog_pars_fragment" ],

M
r76  
Mr.doob 已提交
94 95 96 97 98 99 100
			"float calcLightAttenuation( float lightDistance, float cutoffDistance, float decayExponent ) {",
 				"if ( decayExponent > 0.0 ) {",
 					"return pow( saturate( - lightDistance / cutoffDistance + 1.0 ), decayExponent );",
 				"}",
 				"return 1.0;",
 			"}",

M
Mr.doob 已提交
101 102
			"void main() {",

M
r71  
Mr.doob 已提交
103 104
				"vec3 outgoingLight = vec3( 0.0 );",	// outgoing light does not have an alpha, the surface does
				"vec4 diffuseColor = vec4( diffuse, opacity );",
M
Mr.doob 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

				"vec3 specularTex = vec3( 1.0 );",

				"vec2 uvOverlay = uRepeatOverlay * vUv + uOffset;",
				"vec2 uvBase = uRepeatBase * vUv;",

				"vec3 normalTex = texture2D( tDetail, uvOverlay ).xyz * 2.0 - 1.0;",
				"normalTex.xy *= uNormalScale;",
				"normalTex = normalize( normalTex );",

				"if( enableDiffuse1 && enableDiffuse2 ) {",

					"vec4 colDiffuse1 = texture2D( tDiffuse1, uvOverlay );",
					"vec4 colDiffuse2 = texture2D( tDiffuse2, uvOverlay );",

M
r76  
Mr.doob 已提交
120 121
					"colDiffuse1 = GammaToLinear( colDiffuse1, float( GAMMA_FACTOR ) );",
					"colDiffuse2 = GammaToLinear( colDiffuse2, float( GAMMA_FACTOR ) );",
M
Mr.doob 已提交
122

M
r71  
Mr.doob 已提交
123
					"diffuseColor *= mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
M
Mr.doob 已提交
124 125 126

				" } else if( enableDiffuse1 ) {",

M
r71  
Mr.doob 已提交
127
					"diffuseColor *= texture2D( tDiffuse1, uvOverlay );",
M
Mr.doob 已提交
128 129 130

				"} else if( enableDiffuse2 ) {",

M
r71  
Mr.doob 已提交
131
					"diffuseColor *= texture2D( tDiffuse2, uvOverlay );",
M
Mr.doob 已提交
132 133 134 135 136 137 138 139 140 141 142 143

				"}",

				"if( enableSpecular )",
					"specularTex = texture2D( tSpecular, uvOverlay ).xyz;",

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

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

M
r71  
Mr.doob 已提交
144 145 146
				"vec3 totalDiffuseLight = vec3( 0.0 );",
				"vec3 totalSpecularLight = vec3( 0.0 );",

M
Mr.doob 已提交
147 148
				// point lights

M
r74  
Mr.doob 已提交
149
				"#if NUM_POINT_LIGHTS > 0",
M
Mr.doob 已提交
150

M
r74  
Mr.doob 已提交
151
					"for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
M
Mr.doob 已提交
152

M
r74  
Mr.doob 已提交
153
						"vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",
M
Mr.doob 已提交
154

M
r74  
Mr.doob 已提交
155
						"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
M
Mr.doob 已提交
156 157 158 159 160 161 162 163

						"lVector = normalize( lVector );",

						"vec3 pointHalfVector = normalize( lVector + viewPosition );",

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

M
r65  
Mr.doob 已提交
164
						"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
M
Mr.doob 已提交
165

M
r74  
Mr.doob 已提交
166 167
						"totalDiffuseLight += attenuation * pointLights[ i ].color * pointDiffuseWeight;",
						"totalSpecularLight += attenuation * pointLights[ i ].color * specular * pointSpecularWeight * pointDiffuseWeight;",
M
Mr.doob 已提交
168 169 170 171 172 173 174

					"}",

				"#endif",

				// directional lights

M
r74  
Mr.doob 已提交
175
				"#if NUM_DIR_LIGHTS > 0",
M
Mr.doob 已提交
176 177 178 179

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

M
r74  
Mr.doob 已提交
180
					"for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
M
Mr.doob 已提交
181

M
r74  
Mr.doob 已提交
182
						"vec3 dirVector = directionalLights[ i ].direction;",
M
Mr.doob 已提交
183 184 185 186 187
						"vec3 dirHalfVector = normalize( dirVector + viewPosition );",

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

M
r65  
Mr.doob 已提交
188
						"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
M
Mr.doob 已提交
189

M
r74  
Mr.doob 已提交
190 191
						"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
						"totalSpecularLight += directionalLights[ i ].color * specular * dirSpecularWeight * dirDiffuseWeight;",
M
Mr.doob 已提交
192 193 194 195 196 197 198

					"}",

				"#endif",

				// hemisphere lights

M
r74  
Mr.doob 已提交
199
				"#if NUM_HEMI_LIGHTS > 0",
M
Mr.doob 已提交
200 201

					"vec3 hemiDiffuse  = vec3( 0.0 );",
M
r71  
Mr.doob 已提交
202
					"vec3 hemiSpecular = vec3( 0.0 );",
M
Mr.doob 已提交
203

M
r74  
Mr.doob 已提交
204
					"for( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
M
Mr.doob 已提交
205

M
r72  
Mr.doob 已提交
206
						"vec3 lVector = hemisphereLightDirection[ i ];",
M
Mr.doob 已提交
207 208 209 210 211 212

						// diffuse

						"float dotProduct = dot( normal, lVector );",
						"float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",

M
r74  
Mr.doob 已提交
213
						"totalDiffuseLight += mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight );",
M
Mr.doob 已提交
214 215 216 217 218 219 220

						// specular (sky light)

						"float hemiSpecularWeight = 0.0;",

						"vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
						"float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
M
r65  
Mr.doob 已提交
221
						"hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
M
Mr.doob 已提交
222 223 224 225 226 227 228

						// specular (ground light)

						"vec3 lVectorGround = -lVector;",

						"vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
						"float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
M
r65  
Mr.doob 已提交
229
						"hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
M
Mr.doob 已提交
230

M
r74  
Mr.doob 已提交
231
						"totalSpecularLight += specular * mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
M
Mr.doob 已提交
232 233 234 235 236

					"}",

				"#endif",

M
r71  
Mr.doob 已提交
237
				"outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor + totalSpecularLight );",
M
Mr.doob 已提交
238 239 240

				THREE.ShaderChunk[ "fog_fragment" ],

M
r71  
Mr.doob 已提交
241 242
				"gl_FragColor = vec4( outgoingLight, diffuseColor.a );",	// TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects

M
Mr.doob 已提交
243 244
			"}"

M
r72  
Mr.doob 已提交
245
		].join( "\n" ),
M
Mr.doob 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 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

		vertexShader: [

			"attribute vec4 tangent;",

			"uniform vec2 uRepeatBase;",

			"uniform sampler2D tNormal;",

			"#ifdef VERTEX_TEXTURES",

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

			"#endif",

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

			"varying vec3 vViewPosition;",

			THREE.ShaderChunk[ "shadowmap_pars_vertex" ],

			"void main() {",

				"vNormal = normalize( normalMatrix * normal );",

				// tangent and binormal vectors

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

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

				// texture coordinates

				"vUv = uv;",

				"vec2 uvBase = uv * uRepeatBase;",

				// displacement mapping

				"#ifdef VERTEX_TEXTURES",

					"vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",
					"float df = uDisplacementScale * dv.x + uDisplacementBias;",
					"vec3 displacedPosition = normal * df + position;",

					"vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
					"vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",

				"#else",

					"vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",

				"#endif",

				"gl_Position = projectionMatrix * mvPosition;",

				"vViewPosition = -mvPosition.xyz;",

				"vec3 normalTex = texture2D( tNormal, uvBase ).xyz * 2.0 - 1.0;",
				"vNormal = normalMatrix * normalTex;",

				THREE.ShaderChunk[ "shadowmap_vertex" ],

			"}"

M
r72  
Mr.doob 已提交
318
		].join( "\n" )
M
Mr.doob 已提交
319 320 321 322

	}

};