NodeMaterial.js 8.1 KB

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