deltakosh 12 năm trước cách đây
mục cha
commit
6efd0ecaa3

+ 7 - 2
Babylon/Culling/babylon.boundingBox.js

@@ -5,7 +5,7 @@
         this.minimum = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
         this.maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
 
-        for (var index = start; index < count; index += stride) {
+        for (var index = start; index < start + count; index += stride) {
             var current = new BABYLON.Vector3(vertices[index], vertices[index + 1], vertices[index + 2]);
 
             this.minimum = BABYLON.Vector3.Minimize(current, this.minimum);
@@ -103,6 +103,12 @@
 
         return true;
     };
+    
+    BABYLON.BoundingBox.prototype.intersectsSphere = function (sphere) {    
+        var vector = BABYLON.Vector3.Clamp(sphere.centerWorld, this.minimumWorld, this.maximumWorld);
+        var num = BABYLON.Vector3.DistanceSquared(sphere.centerWorld, vector);
+        return (num <= (sphere.radiusWorld * sphere.radiusWorld));
+    };
 
     // Statics
     BABYLON.BoundingBox.intersects = function (box0, box1) {
@@ -117,5 +123,4 @@
 
         return true;
     };
-
 })();

+ 1 - 1
Babylon/Culling/babylon.boundingSphere.js

@@ -5,7 +5,7 @@
         var minimum = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
         var maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
 
