PhongNode.js 7.7 KB

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