浏览代码

Fixes for skinning and morph targets

Gary Hsu 6 年之前
父节点
当前提交
28cf489a19

+ 7 - 0
loaders/src/glTF/2.0/glTFLoader.ts

@@ -1522,6 +1522,13 @@ export class GLTFLoader implements IGLTFLoader {
                 return new VertexBuffer(this._babylonScene.getEngine(), data, kind, false);
             });
         }
+        // Load joint indices as a float array since the shaders expect float data but glTF uses unsigned byte/short.
+        // This prevents certain platforms (e.g. D3D) from having to convert the data to float on the fly.
+        else if (kind === VertexBuffer.MatricesIndicesKind) {
+            accessor._babylonVertexBuffer = this._loadFloatAccessorAsync(`/accessors/${accessor.index}`, accessor).then((data) => {
+                return new VertexBuffer(this._babylonScene.getEngine(), data, kind, false);
+            });
+        }
         else {
             const bufferView = ArrayItem.Get(`${context}/bufferView`, this._gltf.bufferViews, accessor.bufferView);
             accessor._babylonVertexBuffer = this._loadVertexBufferViewAsync(bufferView, kind).then((babylonBuffer) => {

+ 18 - 8
src/Engines/Native/nativeShaderProcessor.ts

@@ -1,20 +1,25 @@
 import { WebGL2ShaderProcessor } from "../WebGL/webGL2ShaderProcessors";
 import { VertexBuffer } from "../../Meshes/buffer";
 
-// These must match the values for bgfx::Attrib::Enum
+// These numbers must match the values for bgfx::Attrib::Enum
 const attributeLocations: { [kind: string]: number } = {
     [VertexBuffer.PositionKind]: 0,
     [VertexBuffer.NormalKind]: 1,
     [VertexBuffer.TangentKind]: 2,
-    [VertexBuffer.UVKind]: 10,
-    [VertexBuffer.UV2Kind]: 11,
     [VertexBuffer.ColorKind]: 4,
     [VertexBuffer.MatricesIndicesKind]: 8,
     [VertexBuffer.MatricesWeightsKind]: 9,
 };
 
+// Must match bgfx::Attrib::TexCoord0
+const firstGenericAttributeLocation = 10;
+
+// Must match bgfx::Attrib::TexCoord7
+const lastGenericAttributeLocation = 17;
+
 /** @hidden */
 export class NativeShaderProcessor extends WebGL2ShaderProcessor {
+    private _genericAttributeLocation: number;
     private _varyingLocationCount: number;
     private _varyingLocationMap: { [name: string]: number };
     private _replacements: Array<{ searchValue: RegExp, replaceValue: string }>;
@@ -30,12 +35,15 @@ export class NativeShaderProcessor extends WebGL2ShaderProcessor {
     }
 
     public attributeProcessor(attribute: string): string {
-        const match = attribute.match(/attribute\s+\w+\s+(\w+)\s*;/)!;
+        const match = attribute.match(/attribute\s+[^\s]+\s+([^\s]+)\s*(?:\[.+\])?\s*;/)!;
         const name = match[1];
 
-        const location = attributeLocations[name];
+        let location = attributeLocations[name];
         if (location === undefined) {
-            throw new Error(`Unsupported attribute ${name}`);
+            location = this._genericAttributeLocation++;
+            if (location > lastGenericAttributeLocation) {
+                throw new Error("Exceeded maximum custom attributes");
+            }
         }
 
         return `layout(location=${location}) ${super.attributeProcessor(attribute)}`;
@@ -52,11 +60,11 @@ export class NativeShaderProcessor extends WebGL2ShaderProcessor {
             this._varyingLocationMap[varying] = location;
         }
 
-        return `layout(location=${location}) ${super.varyingProcessor(varying, isFragment)}`
+        return `layout(location=${location}) ${super.varyingProcessor(varying, isFragment)}`;
     }
 
     public uniformProcessor(uniform: string): string {
-        const match = uniform.match(/uniform\s+(\w+)\s+(\w+)\s*;/)!;
+        const match = uniform.match(/uniform\s+([^\s]+)\s+([^\s]+)\s*(?:\[.+\])?\s*;/)!;
         const type = match[1];
         const name = match[2];
 
@@ -75,6 +83,8 @@ export class NativeShaderProcessor extends WebGL2ShaderProcessor {
     }
 
     public preProcessor(code: string, defines: string[], isFragment: boolean): string {
+        this._genericAttributeLocation = firstGenericAttributeLocation;
+
         if (!isFragment) {
             this._varyingLocationCount = 0;
             this._varyingLocationMap = {};

+ 1 - 1
src/Engines/engine.ts

@@ -3216,7 +3216,7 @@ export class Engine {
         return effect;
     }
 
-    protected static _concatenateShader(source: string, defines: Nullable<string>, shaderVersion: string): string {
+    protected static _concatenateShader(source: string, defines: Nullable<string>, shaderVersion: string = ""): string {
         return shaderVersion + (defines ? defines + "\n" : "") + source;
     }
 

+ 4 - 1
src/Engines/nativeEngine.ts

@@ -425,7 +425,10 @@ export class NativeEngine extends Engine {
 
     public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): any {
         this.onBeforeShaderCompilationObservable.notifyObservers(this);
-        const program = this._native.createProgram(vertexCode, fragmentCode);
+        const program = this._native.createProgram(
+            Engine._concatenateShader(vertexCode, defines),
+            Engine._concatenateShader(fragmentCode, defines)
+        );
         this.onAfterShaderCompilationObservable.notifyObservers(this);
         return program;
     }

+ 0 - 22
src/Meshes/buffer.ts

@@ -19,21 +19,6 @@ export class Buffer {
     public readonly byteStride: number;
 
     /**
-     * Gets the byte length.
-     */
-    public get byteLength(): number {
-        if (!this._data) {
-            return 0;
-        }
-
-        if (this._data instanceof Array) {
-            return this._data.length * Float32Array.BYTES_PER_ELEMENT;
-        }
-
-        return this._data.byteLength;
-    }
-
-    /**
      * Constructor
      * @param engine the engine
      * @param data the data to use for this buffer
@@ -276,13 +261,6 @@ export class VertexBuffer {
     public readonly type: number;
 
     /**
-     * Gets the byte length.
-     */
-    public get byteLength(): number {
-        return this._buffer.byteLength - this.byteOffset;
-    }
-
-    /**
      * Constructor
      * @param engine the engine
      * @param data the data to use for this vertex buffer

+ 1 - 3
src/Shaders/pbr.fragment.fx

@@ -34,9 +34,7 @@ precision highp float;
 #include<pbrHelperFunctions>
 #include<imageProcessingFunctions>
 #include<shadowsFragmentFunctions>
-#ifndef USESPHERICALINVERTEX
-    #include<harmonicsFunctions>
-#endif
+#include<harmonicsFunctions>
 #include<pbrDirectLightingSetupFunctions>
 #include<pbrDirectLightingFalloffFunctions>
 #include<pbrBRDFFunctions>