Browse Source

Updating instances

David Catuhe 11 năm trước cách đây
mục cha
commit
24cfb774b1

+ 4 - 1
Babylon/Lights/Shadows/babylon.shadowGenerator.js

@@ -135,7 +135,10 @@
             // Instances
             if (useInstances) {
                 defines.push("#define INSTANCES");
-                attribs.push("world");
+                attribs.push("world0");
+                attribs.push("world1");
+                attribs.push("world2");
+                attribs.push("world3");
             }
 
             // Get correct effect

+ 4 - 1
Babylon/Lights/Shadows/babylon.shadowGenerator.ts

@@ -145,7 +145,10 @@
             // Instances
             if (useInstances) {
                 defines.push("#define INSTANCES");
-                attribs.push("world");
+                attribs.push("world0");
+                attribs.push("world1");
+                attribs.push("world2");
+                attribs.push("world3");
             }
 
             // Get correct effect      

+ 4 - 1
Babylon/Materials/babylon.standardMaterial.js

@@ -241,7 +241,10 @@ var BABYLON;
                 // Instances
                 if (useInstances) {
                     defines.push("#define INSTANCES");
-                    attribs.push("world");
+                    attribs.push("world0");
+                    attribs.push("world1");
+                    attribs.push("world2");
+                    attribs.push("world3");
                 }
             }
 

+ 4 - 1
Babylon/Materials/babylon.standardMaterial.ts

@@ -244,7 +244,10 @@
                 // Instances
                 if (useInstances) {
                     defines.push("#define INSTANCES");
-                    attribs.push("world");
+                    attribs.push("world0");
+                    attribs.push("world1");
+                    attribs.push("world2");
+                    attribs.push("world3");
                 }
             }
 

+ 4 - 10
Babylon/Mesh/babylon.groundMesh.js

@@ -10,6 +10,7 @@ var BABYLON;
         __extends(GroundMesh, _super);
         function GroundMesh(name, scene) {
             _super.call(this, name, scene);
+            this.generateOctree = false;
             this._worldInverse = new BABYLON.Matrix();
         }
         Object.defineProperty(GroundMesh.prototype, "subdivisions", {
@@ -20,16 +21,9 @@ var BABYLON;
             configurable: true
         });
 
