NodeMaterial.js 9.6 KB

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