瀏覽代碼

factor out material logic, keep private variables

Trevor Baron 6 年之前
父節點
當前提交
e9afc85bf1

+ 5 - 4
src/Engines/Extensions/engine.multiview.ts

@@ -3,9 +3,10 @@ import { Engine } from "../../Engines/engine";
 import { Scene } from "../../scene";
 import { Scene } from "../../scene";
 import { InternalTexture } from '../../Materials/Textures/internalTexture';
 import { InternalTexture } from '../../Materials/Textures/internalTexture';
 import { Nullable } from '../../types';
 import { Nullable } from '../../types';
-import { RenderTargetTexture, MultiviewRenderTarget } from '../../Materials/Textures/renderTargetTexture';
+import { RenderTargetTexture } from '../../Materials/Textures/renderTargetTexture';
 import { Matrix, Tmp, Frustum } from '../../Maths/math';
 import { Matrix, Tmp, Frustum } from '../../Maths/math';
 import { UniformBuffer } from '../../Materials/uniformBuffer';
 import { UniformBuffer } from '../../Materials/uniformBuffer';
+import { MultiviewRenderTarget } from '../../Materials/Textures/MultiviewRenderTarget';
 
 
 declare module "../../Engines/engine" {
 declare module "../../Engines/engine" {
     export interface Engine {
     export interface Engine {
@@ -116,7 +117,7 @@ declare module "../../scene" {
 Scene.prototype._transformMatrixR = Matrix.Zero();
 Scene.prototype._transformMatrixR = Matrix.Zero();
 Scene.prototype._multiviewSceneUbo = null;
 Scene.prototype._multiviewSceneUbo = null;
 Scene.prototype._createMultiviewUbo = function() {
 Scene.prototype._createMultiviewUbo = function() {
-    this._multiviewSceneUbo = new UniformBuffer(this._engine, undefined, true);
+    this._multiviewSceneUbo = new UniformBuffer(this.getEngine(), undefined, true);
     this._multiviewSceneUbo.addUniform("viewProjection", 16);
     this._multiviewSceneUbo.addUniform("viewProjection", 16);
     this._multiviewSceneUbo.addUniform("viewProjectionR", 16);
     this._multiviewSceneUbo.addUniform("viewProjectionR", 16);
     this._multiviewSceneUbo.addUniform("view", 16);
     this._multiviewSceneUbo.addUniform("view", 16);
@@ -132,7 +133,7 @@ Scene.prototype._updateMultiviewUbo = function(viewR?: Matrix, projectionR?: Mat
     }
     }
 
 
     if (this._multiviewSceneUbo) {
     if (this._multiviewSceneUbo) {
-        this._multiviewSceneUbo.updateMatrix("viewProjection", this._transformMatrix);
+        this._multiviewSceneUbo.updateMatrix("viewProjection", this.getTransformMatrix());
         this._multiviewSceneUbo.updateMatrix("viewProjectionR", this._transformMatrixR);
         this._multiviewSceneUbo.updateMatrix("viewProjectionR", this._transformMatrixR);
         this._multiviewSceneUbo.updateMatrix("view", this._viewMatrix);
         this._multiviewSceneUbo.updateMatrix("view", this._viewMatrix);
         this._multiviewSceneUbo.update();
         this._multiviewSceneUbo.update();
@@ -157,7 +158,7 @@ Scene.prototype._renderMultiviewToSingleView = function(camera: Camera) {
 
 
     // Consume the multiview texture through a shader for each eye
     // Consume the multiview texture through a shader for each eye
     for (var index = 0; index < camera._rigCameras.length; index++) {
     for (var index = 0; index < camera._rigCameras.length; index++) {
-        var engine = this._engine;
+        var engine = this.getEngine();
         this._activeCamera = camera._rigCameras[index];
         this._activeCamera = camera._rigCameras[index];
         engine.setViewport(this._activeCamera.viewport);
         engine.setViewport(this._activeCamera.viewport);
         if (this.postProcessManager) {
         if (this.postProcessManager) {

+ 2 - 1
src/Engines/Extensions/index.ts

@@ -1,2 +1,3 @@
 export * from "./engine.occlusionQuery";
 export * from "./engine.occlusionQuery";
-export * from "./engine.transformFeedback";
+export * from "./engine.transformFeedback";
+export * from "./engine.multiview";

+ 1 - 7
src/Materials/Background/backgroundMaterial.ts

@@ -677,13 +677,7 @@ export class BackgroundMaterial extends PushMaterial {
         defines._needNormals = true;
         defines._needNormals = true;
 
 
         // Multiview
         // Multiview
-        if (scene.activeCamera) {
-            var previousMultiview = defines.MULTIVIEW;
-            defines.MULTIVIEW = (scene.activeCamera.outputRenderTarget !== null && scene.activeCamera.outputRenderTarget.getViewCount() > 1);
-            if (defines.MULTIVIEW != previousMultiview) {
-                defines.markAsUnprocessed();
-            }
-        }
+        MaterialHelper.PrepareDefinesForMultiview(scene, defines);
 
 
         // Textures
         // Textures
         if (defines._areTexturesDirty) {
         if (defines._areTexturesDirty) {

+ 1 - 7
src/Materials/PBR/pbrBaseMaterial.ts

@@ -1254,13 +1254,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         defines._needNormals = true;
         defines._needNormals = true;
 
 
         // Multiview
         // Multiview
-        if (scene.activeCamera) {
-            var previousMultiview = defines.MULTIVIEW;
-            defines.MULTIVIEW = (scene.activeCamera.outputRenderTarget !== null && scene.activeCamera.outputRenderTarget.getViewCount() > 1);
-            if (defines.MULTIVIEW != previousMultiview) {
-                defines.markAsUnprocessed();
-            }
-        }
+        MaterialHelper.PrepareDefinesForMultiview(scene, defines);
 
 
         // Textures
         // Textures
         defines.METALLICWORKFLOW = this.isMetallicWorkflow();
         defines.METALLICWORKFLOW = this.isMetallicWorkflow();

+ 40 - 0
src/Materials/Textures/MultiviewRenderTarget.ts

@@ -0,0 +1,40 @@
+import { RenderTargetTexture } from '../Textures/renderTargetTexture';
+import { Scene } from '../../scene';
+import { InternalTexture } from '../Textures/internalTexture';
+
+/**
+ * Renders to multiple views with a single draw call
+ * @see https://www.khronos.org/registry/webgl/extensions/WEBGL_multiview/
+ */
+export class MultiviewRenderTarget extends RenderTargetTexture {
+    /**
+     * Creates a multiview render target
+     * @param scene scene used with the render target
+     * @param size the size of the render target (used for each view)
+     */
+    constructor(scene: Scene, size: number | { width: number, height: number } | { ratio: number } = 512) {
+        super("multiview rtt", size, scene, false, true, InternalTexture.DATASOURCE_UNKNOWN, false, undefined, false, false, true, undefined, true);
+        var internalTexture = scene.getEngine().createMultiviewRenderTargetTexture(this.getRenderWidth(), this.getRenderHeight());
+        internalTexture.isMultiview = true;
+        this._texture = internalTexture;
+    }
+
+    /**
+     * @hidden
+     * @param faceIndex the face index, if its a cube texture
+     */
+    public _bindFrameBuffer(faceIndex: number = 0) {
+        if (!this._texture) {
+            return;
+        }
+        this.getScene()!.getEngine().bindMultiviewFramebuffer(this._texture);
+    }
+
+    /**
+     * Gets the number of views the corresponding to the texture (eg. a MultiviewRenderTarget will have > 1)
+     * @returns the view count
+     */
+    public getViewCount() {
+        return 2;
+    }
+}

+ 1 - 38
src/Materials/Textures/renderTargetTexture.ts

@@ -1001,41 +1001,4 @@ export class RenderTargetTexture extends Texture {
 
 
 Texture._CreateRenderTargetTexture = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean) => {
 Texture._CreateRenderTargetTexture = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean) => {
     return new RenderTargetTexture(name, renderTargetSize, scene, generateMipMaps);
     return new RenderTargetTexture(name, renderTargetSize, scene, generateMipMaps);
-};
-
-/**
- * Renders to multiple views with a single draw call
- * @see https://www.khronos.org/registry/webgl/extensions/WEBGL_multiview/
- */
-export class MultiviewRenderTarget extends RenderTargetTexture {
-    /**
-     * Creates a multiview render target
-     * @param scene scene used with the render target
-     * @param size the size of the render target (used for each view)
-     */
-    constructor(scene: Scene, size: number | { width: number, height: number } | { ratio: number } = 512) {
-        super("multiview rtt", size, scene, false, true, InternalTexture.DATASOURCE_UNKNOWN, false, undefined, false, false, true, undefined, true);
-        var internalTexture = scene.getEngine().createMultiviewRenderTargetTexture(this.getRenderWidth(), this.getRenderHeight());
-        internalTexture.isMultiview = true;
-        this._texture = internalTexture;
-    }
-
-    /**
-     * @hidden
-     * @param faceIndex the face index, if its a cube texture
-     */
-    public _bindFrameBuffer(faceIndex: number = 0) {
-        if (!this._texture) {
-            return;
-        }
-        this.getScene()!.getEngine().bindMultiviewFramebuffer(this._texture);
-    }
-
-    /**
-     * Gets the number of views the corresponding to the texture (eg. a MultiviewRenderTarget will have > 1)
-     * @returns the view count
-     */
-    public getViewCount() {
-        return 2;
-    }
-}
+};

+ 15 - 0
src/Materials/materialHelper.ts

@@ -229,6 +229,21 @@ export class MaterialHelper {
     }
     }
 
 
     /**
     /**
+     * Prepares the defines related to multiview
+     * @param scene The scene we are intending to draw
+     * @param defines The defines to update
+     */
+    public static PrepareDefinesForMultiview(scene: Scene, defines: any) {
+        if (scene.activeCamera) {
+            var previousMultiview = defines.MULTIVIEW;
+            defines.MULTIVIEW = (scene.activeCamera.outputRenderTarget !== null && scene.activeCamera.outputRenderTarget.getViewCount() > 1);
+            if (defines.MULTIVIEW != previousMultiview) {
+                defines.markAsUnprocessed();
+            }
+        }
+    }
+
+    /**
      * Prepares the defines related to the light information passed in parameter
      * Prepares the defines related to the light information passed in parameter
      * @param scene The scene we are intending to draw
      * @param scene The scene we are intending to draw
      * @param mesh The mesh the effect is compiling for
      * @param mesh The mesh the effect is compiling for

+ 3 - 7
src/Materials/standardMaterial.ts

@@ -793,13 +793,9 @@ export class StandardMaterial extends PushMaterial {
 
 
         // Lights
         // Lights
         defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
         defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
-        if (scene.activeCamera) {
-            var previousMultiview = defines.MULTIVIEW;
-            defines.MULTIVIEW = (scene.activeCamera.outputRenderTarget !== null && scene.activeCamera.outputRenderTarget.getViewCount() > 1);
-            if (defines.MULTIVIEW != previousMultiview) {
-                defines.markAsUnprocessed();
-            }
-        }
+
+        // Multiview
+        MaterialHelper.PrepareDefinesForMultiview(scene, defines);
 
 
         // Textures
         // Textures
         if (defines._areTexturesDirty) {
         if (defines._areTexturesDirty) {

+ 2 - 4
src/scene.ts

@@ -1047,8 +1047,7 @@ export class Scene extends AbstractScene implements IAnimatable {
     public proceduralTexturesEnabled = true;
     public proceduralTexturesEnabled = true;
 
 
     // Private
     // Private
-    /** @hidden */
-    public _engine: Engine;
+    private _engine: Engine;
 
 
     // Performance counters
     // Performance counters
     private _totalVertices = new PerfCounter();
     private _totalVertices = new PerfCounter();
@@ -1114,8 +1113,7 @@ export class Scene extends AbstractScene implements IAnimatable {
     /** @hidden */
     /** @hidden */
     public _activeAnimatables = new Array<Animatable>();
     public _activeAnimatables = new Array<Animatable>();
 
 
-    /** @hidden */
-    public _transformMatrix = Matrix.Zero();
+    private _transformMatrix = Matrix.Zero();
     private _sceneUbo: UniformBuffer;
     private _sceneUbo: UniformBuffer;
 
 
     /** @hidden */
     /** @hidden */