StandardNode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. import {
  2. UniformsLib,
  3. UniformsUtils
  4. } from '../../../../../build/three.module.js';
  5. import { Node } from '../../core/Node.js';
  6. import { ExpressionNode } from '../../core/ExpressionNode.js';
  7. import { ColorNode } from '../../inputs/ColorNode.js';
  8. import { FloatNode } from '../../inputs/FloatNode.js';
  9. import { SpecularMIPLevelNode } from '../../utils/SpecularMIPLevelNode.js';
  10. class StandardNode extends Node {
  11. constructor() {
  12. super();
  13. this.color = new ColorNode( 0xFFFFFF );
  14. this.roughness = new FloatNode( 1 );
  15. this.metalness = new FloatNode( 0 );
  16. }
  17. build( builder ) {
  18. let code;
  19. builder.define( 'STANDARD' );
  20. const useClearcoat = this.clearcoat || this.clearcoatRoughness || this.clearCoatNormal;
  21. if ( useClearcoat ) {
  22. builder.define( 'CLEARCOAT' );
  23. }
  24. builder.requires.lights = true;
  25. builder.extensions.derivatives = true;
  26. builder.extensions.shaderTextureLOD = true;
  27. if ( builder.isShader( 'vertex' ) ) {
  28. const position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  29. builder.mergeUniform( UniformsUtils.merge( [
  30. UniformsLib.fog,
  31. UniformsLib.lights
  32. ] ) );
  33. if ( UniformsLib.LTC_1 ) {
  34. // add ltc data textures to material uniforms
  35. builder.uniforms.ltc_1 = { value: undefined };
  36. builder.uniforms.ltc_2 = { value: undefined };
  37. }
  38. builder.addParsCode( /* glsl */`
  39. varying vec3 vViewPosition;
  40. #ifndef FLAT_SHADED
  41. varying vec3 vNormal;
  42. #endif
  43. //"#include <encodings_pars_fragment> // encoding functions
  44. #include <fog_pars_vertex>
  45. #include <morphtarget_pars_vertex>
  46. #include <skinning_pars_vertex>
  47. #include <shadowmap_pars_vertex>
  48. #include <logdepthbuf_pars_vertex>
  49. #include <clipping_planes_pars_vertex>`
  50. );
  51. const output = [
  52. '#include <beginnormal_vertex>',
  53. '#include <morphnormal_vertex>',
  54. '#include <skinbase_vertex>',
  55. '#include <skinnormal_vertex>',
  56. '#include <defaultnormal_vertex>',
  57. '#ifndef FLAT_SHADED', // Normal computed with derivatives when FLAT_SHADED
  58. ' vNormal = normalize( transformedNormal );',
  59. '#endif',
  60. '#include <begin_vertex>'
  61. ];
  62. if ( position ) {
  63. output.push(
  64. position.code,
  65. position.result ? 'transformed = ' + position.result + ';' : ''
  66. );
  67. }
  68. output.push(
  69. '#include <morphtarget_vertex>',
  70. '#include <skinning_vertex>',
  71. '#include <project_vertex>',
  72. '#include <fog_vertex>',
  73. '#include <logdepthbuf_vertex>',
  74. '#include <clipping_planes_vertex>',
  75. ' vViewPosition = - mvPosition.xyz;',
  76. '#include <worldpos_vertex>',
  77. '#include <shadowmap_vertex>'
  78. );
  79. code = output.join( '\n' );
  80. } else {
  81. const roughnessNode = new ExpressionNode( 'material.roughness', 'f' );
  82. const clearcoatRoughnessNode = new ExpressionNode( 'material.clearcoatRoughness', 'f' );
  83. const contextEnvironment = {
  84. roughness: roughnessNode,
  85. bias: new SpecularMIPLevelNode( roughnessNode ),
  86. viewNormal: new ExpressionNode( 'normal', 'v3' ),
  87. worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.normal, viewMatrix )', 'v3' ),
  88. gamma: true
  89. };
  90. const contextGammaOnly = {
  91. gamma: true
  92. };
  93. const contextClearcoatEnvironment = {
  94. roughness: clearcoatRoughnessNode,
  95. bias: new SpecularMIPLevelNode( clearcoatRoughnessNode ),
  96. viewNormal: new ExpressionNode( 'clearcoatNormal', 'v3' ),
  97. worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.clearcoatNormal, viewMatrix )', 'v3' ),
  98. gamma: true
  99. };
  100. // analyze all nodes to reuse generate codes
  101. if ( this.mask ) this.mask.analyze( builder );
  102. this.color.analyze( builder, { slot: 'color', context: contextGammaOnly } );
  103. this.roughness.analyze( builder );
  104. this.metalness.analyze( builder );
  105. if ( this.alpha ) this.alpha.analyze( builder );
  106. if ( this.normal ) this.normal.analyze( builder );
  107. if ( this.clearcoat ) this.clearcoat.analyze( builder );
  108. if ( this.clearcoatRoughness ) this.clearcoatRoughness.analyze( builder );
  109. if ( this.clearcoatNormal ) this.clearcoatNormal.analyze( builder );
  110. if ( this.reflectivity ) this.reflectivity.analyze( builder );
  111. if ( this.light ) this.light.analyze( builder, { cache: 'light' } );
  112. if ( this.ao ) this.ao.analyze( builder );
  113. if ( this.ambient ) this.ambient.analyze( builder );
  114. if ( this.shadow ) this.shadow.analyze( builder );
  115. if ( this.emissive ) this.emissive.analyze( builder, { slot: 'emissive' } );
  116. if ( this.environment ) {
  117. // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  118. // environment.analyze will detect if there is a need of calculate irradiance
  119. this.environment.analyze( builder, { cache: 'radiance', context: contextEnvironment, slot: 'radiance' } );
  120. if ( builder.requires.irradiance ) {
  121. this.environment.analyze( builder, { cache: 'irradiance', context: contextEnvironment, slot: 'irradiance' } );
  122. }
  123. }
  124. if ( this.sheenColor ) this.sheenColor.analyze( builder );
  125. // build code
  126. const mask = this.mask ? this.mask.flow( builder, 'b' ) : undefined;
  127. const color = this.color.flow( builder, 'c', { slot: 'color', context: contextGammaOnly } );
  128. const roughness = this.roughness.flow( builder, 'f' );
  129. const metalness = this.metalness.flow( builder, 'f' );
  130. const alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  131. const normal = this.normal ? this.normal.flow( builder, 'v3' ) : undefined;
  132. const clearcoat = this.clearcoat ? this.clearcoat.flow( builder, 'f' ) : undefined;
  133. const clearcoatRoughness = this.clearcoatRoughness ? this.clearcoatRoughness.flow( builder, 'f' ) : undefined;
  134. const clearcoatNormal = this.clearcoatNormal ? this.clearcoatNormal.flow( builder, 'v3' ) : undefined;
  135. const reflectivity = this.reflectivity ? this.reflectivity.flow( builder, 'f' ) : undefined;
  136. const light = this.light ? this.light.flow( builder, 'v3', { cache: 'light' } ) : undefined;
  137. const ao = this.ao ? this.ao.flow( builder, 'f' ) : undefined;
  138. const ambient = this.ambient ? this.ambient.flow( builder, 'c' ) : undefined;
  139. const shadow = this.shadow ? this.shadow.flow( builder, 'c' ) : undefined;
  140. const emissive = this.emissive ? this.emissive.flow( builder, 'c', { slot: 'emissive' } ) : undefined;
  141. let environment;
  142. if ( this.environment ) {
  143. environment = {
  144. radiance: this.environment.flow( builder, 'c', { cache: 'radiance', context: contextEnvironment, slot: 'radiance' } )
  145. };
  146. if ( builder.requires.irradiance ) {
  147. environment.irradiance = this.environment.flow( builder, 'c', { cache: 'irradiance', context: contextEnvironment, slot: 'irradiance' } );
  148. }
  149. }
  150. const clearcoatEnv = useClearcoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearcoat', context: contextClearcoatEnvironment, slot: 'environment' } ) : undefined;
  151. const sheenColor = this.sheenColor ? this.sheenColor.flow( builder, 'c' ) : undefined;
  152. builder.requires.transparent = alpha !== undefined;
  153. builder.addParsCode( /* glsl */`
  154. varying vec3 vViewPosition;
  155. #define NODE_MAXIMUM_SPECULAR_COEFFICIENT 0.16
  156. #define NODE_DEFAULT_SPECULAR_COEFFICIENT 0.04
  157. #ifndef FLAT_SHADED
  158. varying vec3 vNormal;
  159. #endif
  160. #include <dithering_pars_fragment>
  161. #include <fog_pars_fragment>
  162. #include <bsdfs>
  163. #include <lights_pars_begin>
  164. #include <lights_physical_pars_fragment>
  165. #include <shadowmap_pars_fragment>
  166. #include <logdepthbuf_pars_fragment>`
  167. );
  168. const output = [
  169. '#include <clipping_planes_fragment>',
  170. // add before: prevent undeclared normal
  171. ' #include <normal_fragment_begin>',
  172. ' #include <clearcoat_normal_fragment_begin>',
  173. // add before: prevent undeclared material
  174. ' PhysicalMaterial material;',
  175. ' material.diffuseColor = vec3( 1.0 );'
  176. ];
  177. if ( mask ) {
  178. output.push(
  179. mask.code,
  180. 'if ( ! ' + mask.result + ' ) discard;'
  181. );
  182. }
  183. output.push(
  184. color.code,
  185. ' vec3 diffuseColor = ' + color.result + ';',
  186. ' ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );',
  187. '#include <logdepthbuf_fragment>',
  188. roughness.code,
  189. ' float roughnessFactor = ' + roughness.result + ';',
  190. metalness.code,
  191. ' float metalnessFactor = ' + metalness.result + ';'
  192. );
  193. if ( alpha ) {
  194. output.push(
  195. alpha.code,
  196. '#ifdef ALPHATEST',
  197. ' if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
  198. '#endif'
  199. );
  200. }
  201. if ( normal ) {
  202. output.push(
  203. normal.code,
  204. 'normal = ' + normal.result + ';'
  205. );
  206. }
  207. if ( clearcoatNormal ) {
  208. output.push(
  209. clearcoatNormal.code,
  210. 'clearcoatNormal = ' + clearcoatNormal.result + ';'
  211. );
  212. }
  213. // anti-aliasing code by @elalish
  214. output.push(
  215. 'vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );',
  216. 'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );',
  217. );
  218. // optimization for now
  219. output.push(
  220. 'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * ( 1.0 - metalnessFactor )' ) + ';',
  221. 'material.roughness = max( roughnessFactor, 0.0525 );',
  222. 'material.roughness += geometryRoughness;',
  223. 'material.roughness = min( material.roughness, 1.0 );',
  224. 'material.roughness = clamp( roughnessFactor, 0.04, 1.0 );'
  225. );
  226. if ( clearcoat ) {
  227. output.push(
  228. clearcoat.code,
  229. 'material.clearcoat = saturate( ' + clearcoat.result + ' );' // Burley clearcoat model
  230. );
  231. } else if ( useClearcoat ) {
  232. output.push( 'material.clearcoat = 0.0;' );
  233. }
  234. if ( clearcoatRoughness ) {
  235. output.push(
  236. clearcoatRoughness.code,
  237. 'material.clearcoatRoughness = max( ' + clearcoatRoughness.result + ', 0.0525 );',
  238. 'material.clearcoatRoughness += geometryRoughness;',
  239. 'material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );'
  240. );
  241. } else if ( useClearcoat ) {
  242. output.push( 'material.clearcoatRoughness = 0.0;' );
  243. }
  244. if ( sheenColor ) {
  245. output.push( 'material.sheenColor = ' + sheenColor.result + ';' );
  246. }
  247. if ( reflectivity ) {
  248. output.push(
  249. reflectivity.code,
  250. 'material.specularColor = mix( vec3( NODE_MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );'
  251. );
  252. } else {
  253. output.push(
  254. 'material.specularColor = mix( vec3( NODE_DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor, metalnessFactor );'
  255. );
  256. }
  257. output.push(
  258. '#include <lights_fragment_begin>'
  259. );
  260. if ( light ) {
  261. output.push(
  262. light.code,
  263. 'reflectedLight.directDiffuse = ' + light.result + ';'
  264. );
  265. // apply color
  266. output.push(
  267. 'diffuseColor *= 1.0 - metalnessFactor;',
  268. 'reflectedLight.directDiffuse *= diffuseColor;',
  269. 'reflectedLight.indirectDiffuse *= diffuseColor;'
  270. );
  271. }
  272. if ( ao ) {
  273. output.push(
  274. ao.code,
  275. 'reflectedLight.indirectDiffuse *= ' + ao.result + ';',
  276. 'float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );',
  277. 'reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ' + ao.result + ', material.roughness );'
  278. );
  279. }
  280. if ( ambient ) {
  281. output.push(
  282. ambient.code,
  283. 'reflectedLight.indirectDiffuse += ' + ambient.result + ';'
  284. );
  285. }
  286. if ( shadow ) {
  287. output.push(
  288. shadow.code,
  289. 'reflectedLight.directDiffuse *= ' + shadow.result + ';',
  290. 'reflectedLight.directSpecular *= ' + shadow.result + ';'
  291. );
  292. }
  293. if ( emissive ) {
  294. output.push(
  295. emissive.code,
  296. 'reflectedLight.directDiffuse += ' + emissive.result + ';'
  297. );
  298. }
  299. if ( environment ) {
  300. output.push( environment.radiance.code );
  301. if ( builder.requires.irradiance ) {
  302. output.push( environment.irradiance.code );
  303. }
  304. if ( clearcoatEnv ) {
  305. output.push(
  306. clearcoatEnv.code,
  307. 'clearcoatRadiance += ' + clearcoatEnv.result + ';'
  308. );
  309. }
  310. output.push( 'radiance += ' + environment.radiance.result + ';' );
  311. if ( builder.requires.irradiance ) {
  312. output.push( 'iblIrradiance += PI * ' + environment.irradiance.result + ';' );
  313. }
  314. }
  315. output.push(
  316. '#include <lights_fragment_end>'
  317. );
  318. output.push( 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;' );
  319. if ( alpha ) {
  320. output.push( 'gl_FragColor = vec4( outgoingLight, ' + alpha.result + ' );' );
  321. } else {
  322. output.push( 'gl_FragColor = vec4( outgoingLight, 1.0 );' );
  323. }
  324. output.push(
  325. '#include <tonemapping_fragment>',
  326. '#include <encodings_fragment>',
  327. '#include <fog_fragment>',
  328. '#include <premultiplied_alpha_fragment>',
  329. '#include <dithering_fragment>'
  330. );
  331. code = output.join( '\n' );
  332. }
  333. return code;
  334. }
  335. copy( source ) {
  336. super.copy( source );
  337. // vertex
  338. if ( source.position ) this.position = source.position;
  339. // fragment
  340. this.color = source.color;
  341. this.roughness = source.roughness;
  342. this.metalness = source.metalness;
  343. if ( source.mask ) this.mask = source.mask;
  344. if ( source.alpha ) this.alpha = source.alpha;
  345. if ( source.normal ) this.normal = source.normal;
  346. if ( source.clearcoat ) this.clearcoat = source.clearcoat;
  347. if ( source.clearcoatRoughness ) this.clearcoatRoughness = source.clearcoatRoughness;
  348. if ( source.clearcoatNormal ) this.clearcoatNormal = source.clearcoatNormal;
  349. if ( source.reflectivity ) this.reflectivity = source.reflectivity;
  350. if ( source.light ) this.light = source.light;
  351. if ( source.shadow ) this.shadow = source.shadow;
  352. if ( source.ao ) this.ao = source.ao;
  353. if ( source.emissive ) this.emissive = source.emissive;
  354. if ( source.ambient ) this.ambient = source.ambient;
  355. if ( source.environment ) this.environment = source.environment;
  356. if ( source.sheenColor ) this.sheenColor = source.sheenColor;
  357. return this;
  358. }
  359. toJSON( meta ) {
  360. let data = this.getJSONNode( meta );
  361. if ( ! data ) {
  362. data = this.createJSONNode( meta );
  363. // vertex
  364. if ( this.position ) data.position = this.position.toJSON( meta ).uuid;
  365. // fragment
  366. data.color = this.color.toJSON( meta ).uuid;
  367. data.roughness = this.roughness.toJSON( meta ).uuid;
  368. data.metalness = this.metalness.toJSON( meta ).uuid;
  369. if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
  370. if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
  371. if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
  372. if ( this.clearcoat ) data.clearcoat = this.clearcoat.toJSON( meta ).uuid;
  373. if ( this.clearcoatRoughness ) data.clearcoatRoughness = this.clearcoatRoughness.toJSON( meta ).uuid;
  374. if ( this.clearcoatNormal ) data.clearcoatNormal = this.clearcoatNormal.toJSON( meta ).uuid;
  375. if ( this.reflectivity ) data.reflectivity = this.reflectivity.toJSON( meta ).uuid;
  376. if ( this.light ) data.light = this.light.toJSON( meta ).uuid;
  377. if ( this.shadow ) data.shadow = this.shadow.toJSON( meta ).uuid;
  378. if ( this.ao ) data.ao = this.ao.toJSON( meta ).uuid;
  379. if ( this.emissive ) data.emissive = this.emissive.toJSON( meta ).uuid;
  380. if ( this.ambient ) data.ambient = this.ambient.toJSON( meta ).uuid;
  381. if ( this.environment ) data.environment = this.environment.toJSON( meta ).uuid;
  382. if ( this.sheenColor ) data.sheenColor = this.sheenColor.toJSON( meta ).uuid;
  383. }
  384. return data;
  385. }
  386. }
  387. StandardNode.prototype.nodeType = 'Standard';
  388. export { StandardNode };