PhongNode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.PhongNode = function() {
  5. THREE.GLNode.call( this );
  6. this.color = new THREE.ColorNode( 0xEEEEEE );
  7. this.specular = new THREE.ColorNode( 0x111111 );
  8. this.shininess = new THREE.FloatNode( 30 );
  9. };
  10. THREE.PhongNode.prototype = Object.create( THREE.GLNode.prototype );
  11. THREE.PhongNode.prototype.constructor = THREE.PhongNode;
  12. THREE.PhongNode.prototype.build = function( builder ) {
  13. var material = builder.material;
  14. var code;
  15. material.define( 'PHONG' );
  16. material.define( 'ALPHATEST', '0.0' );
  17. material.requestAttrib.light = true;
  18. if ( builder.isShader( 'vertex' ) ) {
  19. var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3' ) : undefined;
  20. material.mergeUniform( THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "ambient" ],
  23. THREE.UniformsLib[ "lights" ],
  24. THREE.UniformsLib[ "shadowmap" ]
  25. ] ) );
  26. material.addVertexPars( [
  27. "varying vec3 vViewPosition;",
  28. "#ifndef FLAT_SHADED",
  29. " varying vec3 vNormal;",
  30. "#endif",
  31. THREE.ShaderChunk[ "common" ],
  32. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  33. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  34. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  35. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  36. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
  37. ].join( "\n" ) );
  38. var output = [
  39. THREE.ShaderChunk[ "beginnormal_vertex" ],
  40. THREE.ShaderChunk[ "morphnormal_vertex" ],
  41. THREE.ShaderChunk[ "skinbase_vertex" ],
  42. THREE.ShaderChunk[ "skinnormal_vertex" ],
  43. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  44. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  45. " vNormal = normalize( transformedNormal );",
  46. "#endif",
  47. THREE.ShaderChunk[ "begin_vertex" ]
  48. ];
  49. if ( transform ) {
  50. output.push( transform.code );
  51. output.push( "transformed = " + transform.result + ";" );
  52. }
  53. output.push(
  54. THREE.ShaderChunk[ "morphtarget_vertex" ],
  55. THREE.ShaderChunk[ "skinning_vertex" ],
  56. THREE.ShaderChunk[ "project_vertex" ],
  57. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  58. " vViewPosition = - mvPosition.xyz;",
  59. THREE.ShaderChunk[ "worldpos_vertex" ],
  60. THREE.ShaderChunk[ "lights_phong_vertex" ],
  61. THREE.ShaderChunk[ "shadowmap_vertex" ]
  62. );
  63. code = output.join( "\n" );
  64. }
  65. else {
  66. // verify all nodes to reuse generate codes
  67. this.color.verify( builder );
  68. this.specular.verify( builder );
  69. this.shininess.verify( builder );
  70. if ( this.alpha ) this.alpha.verify( builder );
  71. if ( this.ao ) this.ao.verify( builder );
  72. if ( this.ambient ) this.ambient.verify( builder );
  73. if ( this.shadow ) this.shadow.verify( builder );
  74. if ( this.emissive ) this.emissive.verify( builder );
  75. if ( this.normal ) this.normal.verify( builder );
  76. if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
  77. if ( this.environment ) this.environment.verify( builder );
  78. if ( this.reflectivity && this.environment ) this.reflectivity.verify( builder );
  79. // build code
  80. var color = this.color.buildCode( builder, 'v4' );
  81. var specular = this.specular.buildCode( builder, 'c' );
  82. var shininess = this.shininess.buildCode( builder, 'fv1' );
  83. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  84. var ao = this.ao ? this.ao.buildCode( builder, 'c' ) : undefined;
  85. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  86. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  87. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c' ) : undefined;
  88. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  89. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'fv1' ) : undefined;
  90. var environment = this.environment ? this.environment.buildCode( builder.setCache( 'env' ), 'c' ) : undefined;
  91. var reflectivity = this.reflectivity && this.environment ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
  92. material.requestAttrib.transparent = alpha != undefined;
  93. material.addFragmentPars( [
  94. THREE.ShaderChunk[ "common" ],
  95. THREE.ShaderChunk[ "fog_pars_fragment" ],
  96. THREE.ShaderChunk[ "bsdfs" ],
  97. THREE.ShaderChunk[ "ambient_pars" ],
  98. THREE.ShaderChunk[ "lights_pars" ],
  99. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  100. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  101. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
  102. ].join( "\n" ) );
  103. var output = [
  104. // prevent undeclared normal
  105. THREE.ShaderChunk[ "normal_fragment" ],
  106. color.code,
  107. " vec4 diffuseColor = " + color.result + ";",
  108. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  109. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  110. specular.code,
  111. " vec3 specular = " + specular.result + ";",
  112. shininess.code,
  113. " float shininess = max(0.0001," + shininess.result + ");",
  114. " float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
  115. ];
  116. if ( alpha ) {
  117. output.push(
  118. alpha.code,
  119. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  120. );
  121. }
  122. if ( normal ) {
  123. builder.include( 'perturbNormal2Arb' );
  124. output.push( normal.code );
  125. if ( normalScale ) output.push( normalScale.code );
  126. output.push(
  127. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  128. normal.result + ',' +
  129. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  130. ( normalScale ? normalScale.result : '1.0' ) + ');'
  131. );
  132. }
  133. output.push(
  134. THREE.ShaderChunk[ "shadowmap_fragment" ],
  135. // accumulation
  136. THREE.ShaderChunk[ "lights_phong_fragment" ],
  137. THREE.ShaderChunk[ "lights_template" ]
  138. );
  139. if ( ao ) {
  140. output.push( ao.code );
  141. output.push( "reflectedLight.indirectDiffuse *= " + ao.result + ";" );
  142. }
  143. if ( ambient ) {
  144. output.push( ambient.code );
  145. output.push( "reflectedLight.indirectDiffuse += " + ambient.result + ";" );
  146. }
  147. if ( shadow ) {
  148. output.push( shadow.code );
  149. output.push( "reflectedLight.directDiffuse *= " + shadow.result + ";" );
  150. output.push( "reflectedLight.directSpecular *= " + shadow.result + ";" );
  151. }
  152. if ( emissive ) {
  153. output.push( emissive.code );
  154. output.push( "reflectedLight.directDiffuse += " + emissive.result + ";" );
  155. }
  156. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  157. if ( environment ) {
  158. output.push( environment.code );
  159. if ( reflectivity ) {
  160. output.push( reflectivity.code );
  161. output.push( "outgoingLight = mix(" + 'outgoingLight' + "," + environment.result + "," + reflectivity.result + ");" );
  162. }
  163. else {
  164. output.push( "outgoingLight = " + environment.result + ";" );
  165. }
  166. }
  167. output.push(
  168. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  169. THREE.ShaderChunk[ "fog_fragment" ]
  170. );
  171. if ( alpha ) {
  172. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  173. }
  174. else {
  175. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  176. }
  177. code = output.join( "\n" );
  178. }
  179. return code;
  180. };