瀏覽代碼

Add the anisotropy NME block

Popov72 5 年之前
父節點
當前提交
e23ff71977

+ 5 - 2
nodeEditor/src/blockTools.ts

@@ -65,7 +65,8 @@ 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 { ReflectivityBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/reflectivityBlockBlock';
+import { ReflectivityBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/reflectivityBlock';
+import { AnisotropyBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/PBR/anisotropyBlock';
 
 export class BlockTools {
     public static GetBlockFromString(data: string, scene: Scene, nodeMaterial: NodeMaterial) {
@@ -444,7 +445,9 @@ export class BlockTools {
                 return new AmbientOcclusionBlock("AmbientOcclusion");
             case "ReflectivityBlock":
                 return new ReflectivityBlock("Reflectivity");
-            }
+            case "AnisotropyBlock":
+                return new AnisotropyBlock("Anisotropy");
+        }
 
         return null;
     }

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

@@ -120,7 +120,8 @@ export class NodeListComponent extends React.Component<INodeListComponentProps,
         "PBRMetallicRoughnessBlock": "PBR metallic/roughness material",
         "SheenBlock": "Sheen block",
         "AmbientOcclusionBlock": "Ambient occlusion block",
-        "ReflectivityBlock": "Reflectivity block"
+        "ReflectivityBlock": "Reflectivity block",
+        "AnisotropyBlock": "Anisotropy block"
     };
 
     constructor(props: INodeListComponentProps) {
@@ -149,7 +150,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", "ReflectivityBlock", "SheenBlock"],
+            PBR: ["PBRMetallicRoughnessBlock", "AmbientOcclusionBlock", "AnisotropyBlock", "ReflectivityBlock", "SheenBlock"],
             Range: ["ClampBlock", "RemapBlock", "NormalizeBlock"],
             Round: ["RoundBlock", "CeilingBlock", "FloorBlock"],
             Scene: ["FogBlock", "CameraPositionBlock", "FogColorBlock", "ImageProcessingBlock", "LightBlock", "LightInformationBlock", "ViewDirectionBlock"],

+ 89 - 0
src/Materials/Node/Blocks/Fragment/PBR/anisotropyBlock.ts

@@ -0,0 +1,89 @@
+import { NodeMaterial, NodeMaterialDefines } from '../../../nodeMaterial';
+import { NodeMaterialBlock } from '../../../nodeMaterialBlock';
+import { NodeMaterialBlockConnectionPointTypes } from '../../../Enums/nodeMaterialBlockConnectionPointTypes';
+import { NodeMaterialBuildState } from '../../../nodeMaterialBuildState';
+import { NodeMaterialConnectionPoint, NodeMaterialConnectionPointDirection } from '../../../nodeMaterialBlockConnectionPoint';
+import { NodeMaterialBlockTargets } from '../../../Enums/nodeMaterialBlockTargets';
+import { _TypeStore } from '../../../../../Misc/typeStore';
+import { AbstractMesh } from '../../../../../Meshes/abstractMesh';
+import { NodeMaterialConnectionPointCustomObject } from "../../../nodeMaterialConnectionPointCustomObject";
+
+export class AnisotropyBlock extends NodeMaterialBlock {
+
+    public constructor(name: string) {
+        super(name, NodeMaterialBlockTargets.Fragment);
+
+        this.registerInput("intensity", NodeMaterialBlockConnectionPointTypes.Float, true, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("direction", NodeMaterialBlockConnectionPointTypes.Vector2, true, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("texture", NodeMaterialBlockConnectionPointTypes.Color3, true, NodeMaterialBlockTargets.Fragment);
+
+        this.registerOutput("anisotropy", NodeMaterialBlockConnectionPointTypes.Object, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("anisotropy", this, NodeMaterialConnectionPointDirection.Output, AnisotropyBlock, "AnisotropyBlock"));
+    }
+
+    /**
+     * Initialize the block and prepare the context for build
+     * @param state defines the state that will be used for the build
+     */
+    public initialize(state: NodeMaterialBuildState) {
+        state._excludeVariableName("anisotropicOut");
+        state._excludeVariableName("TBN");
+    }
+
+    /**
+     * Gets the current class name
+     * @returns the class name
+     */
+    public getClassName() {
+        return "AnisotropyBlock";
+    }
+
+    public get intensity(): NodeMaterialConnectionPoint {
+        return this._inputs[0];
+    }
+
+    public get direction(): NodeMaterialConnectionPoint {
+        return this._inputs[1];
+    }
+
+    public get texture(): NodeMaterialConnectionPoint {
+        return this._inputs[2];
+    }
+
+    public get anisotropy(): NodeMaterialConnectionPoint {
+        return this._outputs[0];
+    }
+
+    public getCode(): string {
+        let code = `anisotropicOutParams anisotropicOut;\r\n`;
+
+        const intensity = this.intensity.isConnected ? this.intensity.associatedVariableName : "1.0";
+        const direction = this.direction.isConnected ? this.direction.associatedVariableName : "vec2(1., 0.)";
+        const texture = this.texture.isConnected ? this.texture.associatedVariableName : "vec3(0.)";
+
+        code += `anisotropicBlock(
+                vec3(${direction}, ${intensity}),
+            #ifdef ANISOTROPIC_TEXTURE
+                ${texture},
+            #endif
+                TBN,
+                normalW,
+                viewDirectionW,
+                anisotropicOut
+            );\r\n`;
+
+        return code;
+    }
+
+    public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
+        defines.setValue("ANISOTROPIC", true);
+        defines.setValue("ANISOTROPIC_TEXTURE", this.texture.isConnected);
+    }
+
+    protected _buildBlock(state: NodeMaterialBuildState) {
+        super._buildBlock(state);
+
+        return this;
+    }
+}
+
+_TypeStore.RegisteredTypes["BABYLON.AnisotropyBlock"] = AnisotropyBlock;

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

@@ -2,3 +2,4 @@ export * from "./pbrMetallicRoughnessBlock";
 export * from "./sheenBlock";
 export * from "./ambientOcclusionBlock";
 export * from "./reflectivityBlock";
+export * from "./anisotropyBlock";

+ 15 - 4
src/Materials/Node/Blocks/Fragment/PBR/pbrMetallicRoughnessBlock.ts

@@ -24,6 +24,7 @@ import { BaseTexture } from '../../../../Textures/baseTexture';
 import { Engine } from '../../../../../Engines/engine';
 import { BRDFTextureTools } from '../../../../../Misc/brdfTextureTools';
 import { MaterialFlags } from '../../../../materialFlags';
+import { AnisotropyBlock } from './anisotropyBlock';
 
 export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
     private _lightId: number;
@@ -55,7 +56,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         this.registerInput("sheen", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("sheen", this, NodeMaterialConnectionPointDirection.Input, SheenBlock, "SheenBlock"));
         this.registerInput("clearCoat", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
         this.registerInput("subSurface", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
-        this.registerInput("anisotropy", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment);
+        this.registerInput("anisotropy", NodeMaterialBlockConnectionPointTypes.Object, true, NodeMaterialBlockTargets.Fragment, new NodeMaterialConnectionPointCustomObject("anisotropy", this, NodeMaterialConnectionPointDirection.Input, AnisotropyBlock, "AnisotropyBlock"));
 
         this.registerOutput("ambient", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
         this.registerOutput("diffuse", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
@@ -183,7 +184,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         return this._inputs[6];
     }
 
-    public get reflectivity(): NodeMaterialConnectionPoint {
+    public get reflectivityParams(): NodeMaterialConnectionPoint {
         return this._inputs[7];
     }
 
@@ -302,10 +303,15 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         aoBlock?.prepareDefines(mesh, nodeMaterial, defines);
 
         // Reflectivity
-        const metalRoughTextBlock = this.reflectivity.connectedPoint?.ownerBlock as ReflectivityBlock;
+        const metalRoughTextBlock = this.reflectivityParams.connectedPoint?.ownerBlock as ReflectivityBlock;
 
         metalRoughTextBlock?.prepareDefines(mesh, nodeMaterial, defines);
 
+        // Anisotropy
+        const anisotropyBlock = this.anisotropyParams.connectedPoint?.ownerBlock as AnisotropyBlock;
+
+        anisotropyBlock?.prepareDefines(mesh, nodeMaterial, defines);
+
         // Rendering
         defines.setValue("RADIANCEOVERALPHA", this.useRadianceOverAlpha);
         defines.setValue("SPECULAROVERALPHA", this.useSpecularOverAlpha);
@@ -580,7 +586,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         // _____________________________ Reflectivity _______________________________
         const aoIntensity = aoBlock?.intensity.isConnected ? aoBlock.intensity.associatedVariableName : "1.";
 
-        state.compilationString += (this.reflectivity.connectedPoint?.ownerBlock as Nullable<ReflectivityBlock>)?.getCode(aoIntensity) ?? "";
+        state.compilationString += (this.reflectivityParams.connectedPoint?.ownerBlock as Nullable<ReflectivityBlock>)?.getCode(aoIntensity) ?? "";
 
         // _____________________________ Geometry info _________________________________
         state.compilationString += state._emitCodeFromInclude("pbrBlockGeometryInfo", comments);
@@ -589,6 +595,11 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         state.compilationString += state._emitCodeFromInclude("pbrBlockDirectLighting", comments);
 
         // _____________________________ Anisotropy _______________________________________
+        const anisotropyBlock = this.anisotropyParams.isConnected ? this.anisotropyParams.connectedPoint?.ownerBlock as AnisotropyBlock : null;
+
+        if (anisotropyBlock) {
+            state.compilationString += anisotropyBlock.getCode();
+        }
 
         /*if (this.light) {
             state.compilationString += state._emitCodeFromInclude("lightFragment", comments, {