Browse Source

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 7 năm trước cách đây
mục cha
commit
493e991c9b

BIN
Playground/scenes/BoomBox.glb


BIN
Playground/scenes/BoomBox/BoomBox.bin


+ 173 - 0
Playground/scenes/BoomBox/BoomBox.gltf

@@ -0,0 +1,173 @@
+{
+  "accessors": [
+    {
+      "bufferView": 0,
+      "componentType": 5126,
+      "count": 3575,
+      "type": "VEC2"
+    },
+    {
+      "bufferView": 1,
+      "componentType": 5126,
+      "count": 3575,
+      "type": "VEC3"
+    },
+    {
+      "bufferView": 2,
+      "componentType": 5126,
+      "count": 3575,
+      "type": "VEC4"
+    },
+    {
+      "bufferView": 3,
+      "componentType": 5126,
+      "count": 3575,
+      "type": "VEC3",
+      "max": [
+        0.009921154,
+        0.00977163,
+        0.0100762453
+      ],
+      "min": [
+        -0.009921154,
+        -0.00977163,
+        -0.0100762453
+      ]
+    },
+    {
+      "bufferView": 4,
+      "componentType": 5123,
+      "count": 18108,
+      "type": "SCALAR"
+    }
+  ],
+  "asset": {
+    "generator": "glTF Tools for Unity",
+    "version": "2.0"
+  },
+  "bufferViews": [
+    {
+      "buffer": 0,
+      "byteLength": 28600
+    },
+    {
+      "buffer": 0,
+      "byteOffset": 28600,
+      "byteLength": 42900
+    },
+    {
+      "buffer": 0,
+      "byteOffset": 71500,
+      "byteLength": 57200
+    },
+    {
+      "buffer": 0,
+      "byteOffset": 128700,
+      "byteLength": 42900
+    },
+    {
+      "buffer": 0,
+      "byteOffset": 171600,
+      "byteLength": 36216
+    }
+  ],
+  "buffers": [
+    {
+      "uri": "BoomBox.bin",
+      "byteLength": 207816
+    }
+  ],
+  "images": [
+    {
+      "uri": "BoomBox_baseColor.png"
+    },
+    {
+      "uri": "BoomBox_occlusionRoughnessMetallic.png"
+    },
+    {
+      "uri": "BoomBox_normal.png"
+    },
+    {
+      "uri": "BoomBox_emissive.png"
+    }
+  ],
+  "meshes": [
+    {
+      "primitives": [
+        {
+          "attributes": {
+            "TEXCOORD_0": 0,
+            "NORMAL": 1,
+            "TANGENT": 2,
+            "POSITION": 3
+          },
+          "indices": 4,
+          "material": 0
+        }
+      ],
+      "name": "BoomBox"
+    }
+  ],
+  "materials": [
+    {
+      "pbrMetallicRoughness": {
+        "baseColorTexture": {
+          "index": 0
+        },
+        "metallicRoughnessTexture": {
+          "index": 1
+        }
+      },
+      "normalTexture": {
+        "index": 2
+      },
+      "occlusionTexture": {
+        "index": 1
+      },
+      "emissiveFactor": [
+        1.0,
+        1.0,
+        1.0
+      ],
+      "emissiveTexture": {
+        "index": 3
+      },
+      "name": "BoomBox_Mat"
+    }
+  ],
+  "nodes": [
+    {
+      "mesh": 0,
+      "rotation": [
+        0.0,
+        1.0,
+        0.0,
+        0.0
+      ],
+      "scale": [ 1, 1, 1 ],
+      "name": "BoomBox"
+    }
+  ],
+  "scene": 0,
+  "scenes": [
+    {
+      "nodes": [
+        0
+      ]
+    }
+  ],
+  "textures": [
+    {
+      "source": 0
+    },
+    {
+      "source": 1
+    },
+    {
+      "source": 2
+    },
+    {
+      "source": 3
+    }
+  ]
+}

BIN
Playground/scenes/BoomBox/BoomBox_baseColor.png


BIN
Playground/scenes/BoomBox/BoomBox_emissive.png


BIN
Playground/scenes/BoomBox/BoomBox_normal.png


BIN
Playground/scenes/BoomBox/BoomBox_occlusionRoughnessMetallic.png


+ 6 - 18
loaders/readme.md

@@ -1,24 +1,12 @@
 ## Babylon.js loaders
 
