123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import Node from '../core/Node.js';
- import OperatorNode from '../math/OperatorNode.js';
- import MaterialReferenceNode from './MaterialReferenceNode.js';
- import TextureNode from './TextureNode.js';
- import SplitNode from '../utils/SplitNode.js';
- class MaterialNode extends Node {
- static ALPHA_TEST = 'alphaTest';
- static COLOR = 'color';
- static OPACITY = 'opacity';
- static ROUGHNESS = 'roughness';
- static METALNESS = 'metalness';
- static EMISSIVE = 'emissive';
- static ROTATION = 'rotation';
- constructor( scope = MaterialNode.COLOR ) {
- super();
- this.scope = scope;
- }
- getNodeType( builder ) {
- const scope = this.scope;
- const material = builder.context.material;
- if ( scope === MaterialNode.COLOR ) {
- return material.map !== null ? 'vec4' : 'vec3';
- } else if ( scope === MaterialNode.OPACITY || scope === MaterialNode.ROTATION ) {
- return 'float';
- } else if ( scope === MaterialNode.EMISSIVE ) {
- return 'vec3';
- } else if ( scope === MaterialNode.ROUGHNESS || scope === MaterialNode.METALNESS ) {
- return 'float';
- }
- }
- generate( builder, output ) {
- const material = builder.context.material;
- const scope = this.scope;
- let node = null;
- if ( scope === MaterialNode.ALPHA_TEST ) {
- node = new MaterialReferenceNode( 'alphaTest', 'float' );
- } else if ( scope === MaterialNode.COLOR ) {
- const colorNode = new MaterialReferenceNode( 'color', 'color' );
- if ( material.map?.isTexture === true ) {
- //new MaterialReferenceNode( 'map', 'texture' )
- const map = new TextureNode( material.map );
- node = new OperatorNode( '*', colorNode, map );
- } else {
- node = colorNode;
- }
- } else if ( scope === MaterialNode.OPACITY ) {
- const opacityNode = new MaterialReferenceNode( 'opacity', 'float' );
- if ( material.alphaMap?.isTexture === true ) {
- node = new OperatorNode( '*', opacityNode, new MaterialReferenceNode( 'alphaMap', 'texture' ) );
- } else {
- node = opacityNode;
- }
- } else if ( scope === MaterialNode.ROUGHNESS ) {
- const roughnessNode = new MaterialReferenceNode( 'roughness', 'float' );
- if ( material.roughnessMap?.isTexture === true ) {
- node = new OperatorNode( '*', roughnessNode, new SplitNode( new TextureNode( material.roughnessMap ), 'g' ) );
- } else {
- node = roughnessNode;
- }
- } else if ( scope === MaterialNode.METALNESS ) {
- const metalnessNode = new MaterialReferenceNode( 'metalness', 'float' );
- if ( material.metalnessMap?.isTexture === true ) {
- node = new OperatorNode( '*', metalnessNode, new SplitNode( new TextureNode( material.metalnessMap ), 'b' ) );
- } else {
- node = metalnessNode;
- }
- } else if ( scope === MaterialNode.EMISSIVE ) {
- const emissiveNode = new MaterialReferenceNode( 'emissive', 'color' );
- if ( material.emissiveMap?.isTexture === true ) {
- node = new OperatorNode( '*', emissiveNode, new TextureNode( material.emissiveMap ) );
- } else {
- node = emissiveNode;
- }
- } else if ( scope === MaterialNode.ROTATION ) {
- node = new MaterialReferenceNode( 'rotation', 'float' );
- } else {
- const outputType = this.getNodeType( builder );
- node = new MaterialReferenceNode( scope, outputType );
- }
- return node.build( builder, output );
- }
- }
- export default MaterialNode;
|