PhongNode.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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', 'transform' ) : undefined;
  20. material.mergeUniform( THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "ambient" ],
  23. THREE.UniformsLib[ "lights" ]
  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(
  50. transform.code,
  51. "transformed = " + transform.result + ";"
  52. );
  53. }
  54. output.push(
  55. THREE.ShaderChunk[ "morphtarget_vertex" ],
  56. THREE.ShaderChunk[ "skinning_vertex" ],
  57. THREE.ShaderChunk[ "project_vertex" ],
  58. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  59. " vViewPosition = - mvPosition.xyz;",
  60. THREE.ShaderChunk[ "worldpos_vertex" ],
  61. THREE.ShaderChunk[ "lights_phong_vertex" ],
  62. THREE.ShaderChunk[ "shadowmap_vertex" ]
  63. );
  64. code = output.join( "\n" );
  65. }
  66. else {
  67. // verify all nodes to reuse generate codes
  68. this.color.verify( builder );
  69. this.specular.verify( builder );
  70. this.shininess.verify( builder );
  71. if ( this.alpha ) this.alpha.verify( builder );
  72. if ( this.light ) this.light.verify( builder, 'light' );
  73. if ( this.ao ) this.ao.verify( builder );
  74. if ( this.ambient ) this.ambient.verify( builder );
  75. if ( this.shadow ) this.shadow.verify( builder );
  76. if ( this.emissive ) this.emissive.verify( builder );
  77. if ( this.normal ) this.normal.verify( builder );
  78. if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
  79. if ( this.environment ) this.environment.verify( builder );
  80. if ( this.environmentAlpha && this.environment ) this.environmentAlpha.verify( builder );
  81. // build code
  82. var color = this.color.buildCode( builder, 'c' );
  83. var specular = this.specular.buildCode( builder, 'c' );
  84. var shininess = this.shininess.buildCode( builder, 'fv1' );
  85. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  86. var light = this.light ? this.light.buildCode( builder, 'v3', 'light' ) : undefined;
  87. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  88. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  89. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  90. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c' ) : undefined;
  91. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  92. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  93. var environment = this.environment ? this.environment.buildCode( builder, 'c' ) : undefined;
  94. var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.buildCode( builder, 'fv1' ) : undefined;
  95. material.requestAttrib.transparent = alpha != undefined;
  96. material.addFragmentPars( [
  97. THREE.ShaderChunk[ "common" ],
  98. THREE.ShaderChunk[ "fog_pars_fragment" ],
  99. THREE.ShaderChunk[ "bsdfs" ],
  100. THREE.ShaderChunk[ "ambient_pars" ],
  101. THREE.ShaderChunk[ "lights_pars" ],
  102. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  103. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  104. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ]
  105. ].join( "\n" ) );
  106. var output = [
  107. // prevent undeclared normal
  108. THREE.ShaderChunk[ "normal_fragment" ],
  109. // prevent undeclared material
  110. " BlinnPhongMaterial material;",
  111. color.code,
  112. " vec3 diffuseColor = " + color.result + ";",
  113. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  114. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  115. specular.code,
  116. " vec3 specular = " + specular.result + ";",
  117. shininess.code,
  118. " float shininess = max(0.0001," + shininess.result + ");",
  119. " float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
  120. ];
  121. if ( alpha ) {
  122. output.push(
  123. alpha.code,
  124. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  125. );
  126. }
  127. if ( normal ) {
  128. builder.include( 'perturbNormal2Arb' );
  129. output.push( normal.code );
  130. if ( normalScale ) output.push( normalScale.code );
  131. output.push(
  132. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  133. normal.result + ',' +
  134. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  135. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  136. );
  137. }
  138. // optimization for now
  139. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor' ) + ';' );
  140. output.push(
  141. // accumulation
  142. 'material.specularColor = specular;',
  143. 'material.specularShininess = shininess;',
  144. 'material.specularStrength = specularStrength;',
  145. THREE.ShaderChunk[ "lights_template" ]
  146. );
  147. if ( light ) {
  148. output.push(
  149. light.code,
  150. "reflectedLight.directDiffuse = " + light.result + ";"
  151. );
  152. // apply color
  153. output.push(
  154. "reflectedLight.directDiffuse *= diffuseColor;",
  155. "reflectedLight.indirectDiffuse *= diffuseColor;"
  156. );
  157. }
  158. if ( ao ) {
  159. output.push(
  160. ao.code,
  161. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  162. );
  163. }
  164. if ( ambient ) {
  165. output.push(
  166. ambient.code,
  167. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  168. );
  169. }
  170. if ( shadow ) {
  171. output.push(
  172. shadow.code,
  173. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  174. "reflectedLight.directSpecular *= " + shadow.result + ";"
  175. );
  176. }
  177. if ( emissive ) {
  178. output.push(
  179. emissive.code,
  180. "reflectedLight.directDiffuse += " + emissive.result + ";"
  181. );
  182. }
  183. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;" );
  184. if ( environment ) {
  185. output.push( environment.code );
  186. if ( environmentAlpha ) {
  187. output.push(
  188. environmentAlpha.code,
  189. "outgoingLight = mix(" + 'outgoingLight' + "," + environment.result + "," + environmentAlpha.result + ");"
  190. );
  191. }
  192. else {
  193. output.push( "outgoingLight = " + environment.result + ";" );
  194. }
  195. }
  196. output.push(
  197. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  198. THREE.ShaderChunk[ "fog_fragment" ]
  199. );
  200. if ( alpha ) {
  201. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  202. }
  203. else {
  204. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  205. }
  206. code = output.join( "\n" );
  207. }
  208. return code;
  209. };