-You will find here all loading plugin that you can use to load different formats than .babylon
-
-To use then you just have to reference the loader file:
-
-```
-<script src="Babylon.js"></script>
-<script src="babylon.stlFileLoader.js"></script>
-```
-
-And then the SceneLoader will know how to load the new extension:
-```
-BABYLON.SceneLoader.Load("/Files/", "ch9.stl", engine, function (newScene) { 
-   newScene.activeCamera.attachControl(canvas, false);
-   engine.runRenderLoop(function () { 
-       newScene.render(); 
-   }); 
-});
-```
+### Usage
+See documentation links:
+- https://doc.babylonjs.com/how_to/obj
+- https://doc.babylonjs.com/how_to/stl
+- https://doc.babylonjs.com/how_to/gltf
 
+### Build
 To compile, from the tools/gulp folder:
 
 ```

+ 0 - 74
loaders/src/OBJ/README.md

@@ -1,74 +0,0 @@
-# Babylon.js .obj File Loader
-
-#[Demo](http://www.babylonjs-playground.com/#28YUR5)
-To use it, you just have to reference the loader file:
-
-```
-<script src="babylon.2.1.js"></script>
-<script src="babylon.objFileLoader.js"></script>
-```
-
-Babylon.js will know how to load the obj file and its mtl file automatically: 
-```
-BABYLON.SceneLoader.Load("/assets/", "batman.obj", engine, function (newScene) { 
-   // ...
-});
-```
-```
-var loader = new BABYLON.AssetsManager(scene);
-var batman = loader.addMeshTask("batman", "", "assets/", "batman.obj");
-```
-```
-BABYLON.SceneLoader.ImportMesh("batmanface", "", "batman.obj", scene, function (meshes) { 
-   // ...
-});
-```
-
-## Good things to know
-* Your model doesn't have to be triangulated, as this loader will do it automatically.
-* A Babylon.Mesh will be created for each object/group
-* The obj model should be exported with -Z axis forward, and Y axis upward to be compatible with Babylon.js
-
-![Axis](http://geomorph.sourceforge.net/preview/axes.jpg)
-
-* By default, due to optimization in the code for loading time, UVs problems can appear, like this :
-
-![Batman UVs problem](http://i.imgur.com/vjWKNRK.png)
-
-If you meet this problem, set the variable 
-```
-BABYLON.OBJFileLoader.OPTIMIZE_WITH_UV = true;
-```
-Then, you'll have a better texture, but with a longer loading.
-
-![Batman UVs ok](http://i.imgur.com/Dajwlvq.png)
-
-## Supported
-* Object/group
-* Faces
-    * triangles
-    * quads
-    * polygons
-* Colors
-    * diffuseColor
-    * ambientColor
-    * specularColor
-    * specularPower
-    * alpha
-* Textures
-    * ambientTexture
-    * diffuseTexture
-    * specularTexture
-    * bumpTexture
-    * opacityTexture
-* Multimaterial
-	* For each material defined in the same mesh, it creates a new BABYLON.Mesh.
-	* The name of the created BABYLON.Mesh follows this syntax: meshName_mmX 
-	* X is the nth BABYLON.Mesh created with this method
-
-    
-## Not supported currently
-* Smoothing groups (s parameter in OBJ file)
-* Illumination (illum parameter in MTL file)
-* The differents options for loading textures in MTL file.
-* A good description about MTL file and his options could be found here: http://paulbourke.net/dataformats/mtl/

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

@@ -1716,7 +1716,7 @@ module BABYLON.GLTF2 {
             }
             else {
                 if (magFilter !== ETextureMagFilter.NEAREST) {
-                    Tools.Warn(context + "Invalid texture magnification filter " + magFilter);
+                    Tools.Warn(context + ": Invalid texture magnification filter " + magFilter);
                 }
 
                 switch (minFilter) {

+ 0 - 182
loaders/src/glTF/README.md

@@ -1,182 +0,0 @@
-# Babylon.js glTF File Loader
-
-The glTF file loader is a SceneLoader plugin.
-
-[Simple Playground Example](http://www.babylonjs-playground.com/#2IK4U7)
-
-## Setup
-
-**Full Version**
-
-This loader supports both glTF 1.0 and 2.0 and will use the correct loader based on the glTF version string.
-
-```HTML
-<script src="babylon.js"></script>
-<script src="babylon.glTFFileLoader.js"></script>
-```
-
-**Version 1 Only**
-
-This loader supports only glTF 1.0 and will fail to load glTF 2.0.
-
-```HTML
-<script src="babylon.js"></script>
-<script src="babylon.glTF1FileLoader.js"></script>
-```
-
-**Version 2 Only**
-
-This loader supports only glTF 2.0 and will fail to load glTF 1.0.
-
-```HTML
-<script src="babylon.js"></script>
-<script src="babylon.glTF2FileLoader.js"></script>
-```
-
-## Loading the Scene
-The Load function loads a glTF asset into a new scene.
-```JavaScript
-BABYLON.SceneLoader.Load("./", "duck.gltf", engine, function (scene) {
-    // do something with the scene
-});
-```
-
-The Append function appends a glTF file to an existing scene.
-```JavaScript
-BABYLON.SceneLoader.Append("./", "duck.gltf", scene, function (scene) {
-    // do something with the scene
-});
-```
-
-The ImportMesh function imports specific meshes from a glTF asset to an existing scene and returns the imported meshes and skeletons.
-```JavaScript
-// The first parameter can be set to null to load all meshes and skeletons
-BABYLON.SceneLoader.ImportMesh(["myMesh1", "myMesh2"], "./", "duck.gltf", scene, function (meshes, particleSystems, skeletons) {
-    // do something with the meshes and skeletons
-    // particleSystems are always null for glTF assets
-});
-```
-
-## Advanced
-
-The SceneLoader returns the glTF loader instance to enable setting properties per instance.
-
-```JavaScript
-var loader = BABYLON.SceneLoader.Load("./", "duck.gltf", engine, function (scene) {
-    // do something with the scene
-});
-
-// do something with the loader
-// loader.<option1> = <...>
-// loader.<option2> = <...>
-// loader.dispose();
-```
-
-#### onParsed
-Raised when the asset has been parsed. The `data.json` property stores the glTF JSON. The `data.bin` property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
-
-```JavaScript
-loader.onParsed = function (data) {
-    // do something with the data
-};
-```
-
-### Version 1 Only
-
-#### IncrementalLoading
-Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders. Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled. Defaults to true.
-
-```JavaScript
-BABYLON.GLTFFileLoader.IncrementalLoading = false;
-```
-
-#### HomogeneousCoordinates
-Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters. Defaults to false.
-
-```JavaScript
-BABYLON.GLTFFileLoader.HomogeneousCoordinates = true;
-```
-
-### Version 2 Only
-
-#### coordinateSystemMode
-The coordinate system mode (AUTO, FORCE_RIGHT_HANDED). Defaults to AUTO.
-
-```JavaScript
-loader.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED;
-```
-
-#### animationStartMode
-The animation start mode (NONE, FIRST, ALL). Defaults to FIRST.
-
-```JavaScript
-loader.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.NONE;
-```
-
-#### compileMaterials
-Set to true to compile materials before raising the success callback. Defaults to false.
-
-```JavaScript
-loader.compileMaterials = true;
-```
-
-#### useClipPlane
-Set to true to also compile materials with clip planes. Defaults to false.
-
-```JavaScript
-loader.useClipPlane = true;
-```
-
-#### compileShadowGenerators
-Set to true to compile shadow generators before raising the success callback. Defaults to false.
-
-```JavaScript
-loader.compileShadowGenerators = true;
-```
-
-#### onMeshLoaded
-Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
-
-```JavaScript
-loader.onMeshLoaded = function (mesh) {
-    // do something with the mesh
-};
-```
-
-#### onTextureLoaded
-Raised when the loader creates a texture 
-after parsing the glTF properties of the texture.
-
-```JavaScript
-loader.onTextureLoaded = function (texture) {
-    // do something with the texture
-};
-```
-
-#### onMaterialLoaded
-Raised when the loader creates a material after parsing the glTF properties of the material.
-
-```JavaScript
-loader.onMaterialLoaded = function (material) {
-    // do something with the material
-};
-```
-
-#### onComplete
-Raised when the asset is completely loaded, immediately before the loader is disposed.
-For assets with LODs, raised when all of the LODs are complete.
-For assets without LODs, raised when the model is complete, immediately after onSuccess.
-
-```JavaScript
-loader.onComplete = function () {
-    // do something when loading is complete
-};
-```
-
-#### dispose
-Disposes the loader, releases resources during load, and cancels any outstanding requests.
-
-```JavaScript
-// Cancel loading of the current glTF asset.
-loader.dispose();
-```

+ 64 - 11
src/Cameras/VR/babylon.vrExperienceHelper.ts

@@ -57,6 +57,8 @@ module BABYLON {
         private _teleportationAllowed: boolean = false;
         private _rotationAllowed: boolean = true;
         private _teleportationRequestInitiated = false;
+        private _teleportationBackRequestInitiated = false;
+        private teleportBackwardsVector = new Vector3(0, -1, -1);
         private _rotationRightAsked = false;
         private _rotationLeftAsked = false;
         private _teleportationCircle: Mesh;
@@ -626,6 +628,7 @@ module BABYLON {
                 if (gamepad.leftStick) {
                     gamepad.onleftstickchanged((stickValues) => {
                         if (this._teleportationEnabled) {
+                            this._teleportBackwardsCheck(stickValues);
                             // Listening to classic/xbox gamepad only if no VR controller is active
                             if ((!this._leftLaserPointer && !this._rightLaserPointer) ||
                                 ((this._leftLaserPointer && !this._leftLaserPointer.isVisible) &&
@@ -788,6 +791,49 @@ module BABYLON {
             }
         }
 
+        private _teleportBackwardsCheck(stateObject: StickValues){
+            // Teleport backwards
+            if(stateObject.y > this._padSensibilityUp) {
+                if(!this._teleportationBackRequestInitiated){
+                    if(!this.currentVRCamera){
+                        return;
+                    }
+
+                    // Get rotation and position of the current camera
+                    var rotation = Quaternion.FromRotationMatrix(this.currentVRCamera.getWorldMatrix().getRotationMatrix());
+                    var position = this.currentVRCamera.position;
+
+                    // If the camera has device position, use that instead
+                    if((<WebVRFreeCamera>this.currentVRCamera).devicePosition && (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion){
+                        rotation = (<WebVRFreeCamera>this.currentVRCamera).deviceRotationQuaternion;
+                        position = (<WebVRFreeCamera>this.currentVRCamera).devicePosition;
+                    }
+
+                    // Get matrix with only the y rotation of the device rotation
+                    rotation.toEulerAnglesToRef(this._workingVector);
+                    this._workingVector.z = 0;
+                    this._workingVector.x = 0;
+                    Quaternion.RotationYawPitchRollToRef(this._workingVector.y, this._workingVector.x, this._workingVector.z, this._workingQuaternion);
+                    this._workingQuaternion.toRotationMatrix(this._workingMatrix);
+
+                    // Rotate backwards ray by device rotation to cast at the ground behind the user
+                    Vector3.TransformCoordinatesToRef(this.teleportBackwardsVector, this._workingMatrix, this._workingVector);
+                    
+                    // Teleport if ray hit the ground and is not to far away eg. backwards off a cliff
+                    var ray = new BABYLON.Ray(position, this._workingVector);
+                    var hit = this._scene.pickWithRay(ray, this._raySelectionPredicate);
+                    if(hit && hit.pickedPoint && hit.pickedMesh &&this._isTeleportationFloor(hit.pickedMesh) && hit.distance < 5){
+                        this._teleportCamera(hit.pickedPoint);
+                    }
+                    
+                    this._teleportationBackRequestInitiated = true;
+                }
+            }else{
+                this._teleportationBackRequestInitiated = false;
+            }
+            
+        }
+
         private _enableTeleportationOnController(webVRController: WebVRController) {
             var controllerMesh = webVRController.mesh;
             if (controllerMesh) {
@@ -804,6 +850,7 @@ module BABYLON {
                     this._teleportationEnabledOnRightController = true;
                 }
                 webVRController.onPadValuesChangedObservable.add((stateObject) => {
+                    this._teleportBackwardsCheck(stateObject);
                     if (!this._teleportationRequestInitiated) {
                         if (stateObject.y < -this._padSensibilityUp) {
                             // If laser pointer wasn't enabled yet
@@ -1065,21 +1112,27 @@ module BABYLON {
             }
         }
         private _workingVector = Vector3.Zero();
-        private _teleportCamera() {
+        private _workingQuaternion = Quaternion.Identity();
+        private _workingMatrix = Matrix.Identity();
+        private _teleportCamera(location:Nullable<Vector3> = null) {
             if (!(this.currentVRCamera instanceof FreeCamera)) {
                 return;
             }
 
-            // Teleport the hmd to where the user is looking by moving the anchor to where they are looking minus the
-            // offset of the headset from the anchor. Then add the helper's position to account for user's height offset
-            if (this.webVRCamera.leftCamera) {
-                this._workingVector.copyFrom(this.webVRCamera.leftCamera.globalPosition);
-                this._workingVector.subtractInPlace(this.webVRCamera.position);
-                this._haloCenter.subtractToRef(this._workingVector, this._workingVector);
-            } else {
-                this._workingVector.copyFrom(this._haloCenter);
+            if(!location){
+                // Teleport the hmd to where the user is looking by moving the anchor to where they are looking minus the
+                // offset of the headset from the anchor.
+                if (this.webVRCamera.leftCamera) {
+                    this._workingVector.copyFrom(this.webVRCamera.leftCamera.globalPosition);
+                    this._workingVector.subtractInPlace(this.webVRCamera.position);
+                    this._haloCenter.subtractToRef(this._workingVector, this._workingVector);
+                } else {
+                    this._workingVector.copyFrom(this._haloCenter);
+                }
+                location = this._workingVector;
             }
-            this._workingVector.y += this._defaultHeight;
+            // Add height to account for user's height offset
+            location.y += this._defaultHeight;
 
             // Create animation from the camera's position to the new location
             this.currentVRCamera.animations = [];
@@ -1090,7 +1143,7 @@ module BABYLON {
             },
             {
                 frame: 11,
-                value: this._workingVector
+                value: location
             }
             ];
 

+ 1 - 1
src/Cameras/VR/babylon.webVRCamera.ts

@@ -324,7 +324,7 @@ module BABYLON {
             
             // Get current device rotation in babylon world
             Matrix.FromQuaternionToRef(this._deviceRoomRotationQuaternion, this._workingMatrix);
-            this._deviceToWorld.multiplyToRef(this._workingMatrix, this._workingMatrix);
+            this._workingMatrix.multiplyToRef(this._deviceToWorld, this._workingMatrix)
             Quaternion.FromRotationMatrixToRef(this._workingMatrix, this.deviceRotationQuaternion);
 
             super.update();

+ 44 - 0
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -18,6 +18,9 @@
                 Tools.Error("CannonJS is not available. Please make sure you included the js file.");
                 return;
             }
+
+            this._extendNamespace();
+
             this.world = new this.BJSCANNON.World();
             this.world.broadphase = new this.BJSCANNON.NaiveBroadphase();
             this.world.solver.iterations = iterations;
@@ -590,5 +593,46 @@
         public dispose() {
 
         }
+
+        private _extendNamespace() {
+
+            //this will force cannon to execute at least one step when using interpolation
+            let step_tmp1 = new this.BJSCANNON.Vec3();
+            let Engine = this.BJSCANNON;
+            this.BJSCANNON.World.prototype.step = function (dt: number, timeSinceLastCalled: number, maxSubSteps: number) {
+                maxSubSteps = maxSubSteps || 10;
+                timeSinceLastCalled = timeSinceLastCalled || 0;
+                if (timeSinceLastCalled === 0) {
+                    this.internalStep(dt);
+                    this.time += dt;
+                } else {
+                    var internalSteps = Math.floor((this.time + timeSinceLastCalled) / dt) - Math.floor(this.time / dt);
+                    internalSteps = Math.min(internalSteps, maxSubSteps) || 1;
+                    var t0 = performance.now();
+                    for (var i = 0; i !== internalSteps; i++) {
+                        this.internalStep(dt);
+                        if (performance.now() - t0 > dt * 1000) {
+                            break;
+                        }
+                    }
+                    this.time += timeSinceLastCalled;
+                    var h = this.time % dt;
+                    var h_div_dt = h / dt;
+                    var interpvelo = step_tmp1;
+                    var bodies = this.bodies;
+                    for (var j = 0; j !== bodies.length; j++) {
+                        var b = bodies[j];
+                        if (b.type !== Engine.Body.STATIC && b.sleepState !== Engine.Body.SLEEPING) {
+                            b.position.vsub(b.previousPosition, interpvelo);
+                            interpvelo.scale(h_div_dt, interpvelo);
+                            b.position.vadd(interpvelo, b.interpolatedPosition);
+                        } else {
+                            b.interpolatedPosition.copy(b.position);
+                            b.interpolatedQuaternion.copy(b.quaternion);
+                        }
+                    }
+                }
+            };
+        }
     }
 }