NodeMaterial.js 7.3 KB

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