فهرست منبع

Merge pull request #721 from jbousquie/fix.CreateXXX_with_options

Fix.create xxx with options
David Catuhe 10 سال پیش
والد
کامیت
e12fd4d11c
3فایلهای تغییر یافته به همراه111 افزوده شده و 28 حذف شده
  1. 24 0
      src/Mesh/babylon.geometry.ts
  2. 75 22
      src/Mesh/babylon.mesh.ts
  3. 12 6
      src/Mesh/babylon.mesh.vertexData.ts

+ 24 - 0
src/Mesh/babylon.geometry.ts

@@ -611,6 +611,30 @@
             }
         }
 
+        export class Disc extends _Primitive {
+            // Members
+            public radius: number;
+            public tessellation: number;
+            public side: number;
+
+            constructor(id: string, scene: Scene, radius: number, tessellation: number, canBeRegenerated?: boolean, mesh?: Mesh, side: number = Mesh.DEFAULTSIDE) {
+                this.radius = radius;
+                this.tessellation = tessellation;
+                this.side = side;
+
+                super(id, scene, this._regenerateVertexData(), canBeRegenerated, mesh);
+            }
+
+            public _regenerateVertexData(): VertexData {
+                return VertexData.CreateDisc({ radius: this.radius, tessellation: this.tessellation, sideOrientation: this.side });
+            }
+
+            public copy(id: string): Geometry {
+                return new Disc(id, this.getScene(), this.radius, this.tessellation, this.canBeRegenerated(), null, this.side);
+            }
+        }
+
+
         export class Cylinder extends _Primitive {
             // Members
             public height: number;

+ 75 - 22
src/Mesh/babylon.mesh.ts

@@ -1261,12 +1261,14 @@
                 }
             } else {
                 pathArray = options;
-                options = {
-                    pathArray: pathArray,
-                    closeArray: closeArrayOrScene,
-                    closePath: closePath,
-                    offset: offset,
-                    sideOrientation: sideOrientation
+                if (!instance) {
+                    options = {
+                        pathArray: pathArray,
+                        closeArray: closeArrayOrScene,
+                        closePath: closePath,
+                        offset: offset,
+                        sideOrientation: sideOrientation
+                    }
                 }
             }
 
@@ -1342,6 +1344,7 @@
                     (<any>ribbon)._idx = (<any>vertexData)._idx;
                 }
                 (<any>ribbon)._closePath = closePath;
+                (<any>ribbon)._closeArray = closeArray;
 
                 vertexData.applyToMesh(ribbon, updatable);
 
@@ -1349,11 +1352,23 @@
             }
         }
 
-        public static CreateDisc(name: string, radius: number, tessellation: number, scene: Scene, updatable?: boolean, sideOrientation: number = Mesh.DEFAULTSIDE): Mesh {
+        public static CreateDisc(name: string, radius: number, tessellation: number, scene: Scene, updatable?: boolean, sideOrientation?: number): Mesh;
+        public static CreateDisc(name: string, options: { radius: number, tessellation: number, updatable?: boolean, sideOrientation?: number }, scene: Scene): Mesh;
+        public static CreateDisc(name: string, options: any, tessellationOrScene: any, scene?: Scene, updatable?: boolean, sideOrientation: number = Mesh.DEFAULTSIDE): Mesh {
+            if (tessellationOrScene instanceof Scene) {
+                scene = tessellationOrScene;
+            } else {
+                var radius = options;
+                options = {
+                    radius: radius,
+                    tessellation: tessellationOrScene,
+                    sideOrientation: sideOrientation
+                }
+            }
             var disc = new Mesh(name, scene);
-            var vertexData = VertexData.CreateDisc(radius, tessellation, sideOrientation);
+            var vertexData = VertexData.CreateDisc(options);
 
-            vertexData.applyToMesh(disc, updatable);
+            vertexData.applyToMesh(disc, updatable || options.updatable);
 
             return disc;
         }
@@ -1492,8 +1507,23 @@
         }
 
         // Lines
