2
0

NodeMaterial.js 9.5 KB

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