Explorar o código

Merge pull request #239 from kostar111/master

CreateTiledGround() function
deltakosh %!s(int64=11) %!d(string=hai) anos
pai
achega
59f54f0af3

+ 27 - 3
Babylon/Mesh/babylon.geometry.js

@@ -510,20 +510,21 @@ var BABYLON;
 
             var Cylinder = (function (_super) {
                 __extends(Cylinder, _super);
-                function Cylinder(id, scene, height, diameterTop, diameterBottom, tessellation, canBeRegenerated, mesh) {
+                function Cylinder(id, scene, height, diameterTop, diameterBottom, tessellation, subdivisions, canBeRegenerated, mesh) {
                     this.height = height;
                     this.diameterTop = diameterTop;
                     this.diameterBottom = diameterBottom;
                     this.tessellation = tessellation;
+                    this.subdivisions = subdivisions;
 
                     _super.call(this, id, scene, this._regenerateVertexData(), canBeRegenerated, mesh);
                 }
                 Cylinder.prototype._regenerateVertexData = function () {
-                    return BABYLON.VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation);
+                    return BABYLON.VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions);
                 };
 
                 Cylinder.prototype.copy = function (id) {
-                    return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.canBeRegenerated(), null);
+                    return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions, this.canBeRegenerated(), null);
                 };
                 return Cylinder;
             })(_Primitive);
@@ -569,6 +570,29 @@ var BABYLON;
             })(_Primitive);
             Primitives.Ground = Ground;
 
+            var TiledGround = (function (_super) {
+                __extends(TiledGround, _super);
+                function TiledGround(id, scene, xmin, zmin, xmax, zmax, subdivisions, precision, canBeRegenerated, mesh) {
+                    this.xmin = xmin;
+                    this.zmin = zmin;
+                    this.xmax = xmax;
+                    this.zmax = zmax;
+                    this.subdivisions = subdivisions;
+                    this.precision = precision;
+
+                    _super.call(this, id, scene, this._regenerateVertexData(), canBeRegenerated, mesh);
+                }
+                TiledGround.prototype._regenerateVertexData = function () {
+                    return BABYLON.VertexData.CreateTiledGround(this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision);
+                };
+
+                TiledGround.prototype.copy = function (id) {
+                    return new TiledGround(id, this.getScene(), this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision, this.canBeRegenerated(), null);
+                };
+                return TiledGround;
+            })(_Primitive);
+            Primitives.TiledGround = TiledGround;
+
             var Plane = (function (_super) {
                 __extends(Plane, _super);
                 function Plane(id, scene, size, canBeRegenerated, mesh) {

+ 34 - 3
Babylon/Mesh/babylon.geometry.ts

@@ -527,22 +527,24 @@
             public diameterTop: number;
             public diameterBottom: number;
             public tessellation: number;
+            public subdivisions: number;
 
-            constructor(id: string, scene: Scene, height: number, diameterTop: number, diameterBottom: number, tessellation: number, canBeRegenerated?: boolean, mesh?: Mesh) {
+            constructor(id: string, scene: Scene, height: number, diameterTop: number, diameterBottom: number, tessellation: number, subdivisions: number, canBeRegenerated?: boolean, mesh?: Mesh) {
                 this.height = height;
                 this.diameterTop = diameterTop;
                 this.diameterBottom = diameterBottom;
                 this.tessellation = tessellation;
+                this.subdivisions = subdivisions;
 
                 super(id, scene, this._regenerateVertexData(), canBeRegenerated, mesh);
             }
 
             public _regenerateVertexData(): VertexData {
-                return VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation);
+                return VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions);
             }
 
             public copy(id: string): Geometry {
-                return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.canBeRegenerated(), null);
+                return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions, this.canBeRegenerated(), null);
             }
         }
 
@@ -592,6 +594,35 @@
             }
         }
 
