StandardNode.js 12 KB

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