Bläddra i källkod

Merge pull request #7059 from RaananW/multiview-shaders

Multiview shaders
David Catuhe 5 år sedan
förälder
incheckning
1fe74da25a

+ 1 - 0
dist/preview release/what's new.md

@@ -42,6 +42,7 @@
 - Added `AnimationGroup.onAnimationGroupLoopObservable` ([Deltakosh](https://github.com/deltakosh/))
 - Supports custom materials to generate glow through ```referenceMeshToUseItsOwnMaterial``` in the ```GlowLayer``` ([sebavan](http://www.github.com/sebavan))
 - Added `RawTexture2DArray` to enable use of WebGL2 2D array textures by custom shaders ([atg](https://github.com/atg))
+- Added multiview support for the shader material (and the line-mesh class) ([RaananW](https://github.com/RaananW/))
 - Added various (interpolation) functions to Path3D, also `alignTangentsWithPath`, `slice`, `getClosestPositionTo` ([Poolminer](https://github.com/Poolminer/))
 
 ### Engine

+ 1 - 1
src/Materials/effect.ts

@@ -30,7 +30,7 @@ export interface IEffectCreationOptions {
      */
     uniformsNames: string[];
     /**
-     * Uniform buffer varible names that will be set in the shader.
+     * Uniform buffer variable names that will be set in the shader.
      */
     uniformBuffersNames: string[];
     /**

+ 17 - 0
src/Materials/shaderMaterial.ts

@@ -86,6 +86,7 @@ export class ShaderMaterial extends Material {
     private _cachedWorldViewMatrix = new Matrix();
     private _cachedWorldViewProjectionMatrix = new Matrix();
     private _renderId: number;
+    private _multiview: boolean = false;
 
     /**
      * Instantiate a new shader material.
@@ -485,6 +486,19 @@ export class ShaderMaterial extends Material {
         var attribs = [];
         var fallbacks = new EffectFallbacks();
 
+        // global multiview
+        if (engine.getCaps().multiview &&
+            scene.activeCamera &&
+            scene.activeCamera.outputRenderTarget &&
+            scene.activeCamera.outputRenderTarget.getViewCount() > 1) {
+            this._multiview = true;
+            defines.push("#define MULTIVIEW");
+            if (this._options.uniforms.indexOf("viewProjection") !== -1 &&
+                this._options.uniforms.push("viewProjectionR") === -1) {
+                this._options.uniforms.push("viewProjectionR");
+            }
+        }
+
         for (var index = 0; index < this._options.defines.length; index++) {
             defines.push(this._options.defines[index]);
         }
@@ -625,6 +639,9 @@ export class ShaderMaterial extends Material {
 
             if (this._options.uniforms.indexOf("viewProjection") !== -1) {
                 this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
+                if (this._multiview) {
+                    this._effect.setMatrix("viewProjectionR", this.getScene()._transformMatrixR);
+                }
             }
 
             // Bones

+ 11 - 0
src/Shaders/color.vertex.fx

@@ -13,6 +13,9 @@ attribute vec4 color;
 
 #include<instancesDeclaration>
 uniform mat4 viewProjection;
+#ifdef MULTIVIEW
+	uniform mat4 viewProjectionR;
+#endif 
 
 // Output
 #ifdef VERTEXCOLOR
@@ -24,7 +27,15 @@ void main(void) {
 #include<bonesVertex>
     vec4 worldPos = finalWorld * vec4(position, 1.0);
 
+#ifdef MULTIVIEW
+	if (gl_ViewID_OVR == 0u) {
+		gl_Position = viewProjection * worldPos;
+	} else {
+		gl_Position = viewProjectionR * worldPos;
+	}
+#else
 	gl_Position = viewProjection * worldPos;
+#endif
 
 #include<clipPlaneVertex>