-        for (var index = start; index < count; index += stride) {
+        for (var index = start; index < start + count; index += stride) {
             var current = new BABYLON.Vector3(vertices[index], vertices[index + 1], vertices[index + 2]);
 
             minimum = BABYLON.Vector3.Minimize(current, minimum);

+ 6 - 2
Babylon/Layer/babylon.layer.js

@@ -1,10 +1,11 @@
 var BABYLON = BABYLON || {};
 
 (function () {
-    BABYLON.Layer = function (name, imgUrl, scene, isBackground) {
+    BABYLON.Layer = function (name, imgUrl, scene, isBackground, color) {
         this.name = name;
         this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
         this.isBackground = isBackground === undefined ? true : isBackground;
+        this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
 
         this._scene = scene;
         this._scene.layers.push(this);
@@ -34,7 +35,7 @@
         // Effects
         this._effect = this._scene.getEngine().createEffect("layer",
                     ["position"],
-                    ["textureMatrix"],
+                    ["textureMatrix", "color"],
                     ["textureSampler"], "");
     };
 
@@ -56,6 +57,9 @@
         this._effect.setTexture("textureSampler", this.texture);
         this._effect.setMatrix("textureMatrix", this.texture._computeTextureMatrix());
 
+        // Color
+        this._effect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
+
         // VBOs
         engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
 

+ 27 - 2
Babylon/Mesh/babylon.mesh.js

@@ -60,6 +60,11 @@
     BABYLON.Mesh.prototype.onDispose = false;
 
     // Properties
+
+    BABYLON.Mesh.prototype.getBoundingInfo = function () {
+        return this._boundingInfo;
+    };
+
     BABYLON.Mesh.prototype.getScene = function () {
         return this._scene;
     };
@@ -223,7 +228,25 @@
         this.subMeshes = [];
         return new BABYLON.SubMesh(0, 0, this._totalVertices, 0, this._indices.length, this);
     };
+    
+
+    BABYLON.Mesh.prototype.subdivide = function(count) {
+        if (count < 1) {
+            return;
+        }
+        
+        var subdivisionSize = this._indices.length / count;
+        var offset = 0;
+        
+        this.subMeshes = [];
+        for (var index = 0; index < count; index++)
+        {
+            BABYLON.SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, this._indices.length - offset), this);
 
+            offset += subdivisionSize;
+        }
+    };
+    
     BABYLON.Mesh.prototype.setVertices = function (vertices, uvCount, updatable) {
         if (this._vertexBuffer) {
             this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
@@ -251,6 +274,8 @@
     BABYLON.Mesh.prototype.updateVertices = function(vertices) {
         var engine = this._scene.getEngine();
         engine.updateDynamicVertexBuffer(this._vertexBuffer, vertices);
+        this._vertices = vertices;
+        this._positions = null;
     };
 
     BABYLON.Mesh.prototype.setIndices = function (indices) {
@@ -733,10 +758,10 @@
             for (col = 0; col <= subdivisions; col++)
             {
                 var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
-                var normal = new BABYLON.Vector3(0, 1, 0);
+                var normal = new BABYLON.Vector3(0, 1.0, 0);
 
                 vertices.push(  position.x, position.y, position.z, 
-                                normal.x, normal.y, normal.y,
+                                normal.x, normal.y, normal.z,
                                 col / subdivisions, row / subdivisions);
             }
         }

+ 25 - 2
Babylon/Mesh/babylon.subMesh.js

@@ -11,10 +11,14 @@
         this.indexCount = indexCount;
 
         var stride = this._mesh.getFloatVertexStrideSize();
-        this._boundingInfo = new BABYLON.BoundingInfo(this._mesh.getVertices(), stride, verticesStart * stride, (verticesStart + verticesCount) * stride);
+        this._boundingInfo = new BABYLON.BoundingInfo(this._mesh.getVertices(), stride, verticesStart * stride, verticesCount * stride);
     };
     
     //Properties
+    BABYLON.SubMesh.prototype.getBoundingInfo = function() {
+        return this._boundingInfo;
+    };
+
     BABYLON.SubMesh.prototype.getMaterial = function () {
         var rootMaterial = this._mesh.material;
         
@@ -63,7 +67,7 @@
     };
 
     BABYLON.SubMesh.prototype.canIntersects = function(ray) {
-        return ray.intersectsSphere(this._boundingInfo.boundingSphere);
+        return ray.intersectsBox(this._boundingInfo.boundingBox);
     };
 
     BABYLON.SubMesh.prototype.intersects = function (ray, positions, indices) {
@@ -94,4 +98,23 @@
     BABYLON.SubMesh.prototype.clone = function(newMesh) {
         return new BABYLON.SubMesh(this.materialIndex, this.verticesStart, this.verticesCount, this.indexStart, this.indexCount, newMesh);
     };
+    
+    // Statics
+    BABYLON.SubMesh.CreateFromIndices = function(materialIndex, startIndex, indexCount, mesh) {
+        var minVertexIndex = Number.MAX_VALUE;
+        var maxVertexIndex = -Number.MAX_VALUE;
+
+        var indices = mesh.getIndices();
+
+        for (var index = startIndex; index < startIndex + indexCount; index++) {
+            var vertexIndex = indices[index];
+
+            if (vertexIndex < minVertexIndex)
+                minVertexIndex = vertexIndex;
+            else if (vertexIndex > maxVertexIndex)
+                maxVertexIndex = vertexIndex;
+        }
+
+        return new BABYLON.SubMesh(materialIndex, minVertexIndex, maxVertexIndex - minVertexIndex, startIndex, indexCount, mesh);
+    };
 })();

+ 3 - 1
Babylon/Shaders/layer.fragment.fx

@@ -6,9 +6,11 @@ precision mediump float;
 varying vec2 vUV;
 uniform sampler2D textureSampler;
 
+// Color
+uniform vec4 color;
 
 void main(void) {
 	vec4 baseColor = texture2D(textureSampler, vUV);
 
-	gl_FragColor = baseColor;
+	gl_FragColor = baseColor * color;
 }

+ 4 - 0
Babylon/Shaders/sprites.fragment.fx

@@ -4,6 +4,8 @@ precision mediump float;
 
 uniform bool alphaTest;
 
+varying vec4 vColor;
+
 // Samplers
 varying vec2 vUV;
 uniform sampler2D diffuseSampler;
@@ -55,6 +57,8 @@ void main(void) {
 			discard;
 	}
 
+	baseColor *= vColor;
+
 #ifdef FOG
 	float fog = CalcFogFactor();
 	baseColor.rgb = fog * baseColor.rgb + (1.0 - fog) * vFogColor;

+ 5 - 0
Babylon/Shaders/sprites.vertex.fx

@@ -2,6 +2,7 @@
 attribute vec3 position;
 attribute vec4 options;
 attribute vec4 cellInfo;
+attribute vec4 color;
 
 // Uniforms
 uniform vec2 textureInfos;
@@ -10,6 +11,7 @@ uniform mat4 projection;
 
 // Output
 varying vec2 vUV;
+varying vec4 vColor;
 
 #ifdef FOG
 varying float fFogDistance;
@@ -35,6 +37,9 @@ void main(void) {
 	// Position
 	viewPos += rotatedCorner;
 	gl_Position = projection * vec4(viewPos, 1.0);   
+
+	// Color
+	vColor = color;
 	
 	// Texture
 	vec2 uvOffset = vec2(abs(offset.x - cellInfo.x), 1.0 - abs(offset.y - cellInfo.y));

+ 1 - 0
Babylon/Sprites/babylon.sprite.js

@@ -8,6 +8,7 @@
         this._manager.sprites.push(this);
 
         this.position = BABYLON.Vector3.Zero();
+        this.color = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
 
         this._frameCount = 0;
     };

+ 9 - 4
Babylon/Sprites/babylon.spriteManager.js

@@ -24,6 +24,11 @@
         var offset = (sprite.cellIndex / rowSize) >> 0;
         vertices.push(sprite.cellIndex - offset * rowSize);
         vertices.push(offset);
+        // Color
+        vertices.push(sprite.color.r);
+        vertices.push(sprite.color.g);
+        vertices.push(sprite.color.b);
+        vertices.push(sprite.color.a);
     };
 
     BABYLON.SpriteManager = function (name, imgUrl, capacity, cellSize, scene, epsilon) {
@@ -39,8 +44,8 @@
         this._scene.spriteManagers.push(this);
         
         // VBO
-        this._vertexDeclaration = [3, 4, 4];
-        this._vertexStrideSize = 11 * 4; // 11 floats per sprite (x, y, z, angle, size, offsetX, offsetY, invertU, invertV, cellIndexX, cellIndexY)
+        this._vertexDeclaration = [3, 4, 4, 4];
+        this._vertexStrideSize = 15 * 4; // 15 floats per sprite (x, y, z, angle, size, offsetX, offsetY, invertU, invertV, cellIndexX, cellIndexY, color)
         this._vertexBuffer = scene.getEngine().createDynamicVertexBuffer(capacity * this._vertexStrideSize * 4);
 
         var indices = [];
@@ -62,12 +67,12 @@
         
         // Effects
         this._effectBase = this._scene.getEngine().createEffect("sprites",
-                    ["position", "options", "cellInfo"],
+                    ["position", "options", "cellInfo", "color"],
                     ["view", "projection", "textureInfos", "alphaTest"],
                     ["diffuseSampler"], "");
         
         this._effectFog = this._scene.getEngine().createEffect("sprites",
-                    ["position", "options", "cellInfo"],
+                    ["position", "options", "cellInfo", "color"],
                     ["view", "projection", "textureInfos", "alphaTest", "vFogInfos", "vFogColor"],
                     ["diffuseSampler"], "#define FOG");
     };

+ 93 - 0
Babylon/Tools/babylon.math.js

@@ -9,6 +9,99 @@
     };
 
     // Methods