+        export class TiledGround extends _Primitive {
+            // Members
+            public xmin: number;
+            public zmin: number;
+            public xmax: number;
+            public zmax: number;
+            public subdivisions: {w: number; h: number;};
+            public precision:    {w: number; h: number;};
+
+            constructor(id: string, scene: Scene, xmin: number, zmin: number, xmax: number, zmax: number, subdivisions: {w: number; h: number;}, precision: {w: number; h: number;}, canBeRegenerated?: boolean, mesh?: Mesh) {
+                this.xmin = xmin;
+                this.zmin = zmin;
+                this.xmax = xmax;
+                this.zmax = zmax;
+                this.subdivisions  = subdivisions;
+                this.precision     = precision;
+
+                super(id, scene, this._regenerateVertexData(), canBeRegenerated, mesh);
+            }
+
+            public _regenerateVertexData(): VertexData {
+                return VertexData.CreateTiledGround(this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision);
+            }
+
+            public copy(id: string): Geometry {
+                return new TiledGround(id, this.getScene(), this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision, this.canBeRegenerated(), null);
+            }
+        }
+
         export class Plane extends _Primitive {
             // Members
             public size: number;

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

@@ -749,9 +749,9 @@ var BABYLON;
         };
 
         // Cylinder and cone (Code inspired by SharpDX.org)
-        Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, scene, updatable) {
+        Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable) {
             var cylinder = new BABYLON.Mesh(name, scene);
-            var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation);
+            var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation, subdivisions);
 
             vertexData.applyToMesh(cylinder, updatable);
 
@@ -812,6 +812,16 @@ var BABYLON;
             return ground;
         };
 
+        Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) {
+            var tiledGround = new BABYLON.Mesh(name, scene);
+
+            var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
+
+            vertexData.applyToMesh(tiledGround, updatable);
+
+            return tiledGround;
+        };
+
         Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable) {
             var ground = new BABYLON.GroundMesh(name, scene);
             ground._subdivisions = subdivisions;

+ 12 - 2
Babylon/Mesh/babylon.mesh.ts

@@ -758,9 +758,9 @@
         }
 
         // Cylinder and cone (Code inspired by SharpDX.org)
-        public static CreateCylinder(name: string, height: number, diameterTop: number, diameterBottom: number, tessellation: number, scene: Scene, updatable?: boolean): Mesh {
+        public static CreateCylinder(name: string, height: number, diameterTop: number, diameterBottom: number, tessellation: number, subdivisions: number, scene: Scene, updatable?: boolean): Mesh {
             var cylinder = new BABYLON.Mesh(name, scene);
-            var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation);
+            var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation, subdivisions);
 
             vertexData.applyToMesh(cylinder, updatable);
 
@@ -821,6 +821,16 @@
             return ground;
         }
 
+        public static CreateTiledGround(name: string, xmin: number, zmin: number, xmax: number, zmax: number, subdivisions: {w: number; h: number;}, precision: {w: number; h: number;}, scene: Scene, updatable?: boolean): Mesh {
+            var tiledGround = new BABYLON.Mesh(name, scene);
+
+            var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
+
+            vertexData.applyToMesh(tiledGround, updatable);
+
+            return tiledGround;
+        }
+
         public static CreateGroundFromHeightMap(name: string, url: string, width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, scene: Scene, updatable?: boolean): GroundMesh {
             var ground = new BABYLON.GroundMesh(name, scene);
             ground._subdivisions = subdivisions;

+ 79 - 0
Babylon/Mesh/babylon.mesh.vertexData.js

@@ -639,6 +639,85 @@
             return vertexData;
         };
 
