Browse Source

Merge pull request #4252 from bghgary/KHR_texture_transform

Add support for KHR_texture_transform to glTF loader
David Catuhe 7 years ago
parent
commit
b51681ac6a

+ 4 - 2
Tools/Gulp/config.json

@@ -1538,7 +1538,8 @@
                     "../../loaders/src/glTF/2.0/Extensions/KHR_draco_mesh_compression.ts",
                     "../../loaders/src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts",
                     "../../loaders/src/glTF/2.0/Extensions/KHR_materials_unlit.ts",
-                    "../../loaders/src/glTF/2.0/Extensions/KHR_lights.ts"
+                    "../../loaders/src/glTF/2.0/Extensions/KHR_lights.ts",
+                    "../../loaders/src/glTF/2.0/Extensions/KHR_texture_transform.ts"
                 ],
                 "doNotIncludeInBundle": true,
                 "output": "babylon.glTF2FileLoader.js"
@@ -1562,7 +1563,8 @@
                     "../../loaders/src/glTF/2.0/Extensions/KHR_draco_mesh_compression.ts",
                     "../../loaders/src/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.ts",
                     "../../loaders/src/glTF/2.0/Extensions/KHR_materials_unlit.ts",
-                    "../../loaders/src/glTF/2.0/Extensions/KHR_lights.ts"
+                    "../../loaders/src/glTF/2.0/Extensions/KHR_lights.ts",
+                    "../../loaders/src/glTF/2.0/Extensions/KHR_texture_transform.ts"
                 ],
                 "output": "babylon.glTFFileLoader.js"
             }

+ 1 - 1
dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts

