StandardNode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. function StandardNode() {
  11. Node.call( this );
  12. this.color = new ColorNode( 0xFFFFFF );
  13. this.roughness = new FloatNode( 1 );
  14. this.metalness = new FloatNode( 0 );
  15. }
  16. StandardNode.prototype = Object.create( Node.prototype );
  17. StandardNode.prototype.constructor = StandardNode;
  18. StandardNode.prototype.nodeType = "Standard";
  19. StandardNode.prototype.build = function ( builder ) {
  20. var code;
  21. builder.define( 'STANDARD' );
  22. var useClearcoat = this.clearcoat || this.clearcoatRoughness || this.clearCoatNormal;
  23. if ( useClearcoat ) {
  24. builder.define( 'CLEARCOAT' );
  25. }
  26. builder.requires.lights = true;
  27. builder.extensions.derivatives = true;
  28. builder.extensions.shaderTextureLOD = true;
  29. if ( builder.isShader( 'vertex' ) ) {
  30. var position = this.position ? this.position.analyzeAndFlow( builder, 'v3', { cache: 'position' } ) : undefined;
  31. builder.mergeUniform( UniformsUtils.merge( [
  32. UniformsLib.fog,
  33. UniformsLib.lights
  34. ] ) );
  35. if ( UniformsLib.LTC_1 ) {
  36. // add ltc data textures to material uniforms
  37. builder.uniforms.ltc_1 = { value: undefined };
  38. builder.uniforms.ltc_2 = { value: undefined };
  39. }
  40. builder.addParsCode( [
  41. "varying vec3 vViewPosition;",
  42. "#ifndef FLAT_SHADED",
  43. " varying vec3 vNormal;",
  44. "#endif",
  45. //"#include <encodings_pars_fragment>", // encoding functions
  46. "#include <fog_pars_vertex>",
  47. "#include <morphtarget_pars_vertex>",
  48. "#include <skinning_pars_vertex>",
  49. "#include <shadowmap_pars_vertex>",
  50. "#include <logdepthbuf_pars_vertex>",
  51. "#include <clipping_planes_pars_vertex>"
  52. ].join( "\n" ) );
  53. var output = [
  54. "#include <beginnormal_vertex>",
  55. "#include <morphnormal_vertex>",
  56. "#include <skinbase_vertex>",
  57. "#include <skinnormal_vertex>",
  58. "#include <defaultnormal_vertex>",
  59. "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED
  60. " vNormal = normalize( transformedNormal );",
  61. "#endif",
  62. "#include <begin_vertex>"
  63. ];
  64. if ( position ) {
  65. output.push(
  66. position.code,
  67. position.result ? "transformed = " + position.result + ";" : ''
  68. );
  69. }
  70. output.push(
  71. "#include <morphtarget_vertex>",
  72. "#include <skinning_vertex>",
  73. "#include <project_vertex>",
  74. "#include <fog_vertex>",
  75. "#include <logdepthbuf_vertex>",
  76. "#include <clipping_planes_vertex>",
  77. " vViewPosition = - mvPosition.xyz;",
  78. "#include <worldpos_vertex>",
  79. "#include <shadowmap_vertex>"
  80. );
  81. code = output.join( "\n" );
  82. } else {
  83. var specularRoughness = new ExpressionNode( 'material.specularRoughness', 'f' );
  84. var clearcoatRoughness = new ExpressionNode( 'material.clearcoatRoughness', 'f' );
  85. var contextEnvironment = {
  86. roughness: specularRoughness,
  87. bias: new SpecularMIPLevelNode( specularRoughness ),
  88. viewNormal: new ExpressionNode( 'normal', 'v3' ),
  89. worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.normal, viewMatrix )', 'v3' ),
  90. gamma: true
  91. };
  92. var contextGammaOnly = {
  93. gamma: true
  94. };
  95. var contextClearcoatEnvironment = {
  96. roughness: clearcoatRoughness,
  97. bias: new SpecularMIPLevelNode( clearcoatRoughness ),
  98. viewNormal: new ExpressionNode( 'clearcoatNormal', 'v3' ),
  99. worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.clearcoatNormal, viewMatrix )', 'v3' ),
  100. gamma: true
  101. };
  102. // analyze all nodes to reuse generate codes
  103. if ( this.mask ) this.mask.analyze( builder );
  104. this.color.analyze( builder, { slot: 'color', context: contextGammaOnly } );
  105. this.roughness.analyze( builder );
  106. this.metalness.analyze( builder );
  107. if ( this.alpha ) this.alpha.analyze( builder );
  108. if ( this.normal ) this.normal.analyze( builder );
  109. if ( this.clearcoat ) this.clearcoat.analyze( builder );
  110. if ( this.clearcoatRoughness ) this.clearcoatRoughness.analyze( builder );
  111. if ( this.clearcoatNormal ) this.clearcoatNormal.analyze( builder );
  112. if ( this.reflectivity ) this.reflectivity.analyze( builder );
  113. if ( this.light ) this.light.analyze( builder, { cache: 'light' } );
  114. if ( this.ao ) this.ao.analyze( builder );
  115. if ( this.ambient ) this.ambient.analyze( builder );
  116. if ( this.shadow ) this.shadow.analyze( builder );
  117. if ( this.emissive ) this.emissive.analyze( builder, { slot: 'emissive' } );
  118. if ( this.environment ) {
  119. // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
  120. // environment.analyze will detect if there is a need of calculate irradiance
  121. this.environment.analyze( builder, { cache: 'radiance', context: contextEnvironment, slot: 'radiance' } );
  122. if ( builder.requires.irradiance ) {
  123. this.environment.analyze( builder, { cache: 'irradiance', context: contextEnvironment, slot: 'irradiance' } );
  124. }
  125. }
  126. if ( this.sheen ) this.sheen.analyze( builder );
  127. // build code
  128. var mask = this.mask ? this.mask.flow( builder, 'b' ) : undefined;
  129. var color = this.color.flow( builder, 'c', { slot: 'color', context: contextGammaOnly } );
  130. var roughness = this.roughness.flow( builder, 'f' );
  131. var metalness = this.metalness.flow( builder, 'f' );
  132. var alpha = this.alpha ? this.alpha.flow( builder, 'f' ) : undefined;
  133. var normal = this.normal ? this.normal.flow( builder, 'v3' ) : undefined;
  134. var clearcoat = this.clearcoat ? this.clearcoat.flow( builder, 'f' ) : undefined;
  135. var clearcoatRoughness = this.clearcoatRoughness ? this.clearcoatRoughness.flow( builder, 'f' ) : undefined;
  136. var clearcoatNormal = this.clearcoatNormal ? this.clearcoatNormal.flow( builder, 'v3' ) : undefined;
  137. var reflectivity = this.reflectivity ? this.reflectivity.flow( builder, 'f' ) : undefined;
  138. var light = this.light ? this.light.flow( builder, 'v3', { cache: 'light' } ) : undefined;
  139. var ao = this.ao ? this.ao.flow( builder, 'f' ) : undefined;
  140. var ambient = this.ambient ? this.ambient.flow( builder, 'c' ) : undefined;
  141. var shadow = this.shadow ? this.shadow.flow( builder, 'c' ) : undefined;
  142. var emissive = this.emissive ? this.emissive.flow( builder, 'c', { slot: 'emissive' } ) : undefined;
  143. var environment;
  144. if ( this.environment ) {
  145. environment = {
  146. radiance: this.environment.flow( builder, 'c', { cache: 'radiance', context: contextEnvironment, slot: 'radiance' } )
  147. };
  148. if ( builder.requires.irradiance ) {
  149. environment.irradiance = this.environment.flow( builder, 'c', { cache: 'irradiance', context: contextEnvironment, slot: 'irradiance' } );
  150. }
  151. }
  152. var clearcoatEnv = useClearcoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearcoat', context: contextClearcoatEnvironment, slot: 'environment' } ) : undefined;
  153. var sheen = this.sheen ? this.sheen.flow( builder, 'c' ) : undefined;
  154. builder.requires.transparent = alpha !== undefined;
  155. builder.addParsCode( [
  156. "varying vec3 vViewPosition;",
  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. ].join( "\n" ) );
  168. var 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.specularRoughness = max( roughnessFactor, 0.0525 );',
  222. 'material.specularRoughness += geometryRoughness;',
  223. 'material.specularRoughness = min( material.specularRoughness, 1.0 );',
  224. 'material.specularRoughness = 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 ( sheen ) {
  245. output.push( 'material.sheenColor = ' + sheen.result + ';' );
  246. }
  247. if ( reflectivity ) {
  248. output.push(
  249. reflectivity.code,
  250. 'material.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( ' + reflectivity.result + ' ) ), diffuseColor, metalnessFactor );'
  251. );
  252. } else {
  253. output.push(
  254. 'material.specularColor = mix( vec3( 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.specularRoughness );"
  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. StandardNode.prototype.copy = function ( source ) {
  336. Node.prototype.copy.call( this, 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.sheen ) this.sheen = source.sheen;
  357. return this;
  358. };
  359. StandardNode.prototype.toJSON = function ( meta ) {
  360. var 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.sheen ) data.sheen = this.sheen.toJSON( meta ).uuid;
  383. }
  384. return data;
  385. };
  386. export { StandardNode };
粤ICP备19079148号