NodeMaterial.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import { Material, ShaderMaterial, NoToneMapping } from 'three';
  2. import { getNodesKeys, getCacheKey } from '../core/NodeUtils.js';
  3. import StackNode from '../core/StackNode.js';
  4. import LightsNode from '../lighting/LightsNode.js';
  5. import EnvironmentNode from '../lighting/EnvironmentNode.js';
  6. import ToneMappingNode from '../display/ToneMappingNode.js';
  7. import AONode from '../lighting/AONode.js';
  8. import {
  9. float, vec3, vec4,
  10. assign, mul, bypass, attribute, context, texture, lessThanEqual, discard,
  11. positionLocal, diffuseColor, skinning, instance, modelViewProjection, lightingContext, colorSpace,
  12. materialAlphaTest, materialColor, materialOpacity, materialEmissive, materialNormal, transformedNormalView,
  13. reference, rangeFog, densityFog
  14. } from '../shadernode/ShaderNodeElements.js';
  15. class NodeMaterial extends ShaderMaterial {
  16. constructor() {
  17. super();
  18. this.isNodeMaterial = true;
  19. this.type = this.constructor.name;
  20. this.lights = true;
  21. this.normals = true;
  22. this.lightsNode = null;
  23. }
  24. customProgramCacheKey() {
  25. return getCacheKey( this );
  26. }
  27. build( builder ) {
  28. this.construct( builder );
  29. }
  30. construct( builder ) {
  31. // < STACKS >
  32. const vertexStack = new StackNode();
  33. const fragmentStack = new StackNode();
  34. // < VERTEX STAGE >
  35. vertexStack.outputNode = this.constructPosition( builder, vertexStack );
  36. // < FRAGMENT STAGE >
  37. if ( this.normals === true ) this.constructNormal( builder, fragmentStack );
  38. this.constructDiffuseColor( builder, fragmentStack );
  39. this.constructVariants( builder, fragmentStack );
  40. const outgoingLightNode = this.constructLighting( builder, fragmentStack );
  41. fragmentStack.outputNode = this.constructOutput( builder, fragmentStack, outgoingLightNode, diffuseColor.a );
  42. // < FLOW >
  43. builder.addFlow( 'vertex', vertexStack );
  44. builder.addFlow( 'fragment', fragmentStack );
  45. }
  46. constructPosition( builder ) {
  47. const object = builder.object;
  48. let vertex = positionLocal;
  49. if ( this.positionNode !== null ) {
  50. vertex = bypass( vertex, assign( positionLocal, this.positionNode ) );
  51. }
  52. if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) && builder.isAvailable( 'instance' ) === true ) {
  53. vertex = bypass( vertex, instance( object ) );
  54. }
  55. if ( object.isSkinnedMesh === true ) {
  56. vertex = bypass( vertex, skinning( object ) );
  57. }
  58. builder.context.vertex = vertex;
  59. return modelViewProjection();
  60. }
  61. constructDiffuseColor( builder, stack ) {
  62. let colorNode = vec4( this.colorNode || materialColor );
  63. const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
  64. // VERTEX COLORS
  65. if ( this.vertexColors === true && builder.geometry.hasAttribute( 'color' ) ) {
  66. colorNode = vec4( mul( colorNode.xyz, attribute( 'color' ) ), colorNode.a );
  67. }
  68. // COLOR
  69. stack.assign( diffuseColor, colorNode );
  70. // OPACITY
  71. stack.assign( diffuseColor.a, diffuseColor.a.mul( opacityNode ) );
  72. // ALPHA TEST
  73. if ( this.alphaTestNode || this.alphaTest > 0 ) {
  74. const alphaTestNode = this.alphaTestNode ? float( this.alphaTestNode ) : materialAlphaTest;
  75. stack.add( discard( lessThanEqual( diffuseColor.a, alphaTestNode ) ) );
  76. }
  77. }
  78. constructVariants( /*builder*/ ) {
  79. // Interface function.
  80. }
  81. constructNormal( builder, stack ) {
  82. // NORMAL VIEW
  83. const normalNode = this.normalNode ? vec3( this.normalNode ) : materialNormal;
  84. stack.assign( transformedNormalView, normalNode );
  85. return normalNode;
  86. }
  87. constructLights( builder ) {
  88. let lightsNode = this.lightsNode || builder.lightsNode;
  89. const envNode = this.envNode || builder.scene.environmentNode;
  90. const materialLightsNode = [];
  91. if ( envNode ) {
  92. materialLightsNode.push( new EnvironmentNode( envNode ) );
  93. }
  94. if ( builder.material.aoMap ) {
  95. materialLightsNode.push( new AONode( texture( builder.material.aoMap ) ) );
  96. }
  97. if ( materialLightsNode.length > 0 ) {
  98. lightsNode = new LightsNode( [ ...lightsNode.lightNodes, ...materialLightsNode ] );
  99. }
  100. return lightsNode;
  101. }
  102. constructLightingModel( /*builder*/ ) {
  103. // Interface function.
  104. }
  105. constructLighting( builder ) {
  106. const { material } = builder;
  107. // OUTGOING LIGHT
  108. const lights = ( this.lights === true ) || this.lightsNode !== null;
  109. const lightsNode = lights ? this.constructLights( builder ) : null;
  110. const lightingModelNode = lightsNode ? this.constructLightingModel( builder ) : null;
  111. let outgoingLightNode = diffuseColor.xyz;
  112. if ( lightsNode && lightsNode.hasLight !== false ) {
  113. outgoingLightNode = lightingContext( lightsNode, lightingModelNode );
  114. }
  115. // EMISSIVE
  116. if ( ( this.emissiveNode && this.emissiveNode.isNode === true ) || ( material.emissive && material.emissive.isColor === true ) ) {
  117. outgoingLightNode = outgoingLightNode.add( vec3( this.emissiveNode || materialEmissive ) );
  118. }
  119. return outgoingLightNode;
  120. }
  121. constructOutput( builder, stack, outgoingLight, opacity ) {
  122. const renderer = builder.renderer;
  123. // TONE MAPPING
  124. let toneMappingNode = renderer.toneMappingNode;
  125. if ( ! toneMappingNode && renderer.toneMapping !== NoToneMapping ) {
  126. toneMappingNode = new ToneMappingNode( renderer.toneMapping, reference( 'toneMappingExposure', 'float', renderer ), outgoingLight );
  127. }
  128. if ( toneMappingNode && toneMappingNode.isNode === true ) {
  129. outgoingLight = context( toneMappingNode, { color: outgoingLight } );
  130. }
  131. // @TODO: Optimize outputNode to vec3.
  132. let outputNode = vec4( outgoingLight, opacity );
  133. // ENCODING
  134. outputNode = colorSpace( outputNode, renderer.outputEncoding );
  135. // FOG
  136. let fogNode = builder.fogNode;
  137. if ( ( fogNode && fogNode.isNode !== true ) && builder.scene.fog ) {
  138. const fog = builder.scene.fog;
  139. if ( fog.isFogExp2 ) {
  140. fogNode = densityFog( reference( 'color', 'color', fog ), reference( 'density', 'float', fog ) );
  141. } else if ( fog.isFog ) {
  142. fogNode = rangeFog( reference( 'color', 'color', fog ), reference( 'near', 'float', fog ), reference( 'far', 'float', fog ) );
  143. } else {
  144. console.error( 'NodeMaterial: Unsupported fog configuration.', fog );
  145. }
  146. }
  147. if ( fogNode ) outputNode = vec4( vec3( fogNode.mix( outputNode ) ), outputNode.w );
  148. return outputNode;
  149. }
  150. setDefaultValues( values ) {
  151. // This approach is to reuse the native refreshUniforms*
  152. // and turn available the use of features like transmission and environment in core
  153. for ( const property in values ) {
  154. const value = values[ property ];
  155. if ( this[ property ] === undefined ) {
  156. this[ property ] = value;
  157. if ( value && value.clone ) this[ property ] = value.clone();
  158. }
  159. }
  160. Object.assign( this.defines, values.defines );
  161. }
  162. toJSON( meta ) {
  163. const isRoot = ( meta === undefined || typeof meta === 'string' );
  164. if ( isRoot ) {
  165. meta = {
  166. textures: {},
  167. images: {},
  168. nodes: {}
  169. };
  170. }
  171. const data = Material.prototype.toJSON.call( this, meta );
  172. const nodeKeys = getNodesKeys( this );
  173. data.inputNodes = {};
  174. for ( const name of nodeKeys ) {
  175. data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
  176. }
  177. // TODO: Copied from Object3D.toJSON
  178. function extractFromCache( cache ) {
  179. const values = [];
  180. for ( const key in cache ) {
  181. const data = cache[ key ];
  182. delete data.metadata;
  183. values.push( data );
  184. }
  185. return values;
  186. }
  187. if ( isRoot ) {
  188. const textures = extractFromCache( meta.textures );
  189. const images = extractFromCache( meta.images );
  190. const nodes = extractFromCache( meta.nodes );
  191. if ( textures.length > 0 ) data.textures = textures;
  192. if ( images.length > 0 ) data.images = images;
  193. if ( nodes.length > 0 ) data.nodes = nodes;
  194. }
  195. return data;
  196. }
  197. static fromMaterial( /*material*/ ) { }
  198. }
  199. export default NodeMaterial;