@@ -769,7 +769,7 @@ declare module BABYLON.GLTF2 {
     /**
      * Reference to a texture
      */
-    interface ITextureInfo {
+    interface ITextureInfo extends IProperty {
         /**
          * The index of the texture
          */

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

@@ -4,7 +4,12 @@
 
 ## Updates
 
+### Core Engine
 - Added the choice of [forming a closed loop](http://doc.babylonjs.com/how_to/how_to_use_curve3#catmull-rom-spline) to the catamull-rom-spline curve3 ([johnk](https://github.com/babylonjsguide))
+- Add support for specifying the center of rotation to textures ([bghgary](http://www.github.com/bghgary))
+
+### glTF Loader
+  - Add support for KHR_texture_transform ([bghgary](http://www.github.com/bghgary))
 
 ## Bug fixes
 

+ 51 - 0
loaders/src/glTF/2.0/Extensions/KHR_texture_transform.ts

@@ -0,0 +1,51 @@
+/// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
+
+module BABYLON.GLTF2.Extensions {
+    const NAME = "KHR_texture_transform";
+
+    interface IKHRTextureTransform {
+        offset?: number[];
+        rotation?: number;
+        scale?: number[];
+        texCoord?: number;
+    }
+
+    /**
+     * [Specification](https://github.com/AltspaceVR/glTF/blob/avr-sampler-offset-tile/extensions/2.0/Khronos/KHR_texture_transform/README.md) (Experimental)
+     */
+    export class KHR_texture_transform extends GLTFLoaderExtension {
+        public readonly name = NAME;
+
+        protected _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>> {
+            return this._loadExtensionAsync<IKHRTextureTransform>(context, textureInfo, (extensionContext, extension) => {
+                return this._loader._loadTextureAsync(context, textureInfo, babylonTexture => {
+                    if (extension.offset) {
+                        babylonTexture.uOffset = extension.offset[0];
+                        babylonTexture.vOffset = extension.offset[1];
+                    }
+
+                    // Always rotate around the origin.
+                    babylonTexture.uRotationCenter = 0;
+                    babylonTexture.vRotationCenter = 0;
+
+                    if (extension.rotation) {
+                        babylonTexture.wAng = -extension.rotation;
+                    }
+
+                    if (extension.scale) {
+                        babylonTexture.uScale = extension.scale[0];
+                        babylonTexture.vScale = extension.scale[1];
+                    }
+
+                    if (extension.texCoord != undefined) {
+                        babylonTexture.coordinatesIndex = extension.texCoord;
+                    }
+
+                    assign(babylonTexture);
+                });
+            });
+        }
+    }
+
+    GLTFLoader._Register(NAME, loader => new KHR_texture_transform(loader));
+}

+ 5 - 0
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -1442,6 +1442,11 @@ module BABYLON.GLTF2 {
 
         /** @hidden */
         public _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Promise<void> {
+            const promise = GLTFLoaderExtension._LoadTextureAsync(this, context, textureInfo, assign);
+            if (promise) {
+                return promise;
+            }
+
             const texture = GLTFLoader._GetProperty(`${context}/index`, this._gltf.textures, textureInfo.index);
             context = `#/textures/${textureInfo.index}`;
 

+ 8 - 0
loaders/src/glTF/2.0/babylon.glTFLoaderExtension.ts

@@ -32,6 +32,9 @@ module BABYLON.GLTF2 {
         /** Override this method to modify the default behavior for loading materials. */
         protected _loadMaterialAsync(context: string, material: _ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>> { return null; }
 
+        /** Override this method to modify the default behavior for loading textures. */
+        protected _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>> { return null; }
+
         /** Override this method to modify the default behavior for loading uris. */
         protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>> { return null; }
 
@@ -82,6 +85,11 @@ module BABYLON.GLTF2 {
             return loader._applyExtensions(extension => extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign));
         }
 
+        /** Helper method called by the loader to allow extensions to override loading textures. */
+        public static _LoadTextureAsync(loader: GLTFLoader, context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Nullable<Promise<void>> {
+            return loader._applyExtensions(extension => extension._loadTextureAsync(context, textureInfo, assign));
+        }
+
         /** Helper method called by the loader to allow extensions to override loading uris. */
         public static _LoadUriAsync(loader: GLTFLoader, context: string, uri: string): Nullable<Promise<ArrayBufferView>> {
             return loader._applyExtensions(extension => extension._loadUriAsync(context, uri));

+ 1 - 1
loaders/src/glTF/2.0/babylon.glTFLoaderInterfaces.ts

@@ -1,5 +1,5 @@
 /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 module BABYLON.GLTF2 {
     /** @hidden */

+ 1 - 1
serializers/src/glTF/2.0/babylon.glTFAnimation.ts

@@ -1,4 +1,4 @@
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 module BABYLON.GLTF2 {
     /**

+ 1 - 1
serializers/src/glTF/2.0/babylon.glTFData.ts

@@ -1,4 +1,4 @@
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 module BABYLON {
     /**

+ 1 - 1
serializers/src/glTF/2.0/babylon.glTFExporter.ts

@@ -1,4 +1,4 @@
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 /**
  * Module for the Babylon glTF 2.0 exporter.  Should ONLY be used internally

+ 1 - 1
serializers/src/glTF/2.0/babylon.glTFMaterial.ts

@@ -1,4 +1,4 @@
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 module BABYLON.GLTF2 {
     /** 

+ 1 - 1
serializers/src/glTF/2.0/babylon.glTFUtilities.ts

@@ -1,4 +1,4 @@
-/// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 
 module BABYLON.GLTF2 {
     /**

+ 24 - 6
src/Materials/Textures/babylon.texture.ts

@@ -65,6 +65,24 @@
         @serialize()
         public wAng = 0;
 
+        /**
+         * Defines the center of rotation (U)
+         */
+        @serialize()
+        public uRotationCenter = 0.5;
+
+        /**
+         * Defines the center of rotation (V)
+         */
+        @serialize()
+        public vRotationCenter = 0.5;
+
+        /**
+         * Defines the center of rotation (W)
+         */
+        @serialize()
+        public wRotationCenter = 0.5;
+
         get noMipmap(): boolean {
             return this._noMipmap;
         }
@@ -229,15 +247,15 @@
             x *= this.uScale;
             y *= this.vScale;
 
-            x -= 0.5 * this.uScale;
-            y -= 0.5 * this.vScale;
-            z -= 0.5;
+            x -= this.uRotationCenter * this.uScale;
+            y -= this.vRotationCenter * this.vScale;
+            z -= this.wRotationCenter;
 
             Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
 
-            t.x += 0.5 * this.uScale + this.uOffset;
-            t.y += 0.5 * this.vScale + this.vOffset;
-            t.z += 0.5;
+            t.x += this.uRotationCenter * this.uScale + this.uOffset;
+            t.y += this.vRotationCenter * this.vScale + this.vOffset;
+            t.z += this.wRotationCenter;
         }
 
         public getTextureMatrix(): Matrix {

+ 1 - 1
tests/unit/babylon/babylonReference.ts

@@ -1,6 +1,6 @@
 /// <reference path="../../../dist/preview release/babylon.d.ts" />
 /// <reference path="../../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts" />
-/// <reference path="../../../dist/babylon.glTF2Interface.d.ts"/>
+/// <reference path="../../../dist/preview release/gltf2Interface/babylon.glTF2Interface.d.ts"/>
 /// <reference path="../../../dist/preview release/serializers/babylon.glTF2Serializer.d.ts" />
 
 /// <reference path="../node_modules/@types/chai/index.d.ts" />

BIN
tests/validation/ReferenceImages/gltfExtensionKhrTextureTransform.png


+ 5 - 0
tests/validation/config.json

@@ -303,6 +303,11 @@
       "referenceImage": "gltfUnlit.png"
     },
     {
+      "title": "GLTF Extension KHR_texture_transform",
+      "playgroundId": "#RNT7K4#2",
+      "referenceImage": "gltfExtensionKhrTextureTransform.png"
+    },
+    {
       "title": "Asset Containers",
       "playgroundId": "#P3U079#19",
       "referenceImage": "assetContainer.png"