StandardNode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. import { RoughnessToBlinnExponentNode } from '../../bsdfs/RoughnessToBlinnExponentNode.js';
  8. function StandardNode() {
  9. Node.call( this );
  10. this.color = new ColorNode( 0xEEEEEE );
  11. this.roughness = new FloatNode( 0.5 );
  12. this.metalness = new FloatNode( 0.5 );
  13. }
  14. StandardNode.prototype = Object.create( Node.prototype );
  15. StandardNode.prototype.constructor = StandardNode;
  16. StandardNode.prototype.nodeType = "Standard";
  17. StandardNode.prototype.build = function ( builder ) {
  18. var code;
  19. builder.define( this.clearCoat || this.clearCoatRoughness ? 'PHYSICAL' : 'STANDARD' );
  20. builder.requires.lights = true;
  21. builder.extensions.shaderTextureLOD = true;
  22. if ( builder.isShader( 'vertex' ) ) {
  23. var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  24. builder.mergeUniform( THREE.UniformsUtils.merge( [
  25. THREE.UniformsLib.fog,
  26. THREE.UniformsLib.lights
  27. ] ) );
  28. if ( THREE.UniformsLib.LTC_1 ) {
  29. // add ltc data textures to material uniforms
  30. builder.uniforms.ltc_1 = { value: undefined };
  31. builder.uniforms.ltc_2 = { value: undefined };
  32. }
  33. builder.addParsCode( [
  34. "varying vec3 vViewPosition;",
  35. "#ifndef FLAT_SHADED",
  36. " varying vec3 vNormal;",
  37. "#endif",
  38. //"#include <encodings_pars_fragment>", // encoding functions
  39. "#include <fog_pars_vertex>",
  40. "#include <morphtarget_pars_vertex>",
  41. "#include <skinning_pars_vertex>",
  42. "#include <shadowmap_pars_vertex>",
  43. "#include <logdepthbuf_pars_vertex>",
  44. "#include <clipping_planes_pars_vertex>"
  45. ].join( "\n" ) );
  46. var output = [
  47. "#include <beginnormal_vertex>",
  48. "#include <morphnormal_vertex>",
  49. "#include <skinbase_vertex>",
  50. "#include <skinnormal_vertex>",
  51. "#include <defaultnormal_vertex>",
  52. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  53. " vNormal = normalize( transformedNormal );",
  54. "#endif",
  55. "#include <begin_vertex>"
  56. ];
  57. if ( position ) {
  58. output.push(
  59. position.code,
  60. position.result ? "transformed = " + position.result + ";" : ''
  61. );
  62. }
  63. output.push(
  64. "#include <morphtarget_vertex>",
  65. "#include <skinning_vertex>",
  66. "#include <project_vertex>",
  67. "#include <fog_vertex>",
  68. "#include <logdepthbuf_vertex>",
  69. "#include <clipping_planes_vertex>",
  70. " vViewPosition = - mvPosition.xyz;",
  71. "#include <worldpos_vertex>",
  72. "#include <shadowmap_vertex>"
  73. );
  74. code = output.join( "\n" );
  75. } else {
  76. var contextEnvironment = {
  77. bias: RoughnessToBlinnExponentNode,
  78. gamma: true
  79. };
  80. var contextGammaOnly = {
  81. gamma: true
  82. };
  83. var useClearCoat = ! builder.isDefined( 'STANDARD' );
  84. // analyze all nodes to reuse generate codes
  85. if ( this.mask ) this.mask.analyze( builder );
  86. this.color.analyze( builder, { slot: 'color', context: contextGammaOnly } );
  87. this.roughness.analyze( builder );
  88. this.metalness.analyze( builder );
  89. if ( this.alpha ) this.alpha.analyze( builder );
  90. if ( this.normal ) this.normal.analyze( builder );
  91. if ( this.clearCoat ) this.clearCoat.analyze( builder );
  92. if ( this.clearCoatRoughness ) this.clearCoatRoughness.analyze( builder );
  93. if ( this.reflectivity ) this.reflectivity.analyze( builder );
  94. if ( this.light ) this.light.analyze( builder, { cache: 'light' } );
  95. if ( this.ao ) this.ao.analyze( builder );
  96. if ( this.ambient ) this.ambient.analyze( builder );
  97. if ( this.shadow ) this.shadow.analyze( builder );
  98. if ( this.emissive ) this.emissive.analyze( builder, { slot: 'emissive' } );
  99. if ( this.environment ) this.environment.analyze( builder, { cache: 'env', context: contextEnvironment, slot: 'environment' } ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  100. // build code
  101. var mask = this.mask ? this.mask.flow( builder, 'b' ) : undefined;
  102. var color = this.color.flow( builder, 'c', { slot: 'color', context: contextGammaOnly } );
  103. var roughness = this.roughness.flow( builder, 'f' );
  104. var metalness = this.metalness.flow( builder, 'f' );
  105. var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  106. var normal = this.normal ? this.normal.flow( builder, 'v3' ) : undefined;
  107. var clearCoat = this.clearCoat ? this.clearCoat.flow( builder, 'f' ) : undefined;
  108. var clearCoatRoughness = this.clearCoatRoughness ? this.clearCoatRoughness.flow( builder, 'f' ) : undefined;
  109. var reflectivity = this.reflectivity ? this.reflectivity.flow( builder, 'f' ) : undefined;
  110. var light = this.light ? this.light.flow( builder, 'v3', { cache: 'light' } ) : undefined;
  111. var ao = this.ao ? this.ao.flow( builder, 'f' ) : undefined;
  112. var ambient = this.ambient ? this.ambient.flow( builder, 'c' ) : undefined;
  113. var shadow = this.shadow ? this.shadow.flow( builder, 'c' ) : undefined;
  114. var emissive = this.emissive ? this.emissive.flow( builder, 'c', { slot: 'emissive' } ) : undefined;
  115. var environment = this.environment ? this.environment.flow( builder, 'c', { cache: 'env', context: contextEnvironment, slot: 'environment' } ) : undefined;
  116. var clearCoatEnv = useClearCoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearCoat', context: contextEnvironment, slot: 'environment' } ) : undefined;
  117. builder.requires.transparent = alpha !== undefined;
  118. builder.addParsCode( [
  119. "varying vec3 vViewPosition;",
  120. "#ifndef FLAT_SHADED",
  121. " varying vec3 vNormal;",
  122. "#endif",
  123. "#include <dithering_pars_fragment>",
  124. "#include <fog_pars_fragment>",
  125. "#include <bsdfs>",
  126. "#include <lights_pars_begin>",
  127. "#include <lights_physical_pars_fragment>",
  128. "#include <shadowmap_pars_fragment>",
  129. "#include <logdepthbuf_pars_fragment>"
  130. ].join( "\n" ) );
  131. var output = [
  132. "#include <clipping_planes_fragment>",
  133. // add before: prevent undeclared normal
  134. " #include <normal_fragment_begin>",
  135. // add before: prevent undeclared material
  136. " PhysicalMaterial material;",
  137. " material.diffuseColor = vec3( 1.0 );"
  138. ];
  139. if ( mask ) {
  140. output.push(
  141. mask.code,
  142. 'if ( ! ' + mask.result + ' ) discard;'
  143. );
  144. }
  145. output.push(
  146. color.code,
  147. " vec3 diffuseColor = " + color.result + ";",
  148. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  149. "#include <logdepthbuf_fragment>",
  150. roughness.code,
  151. " float roughnessFactor = " + roughness.result + ";",
  152. metalness.code,
  153. " float metalnessFactor = " + metalness.result + ";"
  154. );
  155. if ( alpha ) {
  156. output.push(
  157. alpha.code,
  158. '#ifdef ALPHATEST',
  159. ' if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
  160. '#endif'
  161. );
  162. }
  163. if ( normal ) {
  164. output.push(
  165. normal.code,
  166. 'normal = ' + normal.result + ';'
  167. );
  168. }
  169. // optimization for now
  170. output.push(
  171. 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';',
  172. 'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );'
  173. );
  174. if ( clearCoat ) {
  175. output.push(
  176. clearCoat.code,
  177. 'material.clearCoat = saturate( ' + clearCoat.result + ' );'
  178. );
  179. } else if ( useClearCoat ) {
  180. output.push( 'material.clearCoat = 0.0;' );
  181. }
  182. if ( clearCoatRoughness ) {
  183. output.push(
  184. clearCoatRoughness.code,
  185. 'material.clearCoatRoughness = clamp( ' + clearCoatRoughness.result + ', 0.04, 1.0 );'
  186. );
  187. } else if ( useClearCoat ) {
  188. output.push( 'material.clearCoatRoughness = 0.0;' );
  189. }
  190. if ( reflectivity ) {
  191. output.push(
  192. reflectivity.code,
  193. 'material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );'
  194. );
  195. } else {
  196. output.push(
  197. 'material.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor, metalnessFactor );'
  198. );
  199. }
  200. output.push(
  201. "#include <lights_fragment_begin>"
  202. );
  203. if ( light ) {
  204. output.push(
  205. light.code,
  206. "reflectedLight.directDiffuse = " + light.result + ";"
  207. );
  208. // apply color
  209. output.push(
  210. "diffuseColor *= 1.0 - metalnessFactor;",
  211. "reflectedLight.directDiffuse *= diffuseColor;",
  212. "reflectedLight.indirectDiffuse *= diffuseColor;"
  213. );
  214. }
  215. if ( ao ) {
  216. output.push(
  217. ao.code,
  218. "reflectedLight.indirectDiffuse *= " + ao.result + ";",
  219. "float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );",
  220. "reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, " + ao.result + ", material.specularRoughness );"
  221. );
  222. }
  223. if ( ambient ) {
  224. output.push(
  225. ambient.code,
  226. "reflectedLight.indirectDiffuse += " + ambient.result + ";"
  227. );
  228. }
  229. if ( shadow ) {
  230. output.push(
  231. shadow.code,
  232. "reflectedLight.directDiffuse *= " + shadow.result + ";",
  233. "reflectedLight.directSpecular *= " + shadow.result + ";"
  234. );
  235. }
  236. if ( emissive ) {
  237. output.push(
  238. emissive.code,
  239. "reflectedLight.directDiffuse += " + emissive.result + ";"
  240. );
  241. }
  242. if ( environment ) {
  243. output.push( environment.code );
  244. if ( clearCoatEnv ) {
  245. output.push(
  246. clearCoatEnv.code,
  247. "clearCoatRadiance += " + clearCoatEnv.result + ";"
  248. );
  249. }
  250. output.push( "radiance += " + environment.result + ";" );
  251. if ( environment.extra.irradiance ) {
  252. output.push( "irradiance += PI * " + environment.extra.irradiance + ";" );
  253. }
  254. }
  255. output.push(
  256. "#include <lights_fragment_end>"
  257. );
  258. output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
  259. if ( alpha ) {
  260. output.push( "gl_FragColor = vec4( outgoingLight, " + alpha.result + " );" );
  261. } else {
  262. output.push( "gl_FragColor = vec4( outgoingLight, 1.0 );" );
  263. }
  264. output.push(
  265. "#include <tonemapping_fragment>",
  266. "#include <encodings_fragment>",
  267. "#include <fog_fragment>",
  268. "#include <premultiplied_alpha_fragment>",
  269. "#include <dithering_fragment>"
  270. );
  271. code = output.join( "\n" );
  272. }
  273. return code;
  274. };
  275. StandardNode.prototype.copy = function ( source ) {
  276. Node.prototype.copy.call( this, source );
  277. // vertex
  278. if ( source.position ) this.position = source.position;
  279. // fragment
  280. this.color = source.color;
  281. this.roughness = source.roughness;
  282. this.metalness = source.metalness;
  283. if ( source.mask ) this.mask = source.mask;
  284. if ( source.alpha ) this.alpha = source.alpha;
  285. if ( source.normal ) this.normal = source.normal;
  286. if ( source.clearCoat ) this.clearCoat = source.clearCoat;
  287. if ( source.clearCoatRoughness ) this.clearCoatRoughness = source.clearCoatRoughness;
  288. if ( source.reflectivity ) this.reflectivity = source.reflectivity;
  289. if ( source.light ) this.light = source.light;
  290. if ( source.shadow ) this.shadow = source.shadow;
  291. if ( source.ao ) this.ao = source.ao;
  292. if ( source.emissive ) this.emissive = source.emissive;
  293. if ( source.ambient ) this.ambient = source.ambient;
  294. if ( source.environment ) this.environment = source.environment;
  295. };
  296. StandardNode.prototype.toJSON = function ( meta ) {
  297. var data = this.getJSONNode( meta );
  298. if ( ! data ) {
  299. data = this.createJSONNode( meta );
  300. // vertex
  301. if ( this.position ) data.position = this.position.toJSON( meta ).uuid;
  302. // fragment
  303. data.color = this.color.toJSON( meta ).uuid;
  304. data.roughness = this.roughness.toJSON( meta ).uuid;
  305. data.metalness = this.metalness.toJSON( meta ).uuid;
  306. if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
  307. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  308. if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
  309. if ( this.clearCoat ) data.clearCoat = this.clearCoat.toJSON( meta ).uuid;
  310. if ( this.clearCoatRoughness ) data.clearCoatRoughness = this.clearCoatRoughness.toJSON( meta ).uuid;
  311. if ( this.reflectivity ) data.reflectivity = this.reflectivity.toJSON( meta ).uuid;
  312. if ( this.light ) data.light = this.light.toJSON( meta ).uuid;
  313. if ( this.shadow ) data.shadow = this.shadow.toJSON( meta ).uuid;
  314. if ( this.ao ) data.ao = this.ao.toJSON( meta ).uuid;
  315. if ( this.emissive ) data.emissive = this.emissive.toJSON( meta ).uuid;
  316. if ( this.ambient ) data.ambient = this.ambient.toJSON( meta ).uuid;
  317. if ( this.environment ) data.environment = this.environment.toJSON( meta ).uuid;
  318. }
  319. return data;
  320. };
  321. export { StandardNode };