StandardNode.js 14 KB

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