Explorar el Código

Make sure the scene ubo is updated with viewPosition before updating the GPU buffer

Popov72 hace 4 años
padre
commit
cf5cc8121e

+ 0 - 1
src/Engines/Extensions/engine.multiview.ts

@@ -144,7 +144,6 @@ Scene.prototype._updateMultiviewUbo = function(viewR?: Matrix, projectionR?: Mat
         this._multiviewSceneUbo.updateMatrix("viewProjection", this.getTransformMatrix());
         this._multiviewSceneUbo.updateMatrix("viewProjectionR", this._transformMatrixR);
         this._multiviewSceneUbo.updateMatrix("view", this._viewMatrix);
-        this._multiviewSceneUbo.update();
     }
 };
 Scene.prototype._renderMultiviewToSingleView = function(camera: Camera) {

+ 0 - 1
src/Lights/Shadows/cascadedShadowGenerator.ts

@@ -803,7 +803,6 @@ export class CascadedShadowGenerator extends ShadowGenerator {
                 const sceneUBO = this._scene.getSceneUniformBuffer();
                 sceneUBO.updateMatrix("viewProjection", this.getCascadeTransformMatrix(layer)!);
                 sceneUBO.updateMatrix("view", this.getCascadeViewMatrix(layer)!);
-                sceneUBO.update();
             }
         });
 

+ 7 - 0
src/Materials/material.ts

@@ -620,6 +620,7 @@ export class Material implements IAnimatable {
      * Stores a reference to the scene
      */
     private _scene: Scene;
+    private _needToFinalizeSceneUbo: boolean;
 
     /**
      * Stores the fill mode state
@@ -942,6 +943,7 @@ export class Material implements IAnimatable {
         if (!this._useUBO) {
             effect.setMatrix("view", this.getScene().getViewMatrix());
         } else {
+            this._needToFinalizeSceneUbo = true;
             this.bindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
         }
     }
@@ -954,6 +956,7 @@ export class Material implements IAnimatable {
         if (!this._useUBO) {
             effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
         } else {
+            this._needToFinalizeSceneUbo = true;
             this.bindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
         }
     }
@@ -964,6 +967,10 @@ export class Material implements IAnimatable {
      */
     protected _afterBind(mesh?: Mesh): void {
         this._scene._cachedMaterial = this;
+        if (this._needToFinalizeSceneUbo) {
+            this._needToFinalizeSceneUbo = false;
+            this.getScene().finalizeSceneUbo();
+        }
         if (mesh) {
             this._scene._cachedVisibility = mesh.visibility;
         } else {

+ 18 - 10
src/scene.ts

@@ -2003,7 +2003,6 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
      * @param projectionR defines the right Projection matrix to use (if provided)
      */
     public setTransformMatrix(viewL: Matrix, projectionL: Matrix, viewR?: Matrix, projectionR?: Matrix): void {
-        // TODO WEBGPU handle case where the matrices didn't change but the viewPosition will still be different than previously
         if (this._viewUpdateFlag === viewL.updateFlag && this._projectionUpdateFlag === projectionL.updateFlag) {
             return;
         }
@@ -2027,17 +2026,26 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
         } else if (this._sceneUbo.useUbo) {
             this._sceneUbo.updateMatrix("viewProjection", this._transformMatrix);
             this._sceneUbo.updateMatrix("view", this._viewMatrix);
+        }
+    }
+
+    /**
+     * Update the scene ubo before it can be used in rendering processing
+     * @returns the scene UniformBuffer
+     */
+    public finalizeSceneUbo(): UniformBuffer {
+        const ubo = this.getSceneUniformBuffer();
+        const eyePosition = this._forcedViewPosition ? this._forcedViewPosition : (this._mirroredCameraPosition ? this._mirroredCameraPosition : (this.activeCamera!).globalPosition);
+        const invertNormal = (this.useRightHandedSystem === (this._mirroredCameraPosition != null));
+        ubo.updateFloat4("viewPosition",
+            eyePosition.x,
+            eyePosition.y,
+            eyePosition.z,
+            invertNormal ? -1 : 1);
 
-            const eyePosition = this._forcedViewPosition ? this._forcedViewPosition : (this._mirroredCameraPosition ? this._mirroredCameraPosition : (this.activeCamera!).globalPosition);
-            const invertNormal = (this.useRightHandedSystem === (this._mirroredCameraPosition != null));
-            this._sceneUbo.updateFloat4("viewPosition",
-                eyePosition.x,
-                eyePosition.y,
-                eyePosition.z,
-                invertNormal ? -1 : 1);
+        ubo.update();
 
-            this._sceneUbo.update();
-        }
+        return ubo;
     }
 
     /**