-        GroundMesh.prototype._setReady = function (state) {
-            if (state) {
-                if (this._subdivisions > 10) {
-                    this.subdivide(this._subdivisions);
-
-                    this.createOrUpdateSubmeshesOctree(32);
-                }
-            }
-
-            _super.prototype._setReady.call(this, state);
+        GroundMesh.prototype.optimize = function (chunksCount) {
+            this.subdivide(this._subdivisions);
+            this.createOrUpdateSubmeshesOctree(32);
         };
 
         GroundMesh.prototype.getHeightAtCoordinates = function (x, z) {

+ 5 - 11
Babylon/Mesh/babylon.groundMesh.ts

@@ -1,7 +1,8 @@
 module BABYLON {
     export class GroundMesh extends Mesh {
-        private _worldInverse = new BABYLON.Matrix();
+        public generateOctree = false;
 
+        private _worldInverse = new BABYLON.Matrix();
         public _subdivisions: number;
 
         constructor(name: string, scene: Scene) {
@@ -12,16 +13,9 @@
             return this._subdivisions;
         }
 
-        public _setReady(state: boolean): void {
-            if (state) {
-                if (this._subdivisions > 10) {
-                    this.subdivide(this._subdivisions);
-
-                    this.createOrUpdateSubmeshesOctree(32);
-                }
-            }
-
-            super._setReady(state);
+        public optimize(chunksCount: number): void {
+            this.subdivide(this._subdivisions);
+            this.createOrUpdateSubmeshesOctree(32);
         }
 
         public getHeightAtCoordinates(x: number, z: number): number {

+ 17 - 3
Babylon/Mesh/babylon.mesh.js

@@ -171,6 +171,14 @@ var BABYLON;
         };
 
         Mesh.prototype.setVerticesData = function (kind, data, updatable) {
+            if (kind instanceof Array) {
+                var temp = data;
+                data = kind;
+                kind = temp;
+
+                BABYLON.Tools.Warn("Deprecated usage of setVerticesData detected (since v1.12). Current signature is setVerticesData(kind, data, updatable).");
+            }
+
             if (!this._geometry) {
                 var vertexData = new BABYLON.VertexData();
                 vertexData.set(data, kind);
@@ -310,12 +318,18 @@ var BABYLON;
                 instancesCount++;
             }
 
-            var offsetLocation = effect.getAttributeLocationByName("world");
-            engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocation);
+            var offsetLocation0 = effect.getAttributeLocationByName("world0");
+            var offsetLocation1 = effect.getAttributeLocationByName("world1");
+            var offsetLocation2 = effect.getAttributeLocationByName("world2");
+            var offsetLocation3 = effect.getAttributeLocationByName("world3");
+
+            var offsetLocations = [offsetLocation0, offsetLocation1, offsetLocation2, offsetLocation3];
+
+            engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocations);
 
             this._draw(subMesh, !wireFrame, instancesCount);
 
-            engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocation);
+            engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocations);
         };
 
         Mesh.prototype.render = function (subMesh) {

+ 18 - 4
Babylon/Mesh/babylon.mesh.ts

@@ -170,7 +170,15 @@
             this.synchronizeInstances();
         }
 
-        public setVerticesData(kind: string, data: number[], updatable?: boolean): void {
+        public setVerticesData(kind: any, data: any, updatable?: boolean): void {
+            if (kind instanceof Array) {
+                var temp = data;
+                data = kind;
+                kind = temp;
+
+                Tools.Warn("Deprecated usage of setVerticesData detected (since v1.12). Current signature is setVerticesData(kind, data, updatable).");
+            }
+
             if (!this._geometry) {
                 var vertexData = new BABYLON.VertexData();
                 vertexData.set(data, kind);
@@ -314,12 +322,18 @@
                 instancesCount++;
             }
 
-            var offsetLocation = effect.getAttributeLocationByName("world");
-            engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocation);
+            var offsetLocation0 = effect.getAttributeLocationByName("world0");
+            var offsetLocation1 = effect.getAttributeLocationByName("world1");
+            var offsetLocation2 = effect.getAttributeLocationByName("world2");
+            var offsetLocation3 = effect.getAttributeLocationByName("world3");
+
+            var offsetLocations = [offsetLocation0, offsetLocation1, offsetLocation2, offsetLocation3];
+
+            engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocations);
 
             this._draw(subMesh, !wireFrame, instancesCount);
 
-            engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocation);
+            engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocations);
         }
 
         public render(subMesh: SubMesh): void {

+ 8 - 1
Babylon/Shaders/default.vertex.fx

@@ -22,7 +22,10 @@ attribute vec4 matricesWeights;
 // Uniforms
 
 #ifdef INSTANCES
-attribute mat4 world;
+attribute vec4 world0;
+attribute vec4 world1;
+attribute vec4 world2;
+attribute vec4 world3;
 #else
 uniform mat4 world;
 #endif
@@ -130,8 +133,12 @@ void main(void) {
 #endif 
 
 #else
+#ifdef INSTANCES
+	finalWorld = mat4(world0, world1, world2, world3);
+#else
 	finalWorld = world;
 #endif
+#endif
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 
 	vec4 worldPos = finalWorld * vec4(position, 1.0);

+ 13 - 4
Babylon/Shaders/shadowMap.vertex.fx

@@ -11,7 +11,10 @@ attribute vec4 matricesWeights;
 
 // Uniform
 #ifdef INSTANCES
-attribute mat4 world;
+attribute vec4 world0;
+attribute vec4 world1;
+attribute vec4 world2;
+attribute vec4 world3;
 #else
 uniform mat4 world;
 #endif
@@ -38,18 +41,24 @@ attribute vec2 uv2;
 
 void main(void)
 {
+#ifdef INSTANCES
+	mat4 finalWorld = mat4(world0, world1, world2, world3);
+#else
+	mat4 finalWorld = world;
+#endif
+
 #ifdef BONES
 	mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;
 	mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;
 	mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;
 	mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;
-	mat4 finalWorld = world * (m0 + m1 + m2 + m3);
+	finalWorld = finalWorld * (m0 + m1 + m2 + m3);
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 #else
 #ifndef VSM
-	vPosition = viewProjection * world * vec4(position, 1.0);
+	vPosition = viewProjection * finalWorld * vec4(position, 1.0);
 #endif
-	gl_Position = viewProjection * world * vec4(position, 1.0);
+	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 #endif
 
 #ifdef ALPHATEST

+ 9 - 7
Babylon/babylon.engine.js

@@ -567,22 +567,24 @@
             return buffer;
         };
 
-        Engine.prototype.updateAndBindInstancesBuffer = function (instancesBuffer, data, offsetLocation) {
+        Engine.prototype.updateAndBindInstancesBuffer = function (instancesBuffer, data, offsetLocations) {
             this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
             this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
 
             for (var index = 0; index < 4; index++) {
-                this._gl.enableVertexAttribArray(offsetLocation + index);
-                this._gl.vertexAttribPointer(offsetLocation + index, 4, this._gl.FLOAT, false, 64, index * 16);
-                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation + index, 1);
+                var offsetLocation = offsetLocations[index];
+                this._gl.enableVertexAttribArray(offsetLocation);
+                this._gl.vertexAttribPointer(offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);
+                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 1);
             }
         };
 
-        Engine.prototype.unBindInstancesBuffer = function (instancesBuffer, offsetLocation) {
+        Engine.prototype.unBindInstancesBuffer = function (instancesBuffer, offsetLocations) {
             this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
             for (var index = 0; index < 4; index++) {
-                this._gl.disableVertexAttribArray(offsetLocation + index);
-                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation + index, 0);
+                var offsetLocation = offsetLocations[index];
+                this._gl.disableVertexAttribArray(offsetLocation);
+                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 0);
             }
         };
 

