PhongNode.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.requestAttribs.light = true;
  18. if ( builder.isShader( 'vertex' ) ) {
  19. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache : 'transform' } ) : undefined;
  20. material.mergeUniform( THREE.UniformsUtils.merge( [
  21. THREE.UniformsLib[ "fog" ],
  22. THREE.UniformsLib[ "lights" ]
  23. ] ) );
  24. material.addVertexPars( [
  25. "varying vec3 vViewPosition;",
  26. "#ifndef FLAT_SHADED",
  27. " varying vec3 vNormal;",
  28. "#endif",
  29. THREE.ShaderChunk[ "common" ],
  30. THREE.ShaderChunk[ "fog_parse_vertex" ],
  31. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  32. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  33. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  34. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ]
  35. ].join( "\n" ) );
  36. var output = [
  37. THREE.ShaderChunk[ "beginnormal_vertex" ],
  38. THREE.ShaderChunk[ "morphnormal_vertex" ],
  39. THREE.ShaderChunk[ "skinbase_vertex" ],
  40. THREE.ShaderChunk[ "skinnormal_vertex" ],
  41. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  42. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  43. " vNormal = normalize( transformedNormal );",
  44. "#endif",
  45. THREE.ShaderChunk[ "begin_vertex" ],
  46. THREE.ShaderChunk[ "fog_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[ "shadowmap_vertex" ]
  62. );
  63. code = output.join( "\n" );
  64. } else {
  65. // parse all nodes to reuse generate codes
  66. this.color.parse( builder, { slot : 'color' } );
  67. this.specular.parse( builder );
  68. this.shininess.parse( builder );
  69. if ( this.alpha ) this.alpha.parse( builder );
  70. if ( this.normal ) this.normal.parse( builder );
  71. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  72. if ( this.light ) this.light.parse( builder, { cache : 'light' } );
  73. if ( this.ao ) this.ao.parse( builder );
  74. if ( this.ambient ) this.ambient.parse( builder );
  75. if ( this.shadow ) this.shadow.parse( builder );
  76. if ( this.emissive ) this.emissive.parse( builder, { slot : 'emissive' } );
  77. if ( this.environment ) this.environment.parse( builder, { slot : 'environment' } );
  78. if ( this.environmentAlpha && this.environment ) this.environmentAlpha.parse( builder );
  79. // build code
  80. var color = this.color.buildCode( builder, 'c', { slot : 'color' } );
  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 normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  85. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  86. var light = this.light ? this.light.buildCode( builder, 'v3', { cache : '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', { slot : 'emissive' } ) : undefined;
  91. var environment = this.environment ? this.environment.buildCode( builder, 'c', { slot : 'environment' } ) : undefined;
  92. var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.buildCode( builder, 'fv1' ) : undefined;
  93. material.requestAttribs.transparent = alpha != undefined;
  94. material.addFragmentPars( [
  95. THREE.ShaderChunk[ "common" ],
  96. THREE.ShaderChunk[ "fog_pars_fragment" ],
  97. THREE.ShaderChunk[ "bsdfs" ],
  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_flip" ],
  106. THREE.ShaderChunk[ "normal_fragment" ],
  107. // prevent undeclared material
  108. " BlinnPhongMaterial material;",
  109. color.code,
  110. " vec3 diffuseColor = " + color.result + ";",
  111. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  112. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  113. specular.code,
  114. " vec3 specular = " + specular.result + ";",
  115. shininess.code,
  116. " float shininess = max(0.0001," + shininess.result + ");",
  117. " float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
  118. ];
  119. if ( alpha ) {
  120. output.push(
  121. alpha.code,
  122. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;'
  123. );
  124. }
  125. if ( normal ) {
  126. builder.include( 'perturbNormal2Arb' );
  127. output.push( normal.code );
  128. if ( normalScale ) output.push( normalScale.code );
  129. output.push(
  130. 'normal = perturbNormal2Arb(-vViewPosition,normal,' +
  131. normal.result + ',' +
  132. new THREE.UVNode().build( builder, 'v2' ) + ',' +
  133. ( normalScale ? normalScale.result : 'vec2( 1.0 )' ) + ');'
  134. );
  135. }
  136. // optimization for now
  137. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor' ) + ';' );
  138. output.push(
  139. // accumulation
  140. 'material.specularColor = specular;',
  141. 'material.specularShininess = shininess;',
  142. 'material.specularStrength = specularStrength;',
  143. THREE.ShaderChunk[ "lights_template" ]
  144. );
  145. if ( light ) {
  146. output.push(
  147. light.code,
  148. "reflectedLight.directDiffuse = " + light.result + ";"
  149. );
  150. // apply color
  151. output.push(
  152. "reflectedLight.directDiffuse *= diffuseColor;",
  153. "reflectedLight.indirectDiffuse *= diffuseColor;"
  154. );
  155. }
  156. if ( ao ) {
  157. output.push(
  158. ao.code,
  159. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  160. );
  161. }
  162. if ( ambient ) {
  163. output.push(
  164. ambient.code,
  165. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  166. );
  167. }
  168. if ( shadow ) {
  169. output.push(
  170. shadow.code,
  171. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  172. "reflectedLight.directSpecular *= " + shadow.result + ";"
  173. );
  174. }
  175. if ( emissive ) {
  176. output.push(
  177. emissive.code,
  178. "reflectedLight.directDiffuse += " + emissive.result + ";"
  179. );
  180. }
  181. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;" );
  182. if ( environment ) {
  183. output.push( environment.code );
  184. if ( environmentAlpha ) {
  185. output.push(
  186. environmentAlpha.code,
  187. "outgoingLight = mix( outgoingLight, " + environment.result + ", " + environmentAlpha.result + " );"
  188. );
  189. } else {
  190. output.push( "outgoingLight = " + environment.result + ";" );
  191. }
  192. }
  193. if ( alpha ) {
  194. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  195. } else {
  196. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  197. }
  198. output.push(
  199. THREE.ShaderChunk[ "premultiplied_alpha_fragment" ],
  200. THREE.ShaderChunk[ "tonemapping_fragment" ],
  201. THREE.ShaderChunk[ "encodings_fragment" ],
  202. THREE.ShaderChunk[ "fog_fragment" ]
  203. );
  204. code = output.join( "\n" );
  205. }
  206. return code;
  207. };