StandardNode.js 7.6 KB
Newer Older
S
SUNAG 已提交
1 2 3 4
/**
 * @author sunag / http://www.sunag.com.br/
 */

S
SUNAG 已提交
5
THREE.StandardNode = function() {
S
SUNAG 已提交
6

S
SUNAG 已提交
7
	THREE.GLNode.call( this );
S
SUNAG 已提交
8

S
SUNAG 已提交
9 10 11
	this.color = new THREE.ColorNode( 0xEEEEEE );
	this.roughness = new THREE.FloatNode( 0.5 );
	this.metalness = new THREE.FloatNode( 0.5 );
S
SUNAG 已提交
12

S
SUNAG 已提交
13 14
};

S
SUNAG 已提交
15 16
THREE.StandardNode.prototype = Object.create( THREE.GLNode.prototype );
THREE.StandardNode.prototype.constructor = THREE.StandardNode;
S
SUNAG 已提交
17

S
SUNAG 已提交
18
THREE.StandardNode.prototype.build = function( builder ) {
S
SUNAG 已提交
19

S
SUNAG 已提交
20 21
	var material = builder.material;
	var code;
S
SUNAG 已提交
22

S
SUNAG 已提交
23 24
	material.define( 'STANDARD' );
	material.define( 'ALPHATEST', '0.0' );
S
SUNAG 已提交
25

S
SUNAG 已提交
26
	material.requestAttrib.light = true;
S
SUNAG 已提交
27 28 29

	if ( builder.isShader( 'vertex' ) ) {

30
		var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3', 'transform' ) : undefined;
S
SUNAG 已提交
31

S
SUNAG 已提交
32 33 34
		material.mergeUniform( THREE.UniformsUtils.merge( [

			THREE.UniformsLib[ "fog" ],
M
Mr.doob 已提交
35
			THREE.UniformsLib[ "lights" ]
S
SUNAG 已提交
36 37

		] ) );
S
SUNAG 已提交
38

S
SUNAG 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		material.addVertexPars( [
			"varying vec3 vViewPosition;",

			"#ifndef FLAT_SHADED",

			"	varying vec3 vNormal;",

			"#endif",

			THREE.ShaderChunk[ "common" ],
			THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
			THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
			THREE.ShaderChunk[ "skinning_pars_vertex" ],
			THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
			THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]

		].join( "\n" ) );
S
SUNAG 已提交
56

S
SUNAG 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
		var output = [
				THREE.ShaderChunk[ "beginnormal_vertex" ],
				THREE.ShaderChunk[ "morphnormal_vertex" ],
				THREE.ShaderChunk[ "skinbase_vertex" ],
				THREE.ShaderChunk[ "skinnormal_vertex" ],
				THREE.ShaderChunk[ "defaultnormal_vertex" ],

			"#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED

			"	vNormal = normalize( transformedNormal );",

			"#endif",

				THREE.ShaderChunk[ "begin_vertex" ]
		];
S
SUNAG 已提交
72

S
SUNAG 已提交
73
		if ( transform ) {
S
SUNAG 已提交
74

S
SUNAG 已提交
75 76 77 78
			output.push(
				transform.code,
				"transformed = " + transform.result + ";"
			);
S
SUNAG 已提交
79

S
SUNAG 已提交
80
		}
S
SUNAG 已提交
81

S
SUNAG 已提交
82 83 84 85 86 87 88 89 90 91 92 93
		output.push(
				THREE.ShaderChunk[ "morphtarget_vertex" ],
				THREE.ShaderChunk[ "skinning_vertex" ],
				THREE.ShaderChunk[ "project_vertex" ],
				THREE.ShaderChunk[ "logdepthbuf_vertex" ],

			"	vViewPosition = - mvPosition.xyz;",

				THREE.ShaderChunk[ "worldpos_vertex" ],
				THREE.ShaderChunk[ "lights_phong_vertex" ],
				THREE.ShaderChunk[ "shadowmap_vertex" ]
		);
S
SUNAG 已提交
94

S
SUNAG 已提交
95
		code = output.join( "\n" );
S
SUNAG 已提交
96

S
SUNAG 已提交
97 98
	}
	else {
S
SUNAG 已提交
99

100
		// autoblur textures for PBR Material effect
S
SUNAG 已提交
101

102 103 104
		var requires = {
			bias : new THREE.RoughnessToBlinnExponentNode()
		};
S
SUNAG 已提交
105

S
SUNAG 已提交
106
		// verify all nodes to reuse generate codes
S
SUNAG 已提交
107

S
SUNAG 已提交
108 109 110
		this.color.verify( builder );
		this.roughness.verify( builder );
		this.metalness.verify( builder );
S
SUNAG 已提交
111 112 113

		if ( this.alpha ) this.alpha.verify( builder );

S
SUNAG 已提交
114 115
		if ( this.light ) this.light.verify( builder, 'light' );

S
SUNAG 已提交
116 117 118 119 120 121 122 123
		if ( this.ao ) this.ao.verify( builder );
		if ( this.ambient ) this.ambient.verify( builder );
		if ( this.shadow ) this.shadow.verify( builder );
		if ( this.emissive ) this.emissive.verify( builder );

		if ( this.normal ) this.normal.verify( builder );
		if ( this.normalScale && this.normal ) this.normalScale.verify( builder );

124
		if ( this.environment ) this.environment.verify( builder, 'env', requires ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
S
SUNAG 已提交
125

S
SUNAG 已提交
126
		// build code
S
SUNAG 已提交
127

S
SUNAG 已提交
128
		var color = this.color.buildCode( builder, 'c' );
S
SUNAG 已提交
129 130
		var roughness = this.roughness.buildCode( builder, 'fv1' );
		var metalness = this.metalness.buildCode( builder, 'fv1' );
S
SUNAG 已提交
131

S
SUNAG 已提交
132
		var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
S
SUNAG 已提交
133

S
SUNAG 已提交
134 135
		var light = this.light ? this.light.buildCode( builder, 'v3', 'light' ) : undefined;

S
SUNAG 已提交
136
		var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
S
SUNAG 已提交
137 138 139
		var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
		var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
		var emissive = this.emissive ? this.emissive.buildCode( builder, 'c' ) : undefined;
S
SUNAG 已提交
140

S
SUNAG 已提交
141
		var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
S
SUNAG 已提交
142
		var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
S
SUNAG 已提交
143

144
		var environment = this.environment ? this.environment.buildCode( builder, 'c', 'env', requires ) : undefined;
S
SUNAG 已提交
145

S
SUNAG 已提交
146
		material.requestAttrib.transparent = alpha != undefined;
S
SUNAG 已提交
147

S
SUNAG 已提交
148
		material.addFragmentPars( [
S
SUNAG 已提交
149

S
SUNAG 已提交
150
			"varying vec3 vViewPosition;",
S
SUNAG 已提交
151

S
SUNAG 已提交
152 153 154 155 156
			"#ifndef FLAT_SHADED",

			"	varying vec3 vNormal;",

			"#endif",
S
SUNAG 已提交
157

S
SUNAG 已提交
158 159 160 161 162 163 164 165
			THREE.ShaderChunk[ "common" ],
			THREE.ShaderChunk[ "fog_pars_fragment" ],
			THREE.ShaderChunk[ "bsdfs" ],
			THREE.ShaderChunk[ "lights_pars" ],
			THREE.ShaderChunk[ "lights_standard_pars_fragment" ],
			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
			THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
		].join( "\n" ) );
S
SUNAG 已提交
166

S
SUNAG 已提交
167 168 169
		var output = [
				// prevent undeclared normal
				THREE.ShaderChunk[ "normal_fragment" ],
S
SUNAG 已提交
170

S
SUNAG 已提交
171 172
				// prevent undeclared material
			"	StandardMaterial material;",
S
SUNAG 已提交
173
			"	material.diffuseColor = vec3( 1.0 );",
S
SUNAG 已提交
174

S
SUNAG 已提交
175
				color.code,
S
SUNAG 已提交
176
			"	vec3 diffuseColor = " + color.result + ";",
S
SUNAG 已提交
177
			"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
S
SUNAG 已提交
178

S
SUNAG 已提交
179
				THREE.ShaderChunk[ "logdepthbuf_fragment" ],
S
SUNAG 已提交
180

S
SUNAG 已提交
181 182
			roughness.code,
			"	float roughnessFactor = " + roughness.result + ";",
S
SUNAG 已提交
183

S
SUNAG 已提交
184 185
			metalness.code,
			"	float metalnessFactor = " + metalness.result + ";"
S
SUNAG 已提交
186 187 188 189 190
		];

		if ( alpha ) {

			output.push(
S
SUNAG 已提交
191 192 193
				alpha.code,
				'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
			);
S
SUNAG 已提交
194

S
SUNAG 已提交
195
		}
S
SUNAG 已提交
196 197 198

		if ( normal ) {

S
SUNAG 已提交
199
			builder.include( 'perturbNormal2Arb' );
S
SUNAG 已提交
200 201 202 203 204

			output.push( normal.code );

			if ( normalScale ) output.push( normalScale.code );

S
SUNAG 已提交
205 206 207
			output.push(
				'normal = perturbNormal2Arb(-vViewPosition,normal,' +
				normal.result + ',' +
S
SUNAG 已提交
208
				new THREE.UVNode().build( builder, 'v2' ) + ',' +
S
SUNAG 已提交
209
				( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
S
SUNAG 已提交
210 211 212 213
			);

		}

S
SUNAG 已提交
214 215 216 217
		// optimization for now

		output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';' );

S
SUNAG 已提交
218 219 220
		output.push(
			// accumulation
			'material.specularRoughness = clamp( roughnessFactor, 0.001, 1.0 );', // disney's remapping of [ 0, 1 ] roughness to [ 0.001, 1 ]
S
SUNAG 已提交
221
			'material.specularColor = mix( vec3( 0.04 ), diffuseColor, metalnessFactor );',
S
SUNAG 已提交
222

S
SUNAG 已提交
223 224
			THREE.ShaderChunk[ "lights_template" ]
		);
S
SUNAG 已提交
225

S
SUNAG 已提交
226 227 228 229 230 231 232
		if ( light ) {

			output.push(
				light.code,
				"reflectedLight.directDiffuse = " + light.result + ";"
			);

S
SUNAG 已提交
233
			// apply color
S
SUNAG 已提交
234

S
SUNAG 已提交
235 236
			output.push(
				"diffuseColor *= 1.0 - metalnessFactor;",
S
SUNAG 已提交
237

S
SUNAG 已提交
238 239 240 241 242
				"reflectedLight.directDiffuse *= diffuseColor;",
				"reflectedLight.indirectDiffuse *= diffuseColor;"
			);

		}
S
SUNAG 已提交
243

S
SUNAG 已提交
244 245
		if ( ao ) {

S
SUNAG 已提交
246 247 248 249
			output.push(
				ao.code,
				"reflectedLight.indirectDiffuse *= " + ao.result + ";"
			);
S
SUNAG 已提交
250

S
SUNAG 已提交
251
		}
S
SUNAG 已提交
252 253 254

		if ( ambient ) {

S
SUNAG 已提交
255 256 257 258
			output.push(
				ambient.code,
				"reflectedLight.indirectDiffuse += " + ambient.result + ";"
			);
S
SUNAG 已提交
259

S
SUNAG 已提交
260
		}
S
SUNAG 已提交
261 262 263

		if ( shadow ) {

S
SUNAG 已提交
264 265 266 267 268
			output.push(
				shadow.code,
				"reflectedLight.directDiffuse *= " + shadow.result + ";",
				"reflectedLight.directSpecular *= " + shadow.result + ";"
			);
S
SUNAG 已提交
269

S
SUNAG 已提交
270
		}
S
SUNAG 已提交
271 272 273

		if ( emissive ) {

S
SUNAG 已提交
274 275 276 277
			output.push(
				emissive.code,
				"reflectedLight.directDiffuse += " + emissive.result + ";"
			);
S
SUNAG 已提交
278

S
SUNAG 已提交
279
		}
S
SUNAG 已提交
280 281 282

		if ( environment ) {

S
SUNAG 已提交
283 284 285 286
			output.push(
				environment.code,
				"RE_IndirectSpecular(" + environment.result + ", geometry, material, reflectedLight );"
			);
S
SUNAG 已提交
287

S
SUNAG 已提交
288
		}
S
SUNAG 已提交
289 290 291

		output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );

S
SUNAG 已提交
292 293 294 295
		output.push(
			THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
			THREE.ShaderChunk[ "fog_fragment" ]
		);
S
SUNAG 已提交
296 297 298

		if ( alpha ) {

S
SUNAG 已提交
299
			output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
S
SUNAG 已提交
300

S
SUNAG 已提交
301 302
		}
		else {
S
SUNAG 已提交
303

S
SUNAG 已提交
304
			output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
S
SUNAG 已提交
305

S
SUNAG 已提交
306
		}
S
SUNAG 已提交
307

S
SUNAG 已提交
308
		code = output.join( "\n" );
S
SUNAG 已提交
309

S
SUNAG 已提交
310
	}
S
SUNAG 已提交
311

S
SUNAG 已提交
312 313
	return code;

S
SUNAG 已提交
314
};