-        public static CreateLines(name: string, points: Vector3[], scene: Scene, updatable?: boolean, linesInstance: LinesMesh = null): LinesMesh {
-            if (linesInstance) { // lines update
+        public static CreateLines(name: string, points: Vector3[], scene: Scene, updatable?: boolean, instance?: LinesMesh): LinesMesh;
+        public static CreateLines(name: string, options: { points: Vector3[], updatable?: boolean, instance?: LinesMesh }, scene: Scene): LinesMesh;
+        public static CreateLines(name: string, options: any, scene: Scene, updatable?: boolean, instance?: LinesMesh): LinesMesh {
+            var points: Vector3[];
+            if (Array.isArray(options)) {
+                points = options;
+                if (!instance) {
+                    options = {
+                        points: points
+                    }
+                }
+            } else {
+                instance = options.instance;
+                points = options.points;
+            }
+
+            if (instance) { // lines update
                 var positionFunction = positions => {
                     var i = 0;
                     for (var p = 0; p < points.length; p++) {
@@ -1503,20 +1533,43 @@
                         i += 3;
                     }
                 };
-                linesInstance.updateMeshPositions(positionFunction, false);
-                return linesInstance;
+                instance.updateMeshPositions(positionFunction, false);
+                return instance;
             }
 
             // lines creation
             var lines = new LinesMesh(name, scene);
-            var vertexData = VertexData.CreateLines(points);
-            vertexData.applyToMesh(lines, updatable);
+            var vertexData = VertexData.CreateLines(options);
+            vertexData.applyToMesh(lines, updatable || options.updatable);
             return lines;
         }
 
         // Dashed Lines
-        public static CreateDashedLines(name: string, points: Vector3[], dashSize: number, gapSize: number, dashNb: number, scene: Scene, updatable?: boolean, linesInstance: LinesMesh = null): LinesMesh {
-            if (linesInstance) {  //  dashed lines update
+        public static CreateDashedLines(name: string, points: Vector3[], dashSize: number, gapSize: number, dashNb: number, scene: Scene, updatable?: boolean, instance?: LinesMesh): LinesMesh;
+        public static CreateDashedLines(name: string, options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number, updatable?: boolean, instance?: LinesMesh }, scene: Scene): LinesMesh;
+        public static CreateDashedLines(name: string, options: any, dashSizeOrScene: any, gapSize?: number, dashNb?: number, scene?: Scene, updatable?: boolean, instance?: LinesMesh):LinesMesh {
+            var points: Vector3[];
+            var dashSize: number;
+            if (Array.isArray(options)) {
+                points = options;
+                dashSize = dashSizeOrScene;
+                if (!instance) {
+                    options = {
+                        points: points,
+                        dashSize: dashSize,
+                        gapSize: gapSize,
+                        dashNb: dashNb
+                    }    
+                }
+            } else {
+                scene = dashSizeOrScene,
+                points = options.points;
+                instance = options.instance;
+                gapSize = options.gapSize;
+                dashNb = options.dashNb;
+                dashSize = options.dashSize;
+            }
+            if (instance) {  //  dashed lines update
                 var positionFunction = (positions: number[]): void => {
                     var curvect = Vector3.Zero();
                     var nbSeg = positions.length / 6;
@@ -1533,7 +1586,7 @@
                         lg += curvect.length();
                     }
                     shft = lg / nbSeg;
-                    dashshft = (<any>linesInstance).dashSize * shft / ((<any>linesInstance).dashSize + (<any>linesInstance).gapSize);
+                    dashshft = (<any>instance).dashSize * shft / ((<any>instance).dashSize + (<any>instance).gapSize);
                     for (i = 0; i < points.length - 1; i++) {
                         points[i + 1].subtractToRef(points[i], curvect);
                         nb = Math.floor(curvect.length() / shft);
@@ -1558,13 +1611,13 @@
                         p += 3;
                     }
                 };
-                linesInstance.updateMeshPositions(positionFunction, false);
-                return linesInstance;
+                instance.updateMeshPositions(positionFunction, false);
+                return instance;
             }
             // dashed lines creation
             var dashedLines = new LinesMesh(name, scene);
-            var vertexData = VertexData.CreateDashedLines(points, dashSize, gapSize, dashNb);
-            vertexData.applyToMesh(dashedLines, updatable);
+            var vertexData = VertexData.CreateDashedLines(options);
+            vertexData.applyToMesh(dashedLines, updatable || options.updatable);
             (<any>dashedLines).dashSize = dashSize;
             (<any>dashedLines).gapSize = gapSize;
             return dashedLines;

+ 12 - 6
src/Mesh/babylon.mesh.vertexData.ts

@@ -937,9 +937,10 @@
             return vertexData;
         }
 
-        public static CreateLines(points: Vector3[]): VertexData {
+        public static CreateLines(options: { points: Vector3[]}): VertexData {
             var indices = [];
             var positions = [];
+            var points = options.points;
 
             for (var index = 0; index < points.length; index++) {
                 positions.push(points[index].x, points[index].y, points[index].z);
@@ -959,10 +960,11 @@
             return vertexData;
         }
 
-        public static CreateDashedLines(points: Vector3[], dashSize: number, gapSize: number, dashNb: number): VertexData {
-            dashSize = dashSize || 3;
-            gapSize = gapSize || 1;
-            dashNb = dashNb || 200;
+        public static CreateDashedLines(options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number }): VertexData {
+            var dashSize = options.dashSize || 3;
+            var gapSize = options.gapSize || 1;
+            var dashNb = options.dashNb || 200;
+            var points = options.points;
 
             var positions = new Array<number>();
             var indices = new Array<number>();
@@ -1236,12 +1238,16 @@
             return vertexData;
         }
 
-        public static CreateDisc(radius: number, tessellation: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
+        public static CreateDisc(options: { radius?: number, tessellation?: number, sideOrientation?: number }): VertexData {
             var positions = [];
             var indices = [];
             var normals = [];
             var uvs = [];
 
+            var radius = options.radius || 0.5;
+            var tessellation = options.tessellation || 64;
+            var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
+
             // positions and uvs
             positions.push(0, 0, 0);    // disc center first
             uvs.push(0.5, 0.5);