StandardNode.js 12 KB

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