Ver código fonte

Make a separate reflectivity block

Popov72 5 anos atrás
pai
commit
950c643df2

+ 3 - 3
nodeEditor/src/blockTools.ts

@@ -65,7 +65,7 @@ import { DesaturateBlock } from 'babylonjs/Materials/Node/Blocks/desaturateBlock
 import { PBRMetallicRoughnessBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/pbrMetallicRoughnessBlock';
 import { SheenBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/sheenBlock';
 import { AmbientOcclusionBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/ambientOcclusionBlock';
-import { MetallicRoughnessTextureBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/metallicRoughnessTextureBlock';
+import { ReflectivityBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/reflectivityBlockBlock';
 
 export class BlockTools {
     public static GetBlockFromString(data: string, scene: Scene, nodeMaterial: NodeMaterial) {
@@ -442,8 +442,8 @@ export class BlockTools {
                 return new SheenBlock("Sheen");
             case "AmbientOcclusionBlock":
                 return new AmbientOcclusionBlock("AmbientOcclusion");
-            case "MetallicRoughnessTextureBlock":
-                return new MetallicRoughnessTextureBlock("MetallicRoughness texture");
+            case "ReflectivityBlock":
+                return new ReflectivityBlock("Reflectivity");
             }
 
         return null;

+ 2 - 2
nodeEditor/src/components/nodeList/nodeListComponent.tsx

@@ -120,7 +120,7 @@ export class NodeListComponent extends React.Component<INodeListComponentProps,
         "PBRMetallicRoughnessBlock": "PBR metallic/roughness material",
         "SheenBlock": "Sheen block",
         "AmbientOcclusionBlock": "Ambient occlusion block",
-        "MetallicRoughnessTextureBlock": "Metallic/Roughness texture block"
+        "ReflectivityBlock": "Reflectivity block"
     };
 
     constructor(props: INodeListComponentProps) {
@@ -149,7 +149,7 @@ export class NodeListComponent extends React.Component<INodeListComponentProps,
             Mesh: ["InstancesBlock", "PositionBlock", "UVBlock", "ColorBlock", "NormalBlock", "PerturbNormalBlock", "NormalBlendBlock" , "TangentBlock", "MatrixIndicesBlock", "MatrixWeightsBlock", "WorldPositionBlock", "WorldNormalBlock", "WorldTangentBlock", "FrontFacingBlock"],
             Noises: ["RandomNumberBlock", "SimplexPerlin3DBlock", "WorleyNoise3DBlock"],
             Output_Nodes: ["VertexOutputBlock", "FragmentOutputBlock", "DiscardBlock"],
-            PBR: ["PBRMetallicRoughnessBlock", "AmbientOcclusionBlock", "MetallicRoughnessTextureBlock", "SheenBlock"],
+            PBR: ["PBRMetallicRoughnessBlock", "AmbientOcclusionBlock", "ReflectivityBlock", "SheenBlock"],
             Range: ["ClampBlock", "RemapBlock", "NormalizeBlock"],
             Round: ["RoundBlock", "CeilingBlock", "FloorBlock"],
             Scene: ["FogBlock", "CameraPositionBlock", "FogColorBlock", "ImageProcessingBlock", "LightBlock", "LightInformationBlock", "ViewDirectionBlock"],

+ 1 - 1
src/Materials/Node/Blocks/Fragment/PBR/index.ts

@@ -1,4 +1,4 @@
 export * from "./pbrMetallicRoughnessBlock";
 export * from "./sheenBlock";
 export * from "./ambientOcclusionBlock";
-export * from "./metallicRoughnessTextureBlock";
+export * from "./reflectivityBlock";

+ 13 - 26
src/Materials/Node/Blocks/Fragment/PBR/pbrMetallicRoughnessBlock.ts

@@ -19,7 +19,7 @@ import { editableInPropertyPage, PropertyTypeForEdition } from "../../../nodeMat
 import { NodeMaterialConnectionPointCustomObject } from "../../../nodeMaterialConnectionPointCustomObject";
 import { AmbientOcclusionBlock } from './ambientOcclusionBlock';
 import { SheenBlock } from './sheenBlock';
-import { MetallicRoughnessTextureBlock } from './metallicRoughnessTextureBlock';
+import { ReflectivityBlock } from './reflectivityBlock';
 import { BaseTexture } from '../../../../Textures/baseTexture';
 import { Engine } from '../../../../../Engines/engine';
 import { BRDFTextureTools } from '../../../../../Misc/brdfTextureTools';
@@ -47,10 +47,8 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         this.registerInput("cameraPosition", NodeMaterialBlockConnectionPointTypes.Vector3, false, NodeMaterialBlockTargets.Fragment);
         this.registerInput("baseColor", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
         this.registerInput("baseTexture", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
-        this.registerInput("metallic", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
-        this.registerInput("roughness", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
-        this.registerInput("metalRoughText", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("metalRoughText", this, NodeMaterialConnectionPointDirection.Input, MetallicRoughnessTextureBlock, "MetallicRoughnessTextureBlock", "rgba"));
         this.registerInput("opacityTexture", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("reflectivity", NodeMaterialBlockConnectionPointTypes.Object, false, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("reflectivity", this, NodeMaterialConnectionPointDirection.Input, ReflectivityBlock, "ReflectivityBlock"));
         this.registerInput("ambientColor", NodeMaterialBlockConnectionPointTypes.Color3, true, NodeMaterialBlockTargets.Fragment);
         this.registerInput("ambientOcclusion", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("ambientOcclusion", this, NodeMaterialConnectionPointDirection.Input, AmbientOcclusionBlock, "AOBlock"));
         this.registerInput("reflection", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
@@ -181,48 +179,40 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         return this._inputs[5];
     }
 
-    public get metallic(): NodeMaterialConnectionPoint {
+    public get opacityTexture(): NodeMaterialConnectionPoint {
         return this._inputs[6];
     }
 
-    public get roughness(): NodeMaterialConnectionPoint {
+    public get reflectivity(): NodeMaterialConnectionPoint {
         return this._inputs[7];
     }
 
-    public get metalRoughTexture(): NodeMaterialConnectionPoint {
-        return this._inputs[8];
-    }
-
-    public get opacityTexture(): NodeMaterialConnectionPoint {
-        return this._inputs[9];
-    }
-
     public get ambientColor(): NodeMaterialConnectionPoint {
-        return this._inputs[10];
+        return this._inputs[8];
     }
 
     public get ambientOcclusionParams(): NodeMaterialConnectionPoint {
-        return this._inputs[11];
+        return this._inputs[9];
     }
 
     public get reflectionParams(): NodeMaterialConnectionPoint {
-        return this._inputs[12];
+        return this._inputs[10];
     }
 
     public get sheenParams(): NodeMaterialConnectionPoint {
-        return this._inputs[13];
+        return this._inputs[11];
     }
 
     public get clearcoatParams(): NodeMaterialConnectionPoint {
-        return this._inputs[14];
+        return this._inputs[12];
     }
 
     public get subSurfaceParams(): NodeMaterialConnectionPoint {
-        return this._inputs[15];
+        return this._inputs[13];
     }
 
     public get anisotropyParams(): NodeMaterialConnectionPoint {
-        return this._inputs[16];
+        return this._inputs[14];
     }
 
     public get ambient(): NodeMaterialConnectionPoint {
@@ -312,9 +302,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         aoBlock?.prepareDefines(mesh, nodeMaterial, defines);
 
         // Reflectivity
-        const metalRoughTextBlock = this.metalRoughTexture.connectedPoint?.ownerBlock as Nullable<MetallicRoughnessTextureBlock>;
-
-        defines.setValue("REFLECTIVITY", this.metalRoughTexture.isConnected);
+        const metalRoughTextBlock = this.reflectivity.connectedPoint?.ownerBlock as ReflectivityBlock;
 
         metalRoughTextBlock?.prepareDefines(mesh, nodeMaterial, defines);
 
@@ -592,8 +580,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         // _____________________________ Reflectivity _______________________________
         const aoIntensity = aoBlock?.intensity.isConnected ? aoBlock.intensity.associatedVariableName : "1.";
 
-        state.compilationString += MetallicRoughnessTextureBlock.getCode(this.metalRoughTexture.isConnected ? this.metalRoughTexture.connectedPoint?.ownerBlock as MetallicRoughnessTextureBlock : null,
-            this.metallic.associatedVariableName, this.roughness.associatedVariableName, aoIntensity);
+        state.compilationString += (this.reflectivity.connectedPoint?.ownerBlock as Nullable<ReflectivityBlock>)?.getCode(aoIntensity) ?? "";
 
         // _____________________________ Geometry info _________________________________
         state.compilationString += state._emitCodeFromInclude("pbrBlockGeometryInfo", comments);

+ 41 - 26
src/Materials/Node/Blocks/Fragment/PBR/metallicRoughnessTextureBlock.ts

@@ -1,16 +1,15 @@
 import { NodeMaterial, NodeMaterialDefines } from '../../../nodeMaterial';
 import { NodeMaterialBuildState } from '../../../nodeMaterialBuildState';
+import { NodeMaterialBlock } from '../../../nodeMaterialBlock';
 import { _TypeStore } from '../../../../../Misc/typeStore';
 import { editableInPropertyPage, PropertyTypeForEdition } from "../../../nodeMaterialDecorator";
 import { AbstractMesh } from '../../../../../Meshes/abstractMesh';
-import { TextureBlock } from '../../Dual/textureBlock';
-import { Nullable } from '../../../../../types';
 import { NodeMaterialBlockConnectionPointTypes } from '../../../Enums/nodeMaterialBlockConnectionPointTypes';
 import { NodeMaterialBlockTargets } from '../../../Enums/nodeMaterialBlockTargets';
 import { NodeMaterialConnectionPointCustomObject } from "../../../nodeMaterialConnectionPointCustomObject";
 import { NodeMaterialConnectionPoint, NodeMaterialConnectionPointDirection } from '../../../nodeMaterialBlockConnectionPoint';
 
-export class MetallicRoughnessTextureBlock extends TextureBlock {
+export class ReflectivityBlock extends NodeMaterialBlock {
 
     @editableInPropertyPage("AO from red channel", PropertyTypeForEdition.Boolean, "METALLIC WORKFLOW")
     public useAmbientOcclusionFromMetallicTextureRed: boolean = false;
@@ -28,20 +27,17 @@ export class MetallicRoughnessTextureBlock extends TextureBlock {
     public useMetallicF0FactorFromMetallicTexture: boolean = false;
 
     /**
-     * Create a new MetallicRoughnessTextureBlock
+     * Create a new ReflectivityBlock
      * @param name defines the block name
      */
     public constructor(name: string) {
-        super(name);
+        super(name, NodeMaterialBlockTargets.Fragment);
 
-        this._outputs = [];
+        this.registerInput("metallic", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("roughness", NodeMaterialBlockConnectionPointTypes.Float, false, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, true, NodeMaterialBlockTargets.Fragment);
 
-        this.registerOutput("rgba", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Neutral, new NodeMaterialConnectionPointCustomObject("rgba", this, NodeMaterialConnectionPointDirection.Output, MetallicRoughnessTextureBlock, "MetallicRoughnessTextureBlock", "metalRoughText"));
-        this.registerOutput("rgb", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Neutral);
-        this.registerOutput("r", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
-        this.registerOutput("g", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
-        this.registerOutput("b", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
-        this.registerOutput("a", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Neutral);
+        this.registerOutput("reflectivity", NodeMaterialBlockConnectionPointTypes.Object, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("reflectivity", this, NodeMaterialConnectionPointDirection.Output, ReflectivityBlock, "ReflectivityBlock"));
     }
 
     /**
@@ -60,31 +56,35 @@ export class MetallicRoughnessTextureBlock extends TextureBlock {
      * @returns the class name
      */
     public getClassName() {
-        return "MetallicRoughnessTextureBlock";
+        return "ReflectivityBlock";
+    }
+
+    public get metallic(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    public get roughness(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    public get rgba(): NodeMaterialConnectionPoint {
+        return this._inputs[2];
     }
 
     /**
      * Gets the rgba output component
      */
-    public get metalRoughTexture(): NodeMaterialConnectionPoint {
+    public get reflectivity(): NodeMaterialConnectionPoint {
         return this._outputs[0];
     }
 
-    public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
-        defines.setValue("AOSTOREINMETALMAPRED", this.useAmbientOcclusionFromMetallicTextureRed);
-        defines.setValue("METALLNESSSTOREINMETALMAPBLUE", this.useMetallnessFromMetallicTextureBlue);
-        defines.setValue("ROUGHNESSSTOREINMETALMAPALPHA", this.useRoughnessFromMetallicTextureAlpha);
-        defines.setValue("ROUGHNESSSTOREINMETALMAPGREEN",  !this.useRoughnessFromMetallicTextureAlpha && this.useRoughnessFromMetallicTextureGreen);
-        defines.setValue("METALLICF0FACTORFROMMETALLICMAP", this.useMetallicF0FactorFromMetallicTexture);
-    }
-
-    public static getCode(block: Nullable<MetallicRoughnessTextureBlock>, metallicVarName: string, roughnessVarName: string, aoIntensityVarName: string): string {
-        const metalRoughTexture = block?.metalRoughTexture.connectedBlocks && block?.metalRoughTexture.connectedBlocks.length > 0 ? block.metalRoughTexture.associatedVariableName : null;
+    public getCode(aoIntensityVarName: string): string {
+        const metalRoughTexture = this.rgba.isConnected ? this.rgba.connectedPoint?.associatedVariableName : null;
 
         let code = `vec3 baseColor = surfaceAlbedo;\r\nreflectivityOutParams reflectivityOut;\r\n`;
 
         code += `reflectivityBlock(
-            vec4(${metallicVarName}, ${roughnessVarName}, 0., 0.04),
+            vec4(${this.metallic.associatedVariableName}, ${this.roughness.associatedVariableName}, 0., 0.04),
         #ifdef METALLICWORKFLOW
             surfaceAlbedo,
         #endif
@@ -113,6 +113,21 @@ export class MetallicRoughnessTextureBlock extends TextureBlock {
 
         return code;
     }
+
+    public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
+        defines.setValue("REFLECTIVITY", this.rgba.isConnected);
+        defines.setValue("AOSTOREINMETALMAPRED", this.useAmbientOcclusionFromMetallicTextureRed);
+        defines.setValue("METALLNESSSTOREINMETALMAPBLUE", this.useMetallnessFromMetallicTextureBlue);
+        defines.setValue("ROUGHNESSSTOREINMETALMAPALPHA", this.useRoughnessFromMetallicTextureAlpha);
+        defines.setValue("ROUGHNESSSTOREINMETALMAPGREEN",  !this.useRoughnessFromMetallicTextureAlpha && this.useRoughnessFromMetallicTextureGreen);
+        defines.setValue("METALLICF0FACTORFROMMETALLICMAP", this.useMetallicF0FactorFromMetallicTexture);
+    }
+
+    protected _buildBlock(state: NodeMaterialBuildState) {
+        super._buildBlock(state);
+
+        return this;
+    }
 }
 
-_TypeStore.RegisteredTypes["BABYLON.MetallicRoughnessTextureBlock"] = MetallicRoughnessTextureBlock;
+_TypeStore.RegisteredTypes["BABYLON.ReflectivityBlock"] = ReflectivityBlock;