PhongNode.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { Node } from '../../core/Node.js';
  5. import { ColorNode } from '../../inputs/ColorNode.js';
  6. import { FloatNode } from '../../inputs/FloatNode.js';
  7. function PhongNode() {
  8. Node.call( this );
  9. this.color = new ColorNode( 0xEEEEEE );
  10. this.specular = new ColorNode( 0x111111 );
  11. this.shininess = new FloatNode( 30 );
  12. }
  13. PhongNode.prototype = Object.create( Node.prototype );
  14. PhongNode.prototype.constructor = PhongNode;
  15. PhongNode.prototype.nodeType = "Phong";
  16. PhongNode.prototype.build = function ( builder ) {
  17. var code;
  18. builder.define( 'PHONG' );
  19. builder.requires.lights = true;
  20. if ( builder.isShader( 'vertex' ) ) {
  21. var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  22. builder.mergeUniform( THREE.UniformsUtils.merge( [
  23. THREE.UniformsLib.fog,
  24. THREE.UniformsLib.lights
  25. ] ) );
  26. builder.addParsCode( [
  27. "varying vec3 vViewPosition;",
  28. "#ifndef FLAT_SHADED",
  29. " varying vec3 vNormal;",
  30. "#endif",
  31. //"#include <encodings_pars_fragment>", // encoding functions
  32. "#include <fog_pars_vertex>",
  33. "#include <morphtarget_pars_vertex>",
  34. "#include <skinning_pars_vertex>",
  35. "#include <shadowmap_pars_vertex>",
  36. "#include <logdepthbuf_pars_vertex>",
  37. "#include <clipping_planes_pars_vertex>"
  38. ].join( "\n" ) );
  39. var output = [
  40. "#include <beginnormal_vertex>",
  41. "#include <morphnormal_vertex>",
  42. "#include <skinbase_vertex>",
  43. "#include <skinnormal_vertex>",
  44. "#include <defaultnormal_vertex>",
  45. "#ifndef FLAT_SHADED", // normal computed with derivatives when FLAT_SHADED
  46. " vNormal = normalize( transformedNormal );",
  47. "#endif",
  48. "#include <begin_vertex>"
  49. ];
  50. if ( position ) {
  51. output.push(
  52. position.code,
  53. position.result ? "transformed = " + position.result + ";" : ''
  54. );
  55. }
  56. output.push(
  57. " #include <morphtarget_vertex>",
  58. " #include <skinning_vertex>",
  59. " #include <project_vertex>",
  60. " #include <fog_vertex>",
  61. " #include <logdepthbuf_vertex>",
  62. " #include <clipping_planes_vertex>",
  63. " vViewPosition = - mvPosition.xyz;",
  64. " #include <worldpos_vertex>",
  65. " #include <shadowmap_vertex>",
  66. " #include <fog_vertex>"
  67. );
  68. code = output.join( "\n" );
  69. } else {
  70. // analyze all nodes to reuse generate codes
  71. if ( this.mask ) this.mask.analyze( builder );
  72. this.color.analyze( builder, { slot: 'color' } );
  73. this.specular.analyze( builder );
  74. this.shininess.analyze( builder );
  75. if ( this.alpha ) this.alpha.analyze( builder );
  76. if ( this.normal ) this.normal.analyze( builder );
  77. if ( this.light ) this.light.analyze( builder, { cache: 'light' } );
  78. if ( this.ao ) this.ao.analyze( builder );
  79. if ( this.ambient ) this.ambient.analyze( builder );
  80. if ( this.shadow ) this.shadow.analyze( builder );
  81. if ( this.emissive ) this.emissive.analyze( builder, { slot: 'emissive' } );
  82. if ( this.environment ) this.environment.analyze( builder, { slot: 'environment' } );
  83. if ( this.environmentAlpha && this.environment ) this.environmentAlpha.analyze( builder );
  84. // build code
  85. var mask = this.mask ? this.mask.flow( builder, 'b' ) : undefined;
  86. var color = this.color.flow( builder, 'c', { slot: 'color' } );
  87. var specular = this.specular.flow( builder, 'c' );
  88. var shininess = this.shininess.flow( builder, 'f' );
  89. var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  90. var normal = this.normal ? this.normal.flow( builder, 'v3' ) : undefined;
  91. var light = this.light ? this.light.flow( builder, 'v3', { cache: 'light' } ) : undefined;
  92. var ao = this.ao ? this.ao.flow( builder, 'f' ) : undefined;
  93. var ambient = this.ambient ? this.ambient.flow( builder, 'c' ) : undefined;
  94. var shadow = this.shadow ? this.shadow.flow( builder, 'c' ) : undefined;
  95. var emissive = this.emissive ? this.emissive.flow( builder, 'c', { slot: 'emissive' } ) : undefined;
  96. var environment = this.environment ? this.environment.flow( builder, 'c', { slot: 'environment' } ) : undefined;
  97. var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.flow( builder, 'f' ) : undefined;
  98. builder.requires.transparent = alpha !== undefined;
  99. builder.addParsCode( [
  100. "#include <fog_pars_fragment>",
  101. "#include <bsdfs>",
  102. "#include <lights_pars_begin>",
  103. "#include <lights_phong_pars_fragment>",
  104. "#include <shadowmap_pars_fragment>",
  105. "#include <logdepthbuf_pars_fragment>"
  106. ].join( "\n" ) );
  107. var output = [
  108. // prevent undeclared normal
  109. "#include <normal_fragment_begin>",
  110. // prevent undeclared material
  111. " BlinnPhongMaterial material;"
  112. ];
  113. if ( mask ) {
  114. output.push(
  115. mask.code,
  116. 'if ( ! ' + mask.result + ' ) discard;'
  117. );
  118. }
  119. output.push(
  120. color.code,
  121. " vec3 diffuseColor = " + color.result + ";",
  122. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  123. "#include <logdepthbuf_fragment>",
  124. specular.code,
  125. " vec3 specular = " + specular.result + ";",
  126. shininess.code,
  127. " float shininess = max( 0.0001, " + shininess.result + " );",
  128. " float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
  129. );
  130. if ( alpha ) {
  131. output.push(
  132. alpha.code,
  133. '#ifdef ALPHATEST',
  134. 'if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
  135. '#endif'
  136. );
  137. }
  138. if ( normal ) {
  139. output.push(
  140. normal.code,
  141. 'normal = ' + normal.result + ';'
  142. );
  143. }
  144. // optimization for now
  145. output.push( 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor' ) + ';' );
  146. output.push(
  147. // accumulation
  148. 'material.specularColor = specular;',
  149. 'material.specularShininess = shininess;',
  150. 'material.specularStrength = specularStrength;',
  151. "#include <lights_fragment_begin>",
  152. "#include <lights_fragment_end>"
  153. );
  154. if ( light ) {
  155. output.push(
  156. light.code,
  157. "reflectedLight.directDiffuse = " + light.result + ";"
  158. );
  159. // apply color
  160. output.push(
  161. "reflectedLight.directDiffuse *= diffuseColor;",
  162. "reflectedLight.indirectDiffuse *= diffuseColor;"
  163. );
  164. }
  165. if ( ao ) {
  166. output.push(
  167. ao.code,
  168. "reflectedLight.indirectDiffuse *= " + ao.result + ";"
  169. );
  170. }
  171. if ( ambient ) {
  172. output.push(
  173. ambient.code,
  174. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  175. );
  176. }
  177. if ( shadow ) {
  178. output.push(
  179. shadow.code,
  180. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  181. "reflectedLight.directSpecular *= " + shadow.result + ";"
  182. );
  183. }
  184. if ( emissive ) {
  185. output.push(
  186. emissive.code,
  187. "reflectedLight.directDiffuse += " + emissive.result + ";"
  188. );
  189. }
  190. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular;" );
  191. if ( environment ) {
  192. output.push( environment.code );
  193. if ( environmentAlpha ) {
  194. output.push(
  195. environmentAlpha.code,
  196. "outgoingLight = mix( outgoingLight, " + environment.result + ", " + environmentAlpha.result + " );"
  197. );
  198. } else {
  199. output.push( "outgoingLight = " + environment.result + ";" );
  200. }
  201. }
  202. /*
  203. switch( builder.material.combine ) {
  204. case THREE.ENVMAP_BLENDING_MULTIPLY:
  205. //output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  206. //outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );
  207. break;
  208. }
  209. */
  210. if ( alpha ) {
  211. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  212. } else {
  213. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  214. }
  215. output.push(
  216. "#include <premultiplied_alpha_fragment>",
  217. "#include <tonemapping_fragment>",
  218. "#include <encodings_fragment>",
  219. "#include <fog_fragment>"
  220. );
  221. code = output.join( "\n" );
  222. }
  223. return code;
  224. };
  225. PhongNode.prototype.copy = function ( source ) {
  226. Node.prototype.copy.call( this, source );
  227. // vertex
  228. if ( source.position ) this.position = source.position;
  229. // fragment
  230. this.color = source.color;
  231. this.specular = source.specular;
  232. this.shininess = source.shininess;
  233. if ( source.mask ) this.mask = source.mask;
  234. if ( source.alpha ) this.alpha = source.alpha;
  235. if ( source.normal ) this.normal = source.normal;
  236. if ( source.light ) this.light = source.light;
  237. if ( source.shadow ) this.shadow = source.shadow;
  238. if ( source.ao ) this.ao = source.ao;
  239. if ( source.emissive ) this.emissive = source.emissive;
  240. if ( source.ambient ) this.ambient = source.ambient;
  241. if ( source.environment ) this.environment = source.environment;
  242. if ( source.environmentAlpha ) this.environmentAlpha = source.environmentAlpha;
  243. };
  244. PhongNode.prototype.toJSON = function ( meta ) {
  245. var data = this.getJSONNode( meta );
  246. if ( ! data ) {
  247. data = this.createJSONNode( meta );
  248. // vertex
  249. if ( this.position ) data.position = this.position.toJSON( meta ).uuid;
  250. // fragment
  251. data.color = this.color.toJSON( meta ).uuid;
  252. data.specular = this.specular.toJSON( meta ).uuid;
  253. data.shininess = this.shininess.toJSON( meta ).uuid;
  254. if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
  255. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  256. if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
  257. if ( this.light ) data.light = this.light.toJSON( meta ).uuid;
  258. if ( this.ao ) data.ao = this.ao.toJSON( meta ).uuid;
  259. if ( this.ambient ) data.ambient = this.ambient.toJSON( meta ).uuid;
  260. if ( this.shadow ) data.shadow = this.shadow.toJSON( meta ).uuid;
  261. if ( this.emissive ) data.emissive = this.emissive.toJSON( meta ).uuid;
  262. if ( this.environment ) data.environment = this.environment.toJSON( meta ).uuid;
  263. if ( this.environmentAlpha ) data.environmentAlpha = this.environmentAlpha.toJSON( meta ).uuid;
  264. }
  265. return data;
  266. };
  267. export { PhongNode };