NodeMaterial.js 9.0 KB

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