浏览代码

Fix issue with lightInformationBlock #6012

David Catuhe 6 年之前
父节点
当前提交
a08d53f0e8

+ 2 - 0
Playground/babylon.d.txt

@@ -51888,6 +51888,8 @@ declare module BABYLON {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */

+ 2 - 0
dist/preview release/babylon.d.ts

@@ -52683,6 +52683,8 @@ declare module BABYLON {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */

文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/babylon.js


文件差异内容过多而无法显示
+ 25 - 12
dist/preview release/babylon.max.js


文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/babylon.max.js.map


+ 5 - 0
dist/preview release/babylon.module.d.ts

@@ -55074,6 +55074,7 @@ declare module "babylonjs/Materials/Node/nodeMaterialBuildStateSharedData" {
     import { InputBlock } from "babylonjs/Materials/Node/Blocks/Input/inputBlock";
     import { TextureBlock } from "babylonjs/Materials/Node/Blocks/Dual/textureBlock";
     import { ReflectionTextureBlock } from "babylonjs/Materials/Node/Blocks/Dual/reflectionTextureBlock";
+    import { Scene } from "babylonjs/scene";
     /**
      * Class used to store shared data between 2 NodeMaterialBuildState
      */
@@ -55142,6 +55143,8 @@ declare module "babylonjs/Materials/Node/nodeMaterialBuildStateSharedData" {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */
@@ -118428,6 +118431,8 @@ declare module BABYLON {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */

+ 2 - 0
dist/preview release/documentation.d.ts

@@ -52683,6 +52683,8 @@ declare module BABYLON {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */

文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/nodeEditor/babylon.nodeEditor.d.ts


文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/nodeEditor/babylon.nodeEditor.js


+ 18 - 5
dist/preview release/nodeEditor/babylon.nodeEditor.max.js

@@ -68356,7 +68356,7 @@ __webpack_require__.r(__webpack_exports__);
 var BlockTools = /** @class */ (function () {
     function BlockTools() {
     }
-    BlockTools.GetBlockFromString = function (data) {
+    BlockTools.GetBlockFromString = function (data, scene) {
         switch (data) {
             case "BonesBlock":
                 return new babylonjs_Materials_Node_Blocks_Fragment_alphaTestBlock__WEBPACK_IMPORTED_MODULE_0__["BonesBlock"]("Bones");
@@ -68423,7 +68423,9 @@ var BlockTools = /** @class */ (function () {
             case "ViewDirectionBlock":
                 return new babylonjs_Materials_Node_Blocks_Fragment_alphaTestBlock__WEBPACK_IMPORTED_MODULE_0__["ViewDirectionBlock"]("View direction");
             case "LightInformationBlock":
-                return new babylonjs_Materials_Node_Blocks_Fragment_alphaTestBlock__WEBPACK_IMPORTED_MODULE_0__["LightInformationBlock"]("Light information");
+                var lightInformationBlock = new babylonjs_Materials_Node_Blocks_Fragment_alphaTestBlock__WEBPACK_IMPORTED_MODULE_0__["LightInformationBlock"]("Light information");
+                lightInformationBlock.light = scene.lights.length ? scene.lights[0] : null;
+                return lightInformationBlock;
             case "MaxBlock":
                 return new babylonjs_Materials_Node_Blocks_Fragment_alphaTestBlock__WEBPACK_IMPORTED_MODULE_0__["MaxBlock"]("Max");
             case "MinBlock":
@@ -71440,7 +71442,6 @@ var PreviewManager = /** @class */ (function () {
         this._engine = new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Engine"](targetCanvas, true);
         this._scene = new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Scene"](this._engine);
         this._camera = new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["ArcRotateCamera"]("Camera", 0, 0.8, 4, babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Vector3"].Zero(), this._scene);
-        this._light = new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["HemisphericLight"]("light", new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Vector3"](0, 1, 0), this._scene);
         this._camera.lowerRadiusLimit = 3;
         this._camera.upperRadiusLimit = 10;
         this._camera.wheelPrecision = 20;
@@ -71476,6 +71477,10 @@ var PreviewManager = /** @class */ (function () {
             var mesh = _a[_i];
             mesh.material = this._material;
         }
+        // Light
+        if (!this._scene.lights.length) {
+            this._light = new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["HemisphericLight"]("light", new babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Vector3"](0, 1, 0), this._scene);
+        }
         // Framing
         this._camera.useFramingBehavior = true;
         var framingBehavior = this._camera.getBehaviorByName("Framing");
@@ -71505,6 +71510,11 @@ var PreviewManager = /** @class */ (function () {
                 }
             }
             this._meshes = [];
+            var lights = this._scene.lights.slice(0);
+            for (var _b = 0, lights_1 = lights; _b < lights_1.length; _b++) {
+                var light = lights_1[_b];
+                light.dispose();
+            }
             switch (this._globalState.previewMeshType) {
                 case _previewMeshType__WEBPACK_IMPORTED_MODULE_1__["PreviewMeshType"].Box:
                     this._meshes.push(babylonjs_Materials_Node_nodeMaterial__WEBPACK_IMPORTED_MODULE_0__["Mesh"].CreateBox("dummy-box", 2, this._scene));
@@ -71581,7 +71591,10 @@ var PreviewManager = /** @class */ (function () {
             var mesh = _a[_i];
             mesh.dispose();
         }
-        this._light.dispose();
+        if (this._light) {
+            this._light.dispose();
+        }
+        this._scene.dispose();
         this._engine.dispose();
     };
     return PreviewManager;
@@ -72719,7 +72732,7 @@ var GraphEditor = /** @class */ (function (_super) {
             nodeModel = this.addValueNode(data);
         }
         else {
-            var block = _blockTools__WEBPACK_IMPORTED_MODULE_19__["BlockTools"].GetBlockFromString(data);
+            var block = _blockTools__WEBPACK_IMPORTED_MODULE_19__["BlockTools"].GetBlockFromString(data, this.props.globalState.nodeMaterial.getScene());
             if (block) {
                 this._toAdd = [];
                 block.autoConfigure(this.props.globalState.nodeMaterial);

文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/nodeEditor/babylon.nodeEditor.max.js.map


文件差异内容过多而无法显示
+ 3 - 2
dist/preview release/nodeEditor/babylon.nodeEditor.module.d.ts


+ 5 - 0
dist/preview release/viewer/babylon.module.d.ts

@@ -55074,6 +55074,7 @@ declare module "babylonjs/Materials/Node/nodeMaterialBuildStateSharedData" {
     import { InputBlock } from "babylonjs/Materials/Node/Blocks/Input/inputBlock";
     import { TextureBlock } from "babylonjs/Materials/Node/Blocks/Dual/textureBlock";
     import { ReflectionTextureBlock } from "babylonjs/Materials/Node/Blocks/Dual/reflectionTextureBlock";
+    import { Scene } from "babylonjs/scene";
     /**
      * Class used to store shared data between 2 NodeMaterialBuildState
      */
@@ -55142,6 +55143,8 @@ declare module "babylonjs/Materials/Node/nodeMaterialBuildStateSharedData" {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */
@@ -118428,6 +118431,8 @@ declare module BABYLON {
         emitComments: boolean;
         /** Emit build activity */
         verbose: boolean;
+        /** Gets or sets the hosting scene */
+        scene: Scene;
         /**
          * Gets the compilation hints emitted at compilation time
          */

文件差异内容过多而无法显示
+ 5 - 5
dist/preview release/viewer/babylon.viewer.js


文件差异内容过多而无法显示
+ 1 - 1
dist/preview release/viewer/babylon.viewer.max.js


+ 5 - 2
nodeEditor/src/blockTools.ts

@@ -41,9 +41,10 @@ import { PerturbNormalBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/per
 import { LengthBlock } from 'babylonjs/Materials/Node/Blocks/lengthBlock';
 import { DistanceBlock } from 'babylonjs/Materials/Node/Blocks/distanceBlock';
 import { NegateBlock } from 'babylonjs/Materials/Node/Blocks/negateBlock';
+import { Scene } from 'babylonjs/scene';
 
 export class BlockTools {
-    public static GetBlockFromString(data: string) {
+    public static GetBlockFromString(data: string, scene: Scene) {
         switch (data) {
             case "BonesBlock":
                 return new BonesBlock("Bones");
@@ -110,7 +111,9 @@ export class BlockTools {
             case "ViewDirectionBlock":
                 return new ViewDirectionBlock("View direction");    
             case "LightInformationBlock":
-                return new LightInformationBlock("Light information");         
+                let lightInformationBlock = new LightInformationBlock("Light information");
+                lightInformationBlock.light = scene.lights.length ? scene.lights[0] : null;
+                return lightInformationBlock;
             case "MaxBlock":
                 return new MaxBlock("Max");       
             case "MinBlock":

+ 16 - 4
nodeEditor/src/components/preview/previewManager.ts

@@ -60,7 +60,6 @@ export class PreviewManager {
         this._engine = new Engine(targetCanvas, true);
         this._scene = new Scene(this._engine);
         this._camera = new ArcRotateCamera("Camera", 0, 0.8, 4, Vector3.Zero(), this._scene);
-        this._light = new HemisphericLight("light", new Vector3(0, 1, 0), this._scene);
 
         this._camera.lowerRadiusLimit = 3;
         this._camera.upperRadiusLimit = 10;
@@ -103,6 +102,11 @@ export class PreviewManager {
             mesh.material = this._material;
         }
 
+        // Light
+        if (!this._scene.lights.length) {
+            this._light = new HemisphericLight("light", new Vector3(0, 1, 0), this._scene);
+        }
+
         // Framing
         this._camera.useFramingBehavior = true;
 
@@ -133,13 +137,16 @@ export class PreviewManager {
 
             this._currentType = this._globalState.previewMeshType;
             if (this._meshes && this._meshes.length) {
-
                 for (var mesh of this._meshes) {
                     mesh.dispose();
                 }
             }
-
             this._meshes = [];
+
+            let lights = this._scene.lights.slice(0);
+            for (var light of lights) {
+                light.dispose();
+            }
         
             switch (this._globalState.previewMeshType) {
                 case PreviewMeshType.Box:
@@ -221,7 +228,12 @@ export class PreviewManager {
         for (var mesh of this._meshes) {
             mesh.dispose();
         }
-        this._light.dispose();
+
+        if (this._light) {
+            this._light.dispose();
+        }
+
+        this._scene.dispose();
         this._engine.dispose();
     }
 }

+ 1 - 1
nodeEditor/src/graphEditor.tsx

@@ -583,7 +583,7 @@ export class GraphEditor extends React.Component<IGraphEditorProps> {
         if (data.indexOf("Block") === -1) {
             nodeModel = this.addValueNode(data);
         } else {
-            let block = BlockTools.GetBlockFromString(data);   
+            let block = BlockTools.GetBlockFromString(data, this.props.globalState.nodeMaterial.getScene());   
             
             if (block) {                
                 this._toAdd = [];

+ 22 - 5
src/Materials/Node/Blocks/Vertex/lightInformationBlock.ts

@@ -69,15 +69,26 @@ export class LightInformationBlock extends NodeMaterialBlock {
             return;
         }
 
-        if (!this.light || !this.light.isEnabled) {
+        if (this.light && this.light.isDisposed) {
+            this.light = null;
+        }
+
+        let light = this.light;
+        let scene = nodeMaterial.getScene();
+
+        if (!light && scene.lights.length) {
+            light = scene.lights[0];
+        }
+
+        if (!light || !light.isEnabled) {
             effect.setFloat3(this._lightDataDefineName, 0, 0, 0);
             effect.setFloat3(this._lightColorDefineName, 0, 0, 0);
             return;
         }
 
-        this.light.transferToNodeMaterialEffect(effect, this._lightDataDefineName);
+        light.transferToNodeMaterialEffect(effect, this._lightDataDefineName);
 
-        effect.setColor3(this._lightColorDefineName, this.light.diffuse);
+        effect.setColor3(this._lightColorDefineName, light.diffuse);
     }
 
     protected _buildBlock(state: NodeMaterialBuildState) {
@@ -88,7 +99,13 @@ export class LightInformationBlock extends NodeMaterialBlock {
         let direction = this.direction;
         let color = this.color;
 
-        if (!this.light) {
+        let light = this.light;
+
+        if (!light && state.sharedData.scene.lights.length) {
+            light = state.sharedData.scene.lights[0];
+        }
+
+        if (!light) {
             state.compilationString += this._declareOutput(direction, state) + ` = vec3(0.);\r\n`;
             state.compilationString += this._declareOutput(color, state) + ` = vec3(0.);\r\n`;
         } else {
@@ -98,7 +115,7 @@ export class LightInformationBlock extends NodeMaterialBlock {
             this._lightColorDefineName = state._getFreeDefineName("lightColor");
             state._emitUniformFromString(this._lightColorDefineName, "vec3");
 
-            if (this.light instanceof PointLight) {
+            if (light instanceof PointLight) {
                 state.compilationString += this._declareOutput(direction, state) + ` = normalize(${this._lightDataDefineName} - ${this.worldPosition.associatedVariableName}.xyz);\r\n`;
             } else {
                 state.compilationString += this._declareOutput(direction, state) + ` = ${this._lightDataDefineName};\r\n`;

+ 1 - 0
src/Materials/Node/nodeMaterial.ts

@@ -537,6 +537,7 @@ export class NodeMaterial extends PushMaterial {
         this._sharedData.buildId = this._buildId;
         this._sharedData.emitComments = this._options.emitComments;
         this._sharedData.verbose = verbose;
+        this._sharedData.scene = this.getScene();
 
         // Initialize blocks
         let vertexNodes: NodeMaterialBlock[] = [];

+ 4 - 0
src/Materials/Node/nodeMaterialBuildStateSharedData.ts

@@ -3,6 +3,7 @@ import { NodeMaterialBlock } from './nodeMaterialBlock';
 import { InputBlock } from './Blocks/Input/inputBlock';
 import { TextureBlock } from './Blocks/Dual/textureBlock';
 import { ReflectionTextureBlock } from './Blocks/Dual/reflectionTextureBlock';
+import { Scene } from '../../scene';
 
 /**
  * Class used to store shared data between 2 NodeMaterialBuildState
@@ -85,6 +86,9 @@ export class NodeMaterialBuildStateSharedData {
     /** Emit build activity */
     public verbose: boolean;
 
+    /** Gets or sets the hosting scene */
+    public scene: Scene;
+
     /**
      * Gets the compilation hints emitted at compilation time
      */