Преглед на файлове

added a new CreateTiledGround() function

Celian преди 11 години
родител
ревизия
e6f44d96cd
променени са 4 файла, в които са добавени 184 реда и са изтрити 0 реда
  1. 14 0
      Babylon/Mesh/babylon.mesh.js
  2. 14 0
      Babylon/Mesh/babylon.mesh.ts
  3. 79 0
      Babylon/Mesh/babylon.mesh.vertexData.js
  4. 77 0
      Babylon/Mesh/babylon.mesh.vertexData.ts

+ 14 - 0
Babylon/Mesh/babylon.mesh.js

@@ -812,6 +812,20 @@ var BABYLON;
             return ground;
         };
 
+        Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) {
+            var tiledGround = new BABYLON.GroundMesh(name, scene);
+            tiledGround._setReady(false);
+            tiledGround._subdivisions = subdivisions;
+
+            var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
+
+            vertexData.applyToMesh(tiledGround, updatable);
+
+            tiledGround._setReady(true);
+
+            return tiledGround;
+        };
+
         Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable) {
             var ground = new BABYLON.GroundMesh(name, scene);
             ground._subdivisions = subdivisions;

+ 14 - 0
Babylon/Mesh/babylon.mesh.ts

@@ -821,6 +821,20 @@
             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.GroundMesh(name, scene);
+            tiledGround._setReady(false);
+            tiledGround._subdivisions = subdivisions;
+
+            var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
+
+            vertexData.applyToMesh(tiledGround, updatable);
+
+            tiledGround._setReady(true);
+
+            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 = [];