123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /**
- * @author sunag / http://www.sunag.com.br/
- */
- THREE.PhongNode = function() {
- THREE.GLNode.call( this );
- this.color = new THREE.ColorNode( 0xEEEEEE );
- this.specular = new THREE.ColorNode( 0x111111 );
- this.shininess = new THREE.FloatNode( 30 );
- };
- THREE.PhongNode.prototype = Object.create( THREE.GLNode.prototype );
- THREE.PhongNode.prototype.constructor = THREE.PhongNode;
- THREE.PhongNode.prototype.build = function( builder ) {
- var material = builder.material;
- var code;
- material.define( 'PHONG' );
- material.define( 'ALPHATEST', '0.0' );
- material.requestAttribs.light = true;
- if ( builder.isShader( 'vertex' ) ) {
- var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache : 'transform' } ) : undefined;
- material.mergeUniform( THREE.UniformsUtils.merge( [
- THREE.UniformsLib[ "fog" ],
- THREE.UniformsLib[ "lights" ]
- ] ) );
- material.addVertexPars( [
- "varying vec3 vViewPosition;",
- "#ifndef FLAT_SHADED",
- " varying vec3 vNormal;",
- "#endif",
- THREE.ShaderChunk[ "common" ],
- THREE.ShaderChunk[ "fog_parse_vertex" ],
- THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
- THREE.ShaderChunk[ "skinning_pars_vertex" ],
- THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
- THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
- ].join( "\n" ) );
- 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" ],
- THREE.ShaderChunk[ "fog_vertex" ]
- ];
- if ( transform ) {
- output.push(
- transform.code,
- "transformed = " + transform.result + ";"
- );
- }
- 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" ]
- );
- code = output.join( "\n" );
- } else {
- // parse all nodes to reuse generate codes
- this.color.parse( builder, { slot : 'color' } );
- this.specular.parse( builder );
- this.shininess.parse( builder );
- if ( this.alpha ) this.alpha.parse( builder );
-
- if ( this.normal ) this.normal.parse( builder );
- if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
-
- if ( this.light ) this.light.parse( builder, { cache : 'light' } );
- if ( this.ao ) this.ao.parse( builder );
- if ( this.ambient ) this.ambient.parse( builder );
- if ( this.shadow ) this.shadow.parse( builder );
- if ( this.emissive ) this.emissive.parse( builder, { slot : 'emissive' } );
- if ( this.environment ) this.environment.parse( builder, { slot : 'environment' } );
- if ( this.environmentAlpha && this.environment ) this.environmentAlpha.parse( builder );
- // build code
- var color = this.color.buildCode( builder, 'c', { slot : 'color' } );
- var specular = this.specular.buildCode( builder, 'c' );
- var shininess = this.shininess.buildCode( builder, 'fv1' );
- var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
-
- var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
- var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
-
- var light = this.light ? this.light.buildCode( builder, 'v3', { cache : 'light' } ) : undefined;
- var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
- 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', { slot : 'emissive' } ) : undefined;
- var environment = this.environment ? this.environment.buildCode( builder, 'c', { slot : 'environment' } ) : undefined;
- var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.buildCode( builder, 'fv1' ) : undefined;
- material.requestAttribs.transparent = alpha != undefined;
- material.addFragmentPars( [
- THREE.ShaderChunk[ "common" ],
- THREE.ShaderChunk[ "fog_pars_fragment" ],
- THREE.ShaderChunk[ "bsdfs" ],
- THREE.ShaderChunk[ "lights_pars" ],
- THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
- THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
- THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
- ].join( "\n" ) );
- var output = [
- // prevent undeclared normal
- THREE.ShaderChunk[ "normal_flip" ],
- THREE.ShaderChunk[ "normal_fragment" ],
- // prevent undeclared material
- " BlinnPhongMaterial material;",
- color.code,
- " vec3 diffuseColor = " + color.result + ";",
- " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
- THREE.ShaderChunk[ "logdepthbuf_fragment" ],
- specular.code,
- " vec3 specular = " + specular.result + ";",
- shininess.code,
- " float shininess = max(0.0001," + shininess.result + ");",
- " float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
- ];
- if ( alpha ) {
- output.push(
- alpha.code,
- 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
- );
- }
- if ( normal ) {
- builder.include( 'perturbNormal2Arb' );
- output.push( normal.code );
- if ( normalScale ) output.push( normalScale.code );
- output.push(
- 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
- normal.result + ',' +
- new THREE.UVNode().build( builder, 'v2' ) + ',' +
- ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
- );
- }
- // optimization for now
- output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor' ) + ';' );
- output.push(
- // accumulation
- 'material.specularColor = specular;',
- 'material.specularShininess = shininess;',
- 'material.specularStrength = specularStrength;',
- THREE.ShaderChunk[ "lights_template" ]
- );
- if ( light ) {
- output.push(
- light.code,
- "reflectedLight.directDiffuse = " + light.result + ";"
- );
- // apply color
- output.push(
- "reflectedLight.directDiffuse *= diffuseColor;",
- "reflectedLight.indirectDiffuse *= diffuseColor;"
- );
- }
- if ( ao ) {
- output.push(
- ao.code,
- "reflectedLight.indirectDiffuse *= " + ao.result + ";"
- );
- }
- if ( ambient ) {
- output.push(
- ambient.code,
- "reflectedLight.indirectDiffuse += " + ambient.result + ";"
- );
- }
- if ( shadow ) {
- output.push(
- shadow.code,
- "reflectedLight.directDiffuse *= " + shadow.result + ";",
- "reflectedLight.directSpecular *= " + shadow.result + ";"
- );
- }
- if ( emissive ) {
- output.push(
- emissive.code,
- "reflectedLight.directDiffuse += " + emissive.result + ";"
- );
- }
- output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;" );
- if ( environment ) {
- output.push( environment.code );
- if ( environmentAlpha ) {
- output.push(
- environmentAlpha.code,
- "outgoingLight = mix( outgoingLight, " + environment.result + ", " + environmentAlpha.result + " );"
- );
- } else {
- output.push( "outgoingLight = " + environment.result + ";" );
- }
- }
- if ( alpha ) {
- output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
- } else {
- output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
- }
- output.push(
- THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
- THREE.ShaderChunk[ "tonemapping_fragment" ],
- THREE.ShaderChunk[ "encodings_fragment" ],
- THREE.ShaderChunk[ "fog_fragment" ]
- );
- code = output.join( "\n" );
- }
- return code;
- };
|