2
0

PhongNode.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.nodeType = "Phong";
  13. THREE.PhongNode.prototype.build = function ( builder ) {
  14. var material = builder.material;
  15. var code;
  16. material.define( 'PHONG' );
  17. material.define( 'ALPHATEST', '0.0' );
  18. material.requires.lights = true;
  19. if ( builder.isShader( 'vertex' ) ) {
  20. var transform = this.transform ? this.transform.parseAndBuildCode( builder, 'v3', { cache: 'transform' } ) : undefined;
  21. material.mergeUniform( THREE.UniformsUtils.merge( [
  22. THREE.UniformsLib[ "fog" ],
  23. THREE.UniformsLib[ "lights" ]
  24. ] ) );
  25. material.addVertexPars( [
  26. "varying vec3 vViewPosition;",
  27. "#ifndef FLAT_SHADED",
  28. " varying vec3 vNormal;",
  29. "#endif",
  30. "#include <common>",
  31. "#include <fog_pars_vertex>",
  32. "#include <morphtarget_pars_vertex>",
  33. "#include <skinning_pars_vertex>",
  34. "#include <shadowmap_pars_vertex>",
  35. "#include <logdepthbuf_pars_vertex>"
  36. ].join( "\n" ) );
  37. var output = [
  38. "#include <beginnormal_vertex>",
  39. "#include <morphnormal_vertex>",
  40. "#include <skinbase_vertex>",
  41. "#include <skinnormal_vertex>",
  42. "#include <defaultnormal_vertex>",
  43. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  44. " vNormal = normalize( transformedNormal );",
  45. "#endif",
  46. "#include <begin_vertex>"
  47. ];
  48. if ( transform ) {
  49. output.push(
  50. transform.code,
  51. "transformed = " + transform.result + ";"
  52. );
  53. }
  54. output.push(
  55. " #include <morphtarget_vertex>",
  56. " #include <skinning_vertex>",
  57. " #include <project_vertex>",
  58. " #include <fog_vertex>",
  59. " #include <logdepthbuf_vertex>",
  60. " vViewPosition = - mvPosition.xyz;",
  61. " #include <worldpos_vertex>",
  62. " #include <shadowmap_vertex>"
  63. );
  64. code = output.join( "\n" );
  65. } else {
  66. // parse all nodes to reuse generate codes
  67. this.color.parse( builder, { slot: 'color' } );
  68. this.specular.parse( builder );
  69. this.shininess.parse( builder );
  70. if ( this.alpha ) this.alpha.parse( builder );
  71. if ( this.normal ) this.normal.parse( builder );
  72. if ( this.normalScale && this.normal ) this.normalScale.parse( builder );
  73. if ( this.light ) this.light.parse( builder, { cache: 'light' } );
  74. if ( this.ao ) this.ao.parse( builder );
  75. if ( this.ambient ) this.ambient.parse( builder );
  76. if ( this.shadow ) this.shadow.parse( builder );
  77. if ( this.emissive ) this.emissive.parse( builder, { slot: 'emissive' } );
  78. if ( this.environment ) this.environment.parse( builder, { slot: 'environment' } );
  79. if ( this.environmentAlpha && this.environment ) this.environmentAlpha.parse( builder );
  80. // build code
  81. var color = this.color.buildCode( builder, 'c', { slot: 'color' } );
  82. var specular = this.specular.buildCode( builder, 'c' );
  83. var shininess = this.shininess.buildCode( builder, 'fv1' );
  84. var alpha = this.alpha ? this.alpha.buildCode( builder, 'fv1' ) : undefined;
  85. var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
  86. var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
  87. var light = this.light ? this.light.buildCode( builder, 'v3', { cache: 'light' } ) : undefined;
  88. var ao = this.ao ? this.ao.buildCode( builder, 'fv1' ) : undefined;
  89. var ambient = this.ambient ? this.ambient.buildCode( builder, 'c' ) : undefined;
  90. var shadow = this.shadow ? this.shadow.buildCode( builder, 'c' ) : undefined;
  91. var emissive = this.emissive ? this.emissive.buildCode( builder, 'c', { slot: 'emissive' } ) : undefined;
  92. var environment = this.environment ? this.environment.buildCode( builder, 'c', { slot: 'environment' } ) : undefined;
  93. var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.buildCode( builder, 'fv1' ) : undefined;
  94. material.requires.transparent = alpha != undefined;
  95. material.addFragmentPars( [
  96. "#include <common>",
  97. "#include <fog_pars_fragment>",
  98. "#include <bsdfs>",
  99. "#include <lights_pars_begin>",
  100. "#include <lights_phong_pars_fragment>",
  101. "#include <shadowmap_pars_fragment>",
  102. "#include <logdepthbuf_pars_fragment>"
  103. ].join( "\n" ) );
  104. var output = [
  105. // prevent undeclared normal
  106. "#include <normal_fragment_begin>",
  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. "#include <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. "#include <lights_fragment_begin>",
  144. "#include <lights_fragment_end>"
  145. );
  146. if ( light ) {
  147. output.push(
  148. light.code,
  149. "reflectedLight.directDiffuse = " + light.result + ";"
  150. );
  151. // apply color
  152. output.push(
  153. "reflectedLight.directDiffuse *= diffuseColor;",
  154. "reflectedLight.indirectDiffuse *= diffuseColor;"
  155. );
  156. }
  157. if ( ao ) {
  158. output.push(
  159. ao.code,
  160. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  161. );
  162. }
  163. if ( ambient ) {
  164. output.push(
  165. ambient.code,
  166. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  167. );
  168. }
  169. if ( shadow ) {
  170. output.push(
  171. shadow.code,
  172. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  173. "reflectedLight.directSpecular *= " + shadow.result + ";"
  174. );
  175. }
  176. if ( emissive ) {
  177. output.push(
  178. emissive.code,
  179. "reflectedLight.directDiffuse += " + emissive.result + ";"
  180. );
  181. }
  182. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;" );
  183. if ( environment ) {
  184. output.push( environment.code );
  185. if ( environmentAlpha ) {
  186. output.push(
  187. environmentAlpha.code,
  188. "outgoingLight = mix( outgoingLight, " + environment.result + ", " + environmentAlpha.result + " );"
  189. );
  190. } else {
  191. output.push( "outgoingLight = " + environment.result + ";" );
  192. }
  193. }
  194. if ( alpha ) {
  195. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  196. } else {
  197. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  198. }
  199. output.push(
  200. "#include <premultiplied_alpha_fragment>",
  201. "#include <tonemapping_fragment>",
  202. "#include <encodings_fragment>",
  203. "#include <fog_fragment>"
  204. );
  205. code = output.join( "\n" );
  206. }
  207. return code;
  208. };
  209. THREE.PhongNode.prototype.toJSON = function ( meta ) {
  210. var data = this.getJSONNode( meta );
  211. if ( ! data ) {
  212. data = this.createJSONNode( meta );
  213. // vertex
  214. if ( this.transform ) data.transform = this.transform.toJSON( meta ).uuid;
  215. // fragment
  216. data.color = this.color.toJSON( meta ).uuid;
  217. data.specular = this.specular.toJSON( meta ).uuid;
  218. data.shininess = this.shininess.toJSON( meta ).uuid;
  219. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  220. if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
  221. if ( this.normalScale ) data.normalScale = this.normalScale.toJSON( meta ).uuid;
  222. if ( this.light ) data.light = this.light.toJSON( meta ).uuid;
  223. if ( this.ao ) data.ao = this.ao.toJSON( meta ).uuid;
  224. if ( this.ambient ) data.ambient = this.ambient.toJSON( meta ).uuid;
  225. if ( this.shadow ) data.shadow = this.shadow.toJSON( meta ).uuid;
  226. if ( this.emissive ) data.emissive = this.emissive.toJSON( meta ).uuid;
  227. if ( this.environment ) data.environment = this.environment.toJSON( meta ).uuid;
  228. if ( this.environmentAlpha ) data.environmentAlpha = this.environmentAlpha.toJSON( meta ).uuid;
  229. }
  230. return data;
  231. };