+        VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
+            if (typeof subdivisions === "undefined") { subdivisions = { w: 1, h: 1 }; }
+            if (typeof precision === "undefined") { precision = { w: 1, h: 1 }; }
+            var indices = [];
+            var positions = [];
+            var normals = [];
+            var uvs = [];
+            var row, col, tileRow, tileCol;
+
+            subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
+            subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
+            precision.w    = (precision.w < 1) ? 1 : precision.w;
+            precision.h    = (precision.h < 1) ? 1 : precision.h;
+
+            var tileSize = {
+                'w' : (xmax - xmin) / subdivisions.w,
+                'h' : (zmax - zmin) / subdivisions.h
+            };
+
+            for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
+                for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
+                    applyTile(
+                        xmin + tileCol * tileSize.w,
+                        zmin + tileRow * tileSize.h,
+                        xmin + (tileCol + 1) * tileSize.w,
+                        zmin + (tileRow + 1) * tileSize.h
+                    );
+                }
+            }
+
+            function applyTile (xTileMin, zTileMin, xTileMax, zTileMax) {
+                // Indices
+                var base = positions.length / 3;
+                var rowLength = precision.w + 1;
+                for (row = 0; row < precision.h; row++) {
+                    for (col = 0; col < precision.w; col++) {
+                        var square = [
+                            base +  col + row * rowLength,
+                            base + (col + 1) + row * rowLength,
+                            base + (col + 1) + (row + 1) * rowLength,
+                            base +  col + (row + 1) * rowLength
+                        ];
+
+                        indices.push(square[1]);
+                        indices.push(square[2]);
+                        indices.push(square[3]);
+                        indices.push(square[0]);
+                        indices.push(square[1]);
+                        indices.push(square[3]);
+                    }
+                }
+
+                // Position, normals and uvs
+                var position = new BABYLON.Vector3.Zero();
+                var normal = new BABYLON.Vector3(0, 1.0, 0);
+                for (row = 0; row <= precision.h; row++) {
+                    position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
+                    for (col = 0; col <= precision.w; col++) {
+                        position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
+                        position.y = 0;
+
+                        positions.push(position.x, position.y, position.z);
+                        normals.push(normal.x, normal.y, normal.z);
+                        uvs.push(col / precision.w, row / precision.h);
+                    }
+                }
+            }
+
+            // Result
+            var vertexData = new BABYLON.VertexData();
+
+            vertexData.indices = indices;
+            vertexData.positions = positions;
+            vertexData.normals = normals;
+            vertexData.uvs = uvs;
+
+            return vertexData;
+        };
+
         VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
             var indices = [];
             var positions = [];

+ 77 - 0
Babylon/Mesh/babylon.mesh.vertexData.ts

@@ -661,6 +661,83 @@
             return vertexData;
         }
 
+        public static CreateTiledGround (xmin: number, zmin: number, xmax: number, zmax: number, subdivisions = {w: 1, h: 1}, precision = {w: 1, h: 1}): VertexData {
+            var indices = [];
+            var positions = [];
+            var normals = [];
+            var uvs = [];
+            var row, col, tileRow, tileCol;
+
+            subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
+            subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
+            precision.w    = (precision.w < 1) ? 1 : precision.w;
+            precision.h    = (precision.h < 1) ? 1 : precision.h;
+
+            var tileSize = {
+                'w' : (xmax - xmin) / subdivisions.w,
+                'h' : (zmax - zmin) / subdivisions.h
+            };
+
+            for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
+                for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
+                    applyTile(
+                        xmin + tileCol * tileSize.w,
+                        zmin + tileRow * tileSize.h,
+                        xmin + (tileCol + 1) * tileSize.w,
+                        zmin + (tileRow + 1) * tileSize.h
+                    );
+                }
+            }
+
+            function applyTile (xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
+                // Indices
+                var base = positions.length / 3;
+                var rowLength = precision.w + 1;
+                for (row = 0; row < precision.h; row++) {
+                    for (col = 0; col < precision.w; col++) {
+                        var square = [
+                            base +  col + row * rowLength,
+                            base + (col + 1) + row * rowLength,
+                            base + (col + 1) + (row + 1) * rowLength,
+                            base +  col + (row + 1) * rowLength
+                        ];
+
+                        indices.push(square[1]);
+                        indices.push(square[2]);
+                        indices.push(square[3]);
+                        indices.push(square[0]);
+                        indices.push(square[1]);
+                        indices.push(square[3]);
+                    }
+                }
+
+                // Position, normals and uvs
+                var position = new BABYLON.Vector3.Zero();
+                var normal = new BABYLON.Vector3(0, 1.0, 0);
+                for (row = 0; row <= precision.h; row++) {
+                    position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
+                    for (col = 0; col <= precision.w; col++) {
+                        position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
+                        position.y = 0;
+
+                        positions.push(position.x, position.y, position.z);
+                        normals.push(normal.x, normal.y, normal.z);
+                        uvs.push(col / precision.w, row / precision.h);
+                    }
+                }
+            }
+
+            // Result
+            var vertexData = new BABYLON.VertexData();
+
+            vertexData.indices = indices;
+            vertexData.positions = positions;
+            vertexData.normals = normals;
+            vertexData.uvs = uvs;
+
+            return vertexData;
+        }
+
         public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth:  number, bufferHeight: number): VertexData {
             var indices = [];
             var positions = [];