NodePhong.js 7.0 KB

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