+ 9 - 7
Babylon/babylon.engine.ts

@@ -599,22 +599,24 @@
         }
 
 
-        public updateAndBindInstancesBuffer(instancesBuffer: WebGLBuffer, data: Float32Array, offsetLocation: number): void {
+        public updateAndBindInstancesBuffer(instancesBuffer: WebGLBuffer, data: Float32Array, offsetLocations: number[]): void {
             this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
             this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
 
             for (var index = 0; index < 4; index++) {
-                this._gl.enableVertexAttribArray(offsetLocation + index);
-                this._gl.vertexAttribPointer(offsetLocation + index, 4, this._gl.FLOAT, false, 64, index * 16);                
-                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation + index, 1);
+                var offsetLocation = offsetLocations[index];
+                this._gl.enableVertexAttribArray(offsetLocation);
+                this._gl.vertexAttribPointer(offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);                
+                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 1);
             }
         }
 
-        public unBindInstancesBuffer(instancesBuffer: WebGLBuffer, offsetLocation: number): void {
+        public unBindInstancesBuffer(instancesBuffer: WebGLBuffer, offsetLocations: number[]): void {
             this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
             for (var index = 0; index < 4; index++) {
-                this._gl.disableVertexAttribArray(offsetLocation + index);
-                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation + index, 0);
+                var offsetLocation = offsetLocations[index];
+                this._gl.disableVertexAttribArray(offsetLocation);
+                this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 0);
             }
         }
 

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 13 - 13
babylon.1.12-beta.js