+    BABYLON.Ray.prototype.intersectsBox = function (box) {
+        var d = 0.0;
+        var maxValue = Number.MAX_VALUE;
+
+        if (Math.abs(this.direction.x) < 0.0000001)
+        {
+            if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            var inv = 1.0 / this.direction.x;
+            var min = (box.minimum.x - this.origin.x) * inv;
+            var max = (box.maximum.x - this.origin.x) * inv;
+
+            if (min > max)
+            {
+                var temp = min;
+                min = max;
+                max = temp;
+            }
+
+            d = Math.max(min, d);
+            maxValue = Math.min(max, maxValue);
+
+            if (d > maxValue)
+            {
+                return false;
+            }
+        }
+
+        if (Math.abs(this.direction.y) < 0.0000001)
+        {
+            if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            var inv = 1.0 / this.direction.y;
+            var min = (box.minimum.y - this.origin.y) * inv;
+            var max = (box.maximum.y - this.origin.y) * inv;
+
+            if (min > max)
+            {
+                var temp = min;
+                min = max;
+                max = temp;
+            }
+
+            d = Math.max(min, d);
+            maxValue = Math.min(max, maxValue);
+
+            if (d > maxValue)
+            {
+                return false;
+            }
+        }
+
+        if (Math.abs(this.direction.z) < 0.0000001)
+        {
+            if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z)
+            {
+                return false;
+            }
+        }
+        else
+        {
+            var inv = 1.0 / this.direction.z;
+            var min = (box.minimum.z - this.origin.z) * inv;
+            var max = (box.maximum.z - this.origin.z) * inv;
+
+            if (min > max)
+            {
+                var temp = min;
+                min = max;
+                max = temp;
+            }
+
+            d = Math.max(min, d);
+            maxValue = Math.min(max, maxValue);
+
+            if (d > maxValue)
+            {
+                return false;
+            }
+        }
+        return true;        
+    };
+    
     BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
         var x = sphere.center.x - this.origin.x;
         var y = sphere.center.y - this.origin.y;

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


+ 2 - 0
what's new.txt

@@ -1,3 +1,5 @@
+1.0.5:
+ - Adding color tint for sprites and layers
 1.0.4:
  - Various optimizations
  - Fog