StandardNode.js 7.5 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
		material.addVertexPars( [
			"varying vec3 vViewPosition;",

			"#ifndef FLAT_SHADED",

			"	varying vec3 vNormal;",

			"#endif",

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

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

S
SUNAG 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		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 已提交
71

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

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

S
SUNAG 已提交
79
		}
S
SUNAG 已提交
80

S
SUNAG 已提交
81 82 83 84 85 86 87 88 89 90 91
		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[ "shadowmap_vertex" ]
		);
S
SUNAG 已提交
92

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

S
SUNAG 已提交
95 96
	}
	else {
S
SUNAG 已提交
97

98
		// autoblur textures for PBR Material effect
S
SUNAG 已提交
99

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

S
SUNAG 已提交
104
		// verify all nodes to reuse generate codes
S
SUNAG 已提交
105

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

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

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

S
SUNAG 已提交
114 115 116 117 118 119 120 121
		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 );

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

S
SUNAG 已提交
124
		// build code
S
SUNAG 已提交
125

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

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

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

S
SUNAG 已提交
134
		var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
S
SUNAG 已提交
135 136 137
		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 已提交
138

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

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

S
SUNAG 已提交
144
		material.requestAttrib.transparent = alpha != undefined;
S
SUNAG 已提交
145

S
SUNAG 已提交
146
		material.addFragmentPars( [
S
SUNAG 已提交
147

S
SUNAG 已提交
148
			"varying vec3 vViewPosition;",
S
SUNAG 已提交
149

S
SUNAG 已提交
150 151 152 153 154
			"#ifndef FLAT_SHADED",

			"	varying vec3 vNormal;",

			"#endif",
S
SUNAG 已提交
155

S
SUNAG 已提交
156 157 158 159 160 161 162 163
			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 已提交
164

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

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

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

S
SUNAG 已提交
177
				THREE.ShaderChunk[ "logdepthbuf_fragment" ],
S
SUNAG 已提交
178

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

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

		if ( alpha ) {

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

S
SUNAG 已提交
193
		}
S
SUNAG 已提交
194 195 196

		if ( normal ) {

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

			output.push( normal.code );

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

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

		}

S
SUNAG 已提交
212 213 214 215
		// optimization for now

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

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

S
SUNAG 已提交
221 222
			THREE.ShaderChunk[ "lights_template" ]
		);
S
SUNAG 已提交
223

S
SUNAG 已提交
224 225 226 227 228 229 230
		if ( light ) {

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

S
SUNAG 已提交
231
			// apply color
S
SUNAG 已提交
232

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

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

		}
S
SUNAG 已提交
241

S
SUNAG 已提交
242 243
		if ( ao ) {

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

S
SUNAG 已提交
249
		}
S
SUNAG 已提交
250 251 252

		if ( ambient ) {

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

S
SUNAG 已提交
258
		}
S
SUNAG 已提交
259 260 261

		if ( shadow ) {

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

S
SUNAG 已提交
268
		}
S
SUNAG 已提交
269 270 271

		if ( emissive ) {

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

S
SUNAG 已提交
277
		}
S
SUNAG 已提交
278 279 280

		if ( environment ) {

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

S
SUNAG 已提交
286
		}
S
SUNAG 已提交
287 288 289

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

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

		if ( alpha ) {

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

S
SUNAG 已提交
299 300
		}
		else {
S
SUNAG 已提交
301

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

S
SUNAG 已提交
304
		}
S
SUNAG 已提交
305

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

S
SUNAG 已提交
308
	}
S
SUNAG 已提交
309

S
SUNAG 已提交
310 311
	return code;

S
SUNAG 已提交
312
};