Ver código fonte

Moving clone features to mesh's constructor
moving general stuff from PolygonMeshBuilder to Math

David Catuhe 10 anos atrás
pai
commit
ecebd9dc71

+ 116 - 0
Babylon/Math/babylon.math.js

@@ -2548,5 +2548,121 @@
         return BezierCurve;
     })();
     BABYLON.BezierCurve = BezierCurve;
+
+    (function (Orientation) {
+        Orientation[Orientation["CW"] = 0] = "CW";
+        Orientation[Orientation["CCW"] = 1] = "CCW";
+    })(BABYLON.Orientation || (BABYLON.Orientation = {}));
+    var Orientation = BABYLON.Orientation;
+
+    var Angle = (function () {
+        function Angle(radians) {
+            var _this = this;
+            this.degrees = function () {
+                return _this._radians * 180 / Math.PI;
+            };
+            this.radians = function () {
+                return _this._radians;
+            };
+            this._radians = radians;
+            if (this._radians < 0)
+                this._radians += (2 * Math.PI);
+        }
+        Angle.BetweenTwoPoints = function (a, b) {
+            var delta = b.subtract(a);
+            var theta = Math.atan2(delta.y, delta.x);
+            return new Angle(theta);
+        };
+
+        Angle.FromRadians = function (radians) {
+            return new Angle(radians);
+        };
+
+        Angle.FromDegrees = function (degrees) {
+            return new Angle(degrees * Math.PI / 180);
+        };
+        return Angle;
+    })();
+    BABYLON.Angle = Angle;
+
+    var Arc = (function () {
+        function Arc(startPoint, midPoint, endPoint) {
+            this.startPoint = startPoint;
+            this.midPoint = midPoint;
+            this.endPoint = endPoint;
+            var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
+            var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
+            var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
+            var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
+
+            this.centerPoint = new Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
+
+            this.radius = this.centerPoint.subtract(this.startPoint).length();
+
+            this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
+
+            var a1 = this.startAngle.degrees();
+            var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
+            var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
+
+            // angles correction
+            if (a2 - a1 > +180.0)
+                a2 -= 360.0;
+            if (a2 - a1 < -180.0)
+                a2 += 360.0;
+            if (a3 - a2 > +180.0)
+                a3 -= 360.0;
+            if (a3 - a2 < -180.0)
+                a3 += 360.0;
+
+            this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
+            this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
+        }
+        return Arc;
+    })();
+    BABYLON.Arc = Arc;
+
+    var Path = (function () {
+        function Path(x, y) {
+            this._points = [];
+            this._points.push(new Vector2(x, y));
+        }
+        Path.prototype.addLineTo = function (x, y) {
+            this._points.push(new Vector2(x, y));
+            return this;
+        };
+
+        Path.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
+            if (typeof numberOfSegments === "undefined") { numberOfSegments = 36; }
+            var startPoint = this._points[this._points.length - 1];
+            var midPoint = new Vector2(midX, midY);
+            var endPoint = new Vector2(endX, endY);
+
+            var arc = new Arc(startPoint, midPoint, endPoint);
+
+            var increment = arc.angle.radians() / numberOfSegments;
+            if (arc.orientation === 0 /* CW */)
+                increment *= -1;
+            var currentAngle = arc.startAngle.radians() + increment;
+
+            for (var i = 0; i < numberOfSegments; i++) {
+                var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
+                var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
+                this.addLineTo(x, y);
+                currentAngle += increment;
+            }
+            return this;
+        };
+
+        Path.prototype.close = function () {
+            return this._points;
+        };
+
+        Path.StartingAt = function (x, y) {
+            return new Path(x, y);
+        };
+        return Path;
+    })();
+    BABYLON.Path = Path;
 })(BABYLON || (BABYLON = {}));
 //# sourceMappingURL=babylon.math.js.map

+ 110 - 1
Babylon/Math/babylon.math.ts

@@ -2515,7 +2515,6 @@
     };
 
     export class BezierCurve {
-
         public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
 
             // Extract X (which is equal to time here)
@@ -2542,4 +2541,114 @@
 
         }
     }
+
+    export enum Orientation {
+        CW = 0,
+        CCW = 1
+    }
+
+    export class Angle {
+        private _radians: number;
+
+        constructor(radians: number) {
+            this._radians = radians;
+            if (this._radians < 0) this._radians += (2 * Math.PI);
+        }
+
+        degrees = () => this._radians * 180 / Math.PI;
+        radians = () => this._radians;
+
+        static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
+            var delta = b.subtract(a);
+            var theta = Math.atan2(delta.y, delta.x);
+            return new Angle(theta);
+        }
+
+        static FromRadians(radians: number): Angle {
+            return new Angle(radians);
+        }
+
+        static FromDegrees(degrees: number): Angle {
+            return new Angle(degrees * Math.PI / 180);
+        }
+    }
+
+    export class Arc {
+        centerPoint: Vector2;
+        radius: number;
+        angle: Angle;
+        startAngle: Angle;
+        orientation: Orientation;
+
+        constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
+
+            var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
+            var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
+            var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
+            var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
+
+            this.centerPoint = new Vector2(
+                (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
+                ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
+                );
+
+            this.radius = this.centerPoint.subtract(this.startPoint).length();
+
+            this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
+
+            var a1 = this.startAngle.degrees();
+            var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
+            var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
+
+            // angles correction
+            if (a2 - a1 > +180.0) a2 -= 360.0;
+            if (a2 - a1 < -180.0) a2 += 360.0;
+            if (a3 - a2 > +180.0) a3 -= 360.0;
+            if (a3 - a2 < -180.0) a3 += 360.0;
+
+            this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
+            this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
+        }
+    }
+
+    export class Path {
+        private _points: Vector2[] = [];
+
+        constructor(x: number, y: number) {
+            this._points.push(new Vector2(x, y));
+        }
+
+        addLineTo(x: number, y: number): Path {
+            this._points.push(new Vector2(x, y));
+            return this;
+        }
+
+        addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path {
+            var startPoint = this._points[this._points.length - 1];
+            var midPoint = new Vector2(midX, midY);
+            var endPoint = new Vector2(endX, endY);
+
+            var arc = new Arc(startPoint, midPoint, endPoint);
+
+            var increment = arc.angle.radians() / numberOfSegments;
+            if (arc.orientation === Orientation.CW) increment *= -1;
+            var currentAngle = arc.startAngle.radians() + increment;
+
+            for (var i = 0; i < numberOfSegments; i++) {
+                var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
+                var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
+                this.addLineTo(x, y);
+                currentAngle += increment;
+            }
+            return this;
+        }
+
+        close(): Vector2[] {
+            return this._points;
+        }
+
+        static StartingAt(x: number, y: number): Path {
+            return new Path(x, y);
+        }
+    }
 }

+ 53 - 40
Babylon/Mesh/babylon.mesh.js

@@ -18,7 +18,17 @@ var BABYLON;
 
     var Mesh = (function (_super) {
         __extends(Mesh, _super);
-        function Mesh(name, scene) {
+        /**
+        * @param {string} name - The value used by scene.getMeshByName() to do a lookup.
+        * @param {BABYLON.Scene} scene - The scene to add this mesh to.
+        * @param {BABYLON.Node} parent - The parent of this mesh, if it has one
+        * @param {BABYLON.Mesh} source - An optional Mesh from which geometry is shared, cloned.
+        * @param {boolean} doNotCloneChildren - When cloning, skip cloning child meshes of source, default False.
+        *                  When false, achieved by calling a clone(), also passing False.
+        *                  This will make creation of children, recursive.
+        */
+        function Mesh(name, scene, parent, source, doNotCloneChildren) {
+            if (typeof parent === "undefined") { parent = null; }
             _super.call(this, name, scene);
             // Members
             this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
@@ -30,7 +40,48 @@ var BABYLON;
             this._renderIdForInstances = new Array();
             this._batchCache = new _InstancesBatch();
             this._instancesBufferSize = 32 * 16 * 4;
+
+            if (source) {
+                // Geometry
+                if (source._geometry) {
+                    source._geometry.applyToMesh(this);
+                }
+
+                // Deep copy
+                BABYLON.Tools.DeepCopy(source, this, ["name", "material", "skeleton"], []);
+
+                // Material
+                this.material = source.material;
+
+                if (!doNotCloneChildren) {
+                    for (var index = 0; index < scene.meshes.length; index++) {
+                        var mesh = scene.meshes[index];
+
+                        if (mesh.parent === source) {
+                            // doNotCloneChildren is always going to be False
+                            var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
+                        }
+                    }
+                }
+
+                for (index = 0; index < scene.particleSystems.length; index++) {
+                    var system = scene.particleSystems[index];
+
+                    if (system.emitter === source) {
+                        system.clone(system.name, this);
+                    }
+                }
+                this.computeWorldMatrix(true);
+            }
+
+            // Parent
+            if (parent !== null) {
+                this.parent = parent;
+            }
         }
+        Mesh.prototype._clone = function () {
+        };
+
         Object.defineProperty(Mesh.prototype, "hasLODLevels", {
             // Methods
             get: function () {
@@ -758,45 +809,7 @@ var BABYLON;
 
         // Clone
         Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
-            var result = new BABYLON.Mesh(name, this.getScene());
-
-            // Geometry
-            if (this._geometry) {
-                this._geometry.applyToMesh(result);
-            }
-
-            // Deep copy
-            BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], []);
-
-            // Material
-            result.material = this.material;
-
-            // Parent
-            if (newParent) {
-                result.parent = newParent;
-            }
-
-            if (!doNotCloneChildren) {
-                for (var index = 0; index < this.getScene().meshes.length; index++) {
-                    var mesh = this.getScene().meshes[index];
-
-                    if (mesh.parent == this) {
-                        mesh.clone(mesh.name, result);
-                    }
-                }
-            }
-
-            for (index = 0; index < this.getScene().particleSystems.length; index++) {
-                var system = this.getScene().particleSystems[index];
-
-                if (system.emitter == this) {
-                    system.clone(system.name, result);
-                }
-            }
-
-            result.computeWorldMatrix(true);
-
-            return result;
+            return new BABYLON.Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
         };
 
         // Dispose

+ 55 - 42
Babylon/Mesh/babylon.mesh.ts

@@ -28,8 +28,61 @@
         public _shouldGenerateFlatShading: boolean;
         private _preActivateId: number;
 
-        constructor(name: string, scene: Scene) {
+        /**
+         * @param {string} name - The value used by scene.getMeshByName() to do a lookup.
+         * @param {BABYLON.Scene} scene - The scene to add this mesh to.
+         * @param {BABYLON.Node} parent - The parent of this mesh, if it has one
+         * @param {BABYLON.Mesh} source - An optional Mesh from which geometry is shared, cloned.
+         * @param {boolean} doNotCloneChildren - When cloning, skip cloning child meshes of source, default False.
+         *                  When false, achieved by calling a clone(), also passing False.
+         *                  This will make creation of children, recursive.
+         */
+        constructor(name: string, scene: Scene, parent: Node = null, source?: Mesh, doNotCloneChildren?: boolean) {
             super(name, scene);
+            
+            if (source){
+                // Geometry
+                if (source._geometry) {
+                    source._geometry.applyToMesh(this);
+                }
+
+                // Deep copy
+                BABYLON.Tools.DeepCopy(source, this, ["name", "material", "skeleton"], []);
+
+                // Material
+                this.material = source.material;
+
+                if (!doNotCloneChildren) {
+                    // Children
+                    for (var index = 0; index < scene.meshes.length; index++) {
+                        var mesh = scene.meshes[index];
+
+                        if (mesh.parent === source) {
+                            // doNotCloneChildren is always going to be False
+                            var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren); 
+                        }
+                    }
+                }
+
+                // Particles
+                for (index = 0; index < scene.particleSystems.length; index++) {
+                    var system = scene.particleSystems[index];
+
+                    if (system.emitter === source) {
+                        system.clone(system.name, this);
+                    }
+                }
+                this.computeWorldMatrix(true);
+            }
+
+            // Parent
+            if (parent !== null) {
+                this.parent = parent;
+            }
+
+        }
+
+        private _clone() {
         }
 
         // Methods
@@ -759,47 +812,7 @@
 
         // Clone
         public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): Mesh {
-            var result = new BABYLON.Mesh(name, this.getScene());
-
-            // Geometry
-            if (this._geometry) {
-                this._geometry.applyToMesh(result);
-            }
-
-            // Deep copy
-            BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], []);
-
-            // Material
-            result.material = this.material;
-
-            // Parent
-            if (newParent) {
-                result.parent = newParent;
-            }
-
-            if (!doNotCloneChildren) {
-                // Children
-                for (var index = 0; index < this.getScene().meshes.length; index++) {
-                    var mesh = this.getScene().meshes[index];
-
-                    if (mesh.parent == this) {
-                        mesh.clone(mesh.name, result);
-                    }
-                }
-            }
-
-            // Particles
-            for (index = 0; index < this.getScene().particleSystems.length; index++) {
-                var system = this.getScene().particleSystems[index];
-
-                if (system.emitter == this) {
-                    system.clone(system.name, result);
-                }
-            }
-
-            result.computeWorldMatrix(true);
-
-            return result;
+            return new BABYLON.Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
         }
 
         // Dispose

+ 35 - 112
Babylon/Mesh/babylon.polygonmesh.js

@@ -1,4 +1,4 @@
-var __extends = this.__extends || function (d, b) {
+var __extends = this.__extends || function (d, b) {
     for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
     function __() { this.constructor = d; }
     __.prototype = b.prototype;
@@ -14,13 +14,7 @@ var BABYLON;
         }
         return IndexedVector2;
     })(BABYLON.Vector2);
-    function nearlyEqual(a, b, epsilon) {
-        if (epsilon === void 0) { epsilon = 0.0001; }
-        if (a === b) {
-            return true;
-        }
-        return Math.abs(a - b) < epsilon;
-    }
+
     var PolygonPoints = (function () {
         function PolygonPoints() {
             this.elements = new Array();
@@ -29,33 +23,36 @@ var BABYLON;
             var _this = this;
             var result = new Array();
             originalPoints.forEach(function (point) {
-                if (result.length == 0 || !(nearlyEqual(point.x, result[0].x) && nearlyEqual(point.y, result[0].y))) {
+                if (result.length == 0 || !(BABYLON.Tools.WithinEpsilon(point.x, result[0].x) && BABYLON.Tools.WithinEpsilon(point.y, result[0].y))) {
                     var newPoint = new IndexedVector2(point, _this.elements.length);
                     result.push(newPoint);
                     _this.elements.push(newPoint);
                 }
             });
+
             return result;
         };
+
         PolygonPoints.prototype.computeBounds = function () {
             var lmin = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
             var lmax = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
+
             this.elements.forEach(function (point) {
                 // x
                 if (point.x < lmin.x) {
                     lmin.x = point.x;
-                }
-                else if (point.x > lmax.x) {
+                } else if (point.x > lmax.x) {
                     lmax.x = point.x;
                 }
+
                 // y
                 if (point.y < lmin.y) {
                     lmin.y = point.y;
-                }
-                else if (point.y > lmax.y) {
+                } else if (point.y > lmax.y) {
                     lmax.y = point.y;
                 }
             });
+
             return {
                 min: lmin,
                 max: lmax,
@@ -65,6 +62,7 @@ var BABYLON;
         };
         return PolygonPoints;
     })();
+
     var Polygon = (function () {
         function Polygon() {
         }
@@ -76,125 +74,42 @@ var BABYLON;
                 new BABYLON.Vector2(xmin, ymax)
             ];
         };
+
         Polygon.Circle = function (radius, cx, cy, numberOfSides) {
-            if (cx === void 0) { cx = 0; }
-            if (cy === void 0) { cy = 0; }
-            if (numberOfSides === void 0) { numberOfSides = 32; }
+            if (typeof cx === "undefined") { cx = 0; }
+            if (typeof cy === "undefined") { cy = 0; }
+            if (typeof numberOfSides === "undefined") { numberOfSides = 32; }
             var result = new Array();
+
             var angle = 0;
             var increment = (Math.PI * 2) / numberOfSides;
+
             for (var i = 0; i < numberOfSides; i++) {
                 result.push(new BABYLON.Vector2(cx + Math.cos(angle) * radius, cy + Math.sin(angle) * radius));
                 angle -= increment;
             }
+
             return result;
         };
+
         Polygon.Parse = function (input) {
-            var floats = input.split(/[^-+eE\.\d]+/).map(parseFloat).filter(function (val) { return (!isNaN(val)); });
+            var floats = input.split(/[^-+eE\.\d]+/).map(parseFloat).filter(function (val) {
+                return (!isNaN(val));
+            });
             var i, result = [];
             for (i = 0; i < (floats.length & 0x7FFFFFFE); i += 2) {
                 result.push(new poly2tri.Point(floats[i], floats[i + 1]));
             }
             return result;
         };
+
         Polygon.StartingAt = function (x, y) {
-            return Path.StartingAt(x, y);
+            return BABYLON.Path.StartingAt(x, y);
         };
         return Polygon;
     })();
     BABYLON.Polygon = Polygon;
-    var Arc = (function () {
-        function Arc(startPoint, midPoint, endPoint) {
-            this.startPoint = startPoint;
-            this.midPoint = midPoint;
-            this.endPoint = endPoint;
-            var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
-            var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
-            var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
-            var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
-            this.centerPoint = new BABYLON.Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
-            this.radius = this.centerPoint.subtract(this.startPoint).length();
-            this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
-            var a1 = this.startAngle.degrees();
-            var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
-            var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
-            // angles correction
-            if (a2 - a1 > +180.0)
-                a2 -= 360.0;
-            if (a2 - a1 < -180.0)
-                a2 += 360.0;
-            if (a3 - a2 > +180.0)
-                a3 -= 360.0;
-            if (a3 - a2 < -180.0)
-                a3 += 360.0;
-            this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
-            this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
-        }
-        return Arc;
-    })();
-    var Orientation;
-    (function (Orientation) {
-        Orientation[Orientation["CW"] = 0] = "CW";
-        Orientation[Orientation["CCW"] = 1] = "CCW";
-    })(Orientation || (Orientation = {}));
-    var Angle = (function () {
-        function Angle(radians) {
-            var _this = this;
-            this.degrees = function () { return _this._radians * 180 / Math.PI; };
-            this.radians = function () { return _this._radians; };
-            this._radians = radians;
-            if (this._radians < 0)
-                this._radians += (2 * Math.PI);
-        }
-        Angle.BetweenTwoPoints = function (a, b) {
-            var delta = b.subtract(a);
-            var theta = Math.atan2(delta.y, delta.x);
-            return new Angle(theta);
-        };
-        Angle.FromRadians = function (radians) {
-            return new Angle(radians);
-        };
-        Angle.FromDegrees = function (degrees) {
-            return new Angle(degrees * Math.PI / 180);
-        };
-        return Angle;
-    })();
-    var Path = (function () {
-        function Path(x, y) {
-            this._points = [];
-            this._points.push(new BABYLON.Vector2(x, y));
-        }
-        Path.prototype.addLineTo = function (x, y) {
-            this._points.push(new BABYLON.Vector2(x, y));
-            return this;
-        };
-        Path.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
-            if (numberOfSegments === void 0) { numberOfSegments = 36; }
-            var startPoint = this._points[this._points.length - 1];
-            var midPoint = new BABYLON.Vector2(midX, midY);
-            var endPoint = new BABYLON.Vector2(endX, endY);
-            var arc = new Arc(startPoint, midPoint, endPoint);
-            var increment = arc.angle.radians() / numberOfSegments;
-            if (arc.orientation === 0 /* CW */)
-                increment *= -1;
-            var currentAngle = arc.startAngle.radians() + increment;
-            for (var i = 0; i < numberOfSegments; i++) {
-                var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
-                var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
-                this.addLineTo(x, y);
-                currentAngle += increment;
-            }
-            return this;
-        };
-        Path.prototype.close = function () {
-            return this._points;
-        };
-        Path.StartingAt = function (x, y) {
-            return new Path(x, y);
-        };
-        return Path;
-    })();
-    BABYLON.Path = Path;
+
     var PolygonMeshBuilder = (function () {
         function PolygonMeshBuilder(name, contours, scene) {
             this.name = name;
@@ -203,39 +118,47 @@ var BABYLON;
             if (!("poly2tri" in window)) {
                 throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
             }
+
             this._swctx = new poly2tri.SweepContext(this._points.add(contours));
         }
         PolygonMeshBuilder.prototype.addHole = function (hole) {
             this._swctx.addHole(this._points.add(hole));
             return this;
         };
+
         PolygonMeshBuilder.prototype.build = function (updatable) {
-            if (updatable === void 0) { updatable = false; }
+            if (typeof updatable === "undefined") { updatable = false; }
             var result = new BABYLON.Mesh(this.name, this.scene);
+
             var normals = [];
             var positions = [];
             var uvs = [];
+
             var bounds = this._points.computeBounds();
             this._points.elements.forEach(function (p) {
                 normals.push(0, 1.0, 0);
                 positions.push(p.x, 0, p.y);
                 uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
             });
+
             var indices = [];
+
             this._swctx.triangulate();
             this._swctx.getTriangles().forEach(function (triangle) {
                 triangle.getPoints().forEach(function (point) {
                     indices.push(point.index);
                 });
             });
+
             result.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
             result.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
             result.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
             result.setIndices(indices);
+
             return result;
         };
         return PolygonMeshBuilder;
     })();
     BABYLON.PolygonMeshBuilder = PolygonMeshBuilder;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.polygonmesh.js.map
+//# sourceMappingURL=babylon.polygonMesh.js.map

+ 2 - 122
Babylon/Mesh/babylon.polygonmesh.ts

@@ -5,13 +5,6 @@
         }
     }
 
-    function nearlyEqual(a: number, b: number, epsilon: number = 0.0001): boolean {
-        if (a === b) {
-            return true;
-        }
-        return Math.abs(a - b) < epsilon;
-    }
-
     class PolygonPoints {
         elements = new Array<IndexedVector2>();
 
@@ -19,7 +12,7 @@
 
             var result = new Array<IndexedVector2>();
             originalPoints.forEach(point => {
-                if (result.length == 0 || !(nearlyEqual(point.x, result[0].x) && nearlyEqual(point.y, result[0].y))) {
+                if (result.length == 0 || !(Tools.WithinEpsilon(point.x, result[0].x) && Tools.WithinEpsilon(point.y, result[0].y))) {
                     var newPoint = new IndexedVector2(point, this.elements.length);
                     result.push(newPoint);
                     this.elements.push(newPoint);
@@ -101,120 +94,7 @@
         static StartingAt(x: number, y: number): Path {
             return Path.StartingAt(x, y);
         }
-    }
-
-    class Arc {
-        centerPoint: Vector2;
-        radius: number;
-        angle: Angle;
-        startAngle: Angle;
-        orientation : Orientation;
-
-        constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
-            
-            var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
-            var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
-            var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
-            var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
-
-            this.centerPoint = new Vector2(
-                (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
-                ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
-                );
-            
-            this.radius = this.centerPoint.subtract(this.startPoint).length();
-
-            this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
-            
-            var a1 = this.startAngle.degrees();
-            var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
-            var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
-            
-            // angles correction
-            if (a2 - a1 > +180.0) a2 -= 360.0;
-            if (a2 - a1 < -180.0) a2 += 360.0;
-            if (a3 - a2 > +180.0) a3 -= 360.0;
-            if (a3 - a2 < -180.0) a3 += 360.0;
-
-            this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
-            this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
-        }
-    }
-    
-    enum Orientation {
-        CW,
-        CCW
-    }
-
-    class Angle {
-
-        private _radians: number;
-
-        constructor(radians: number) {
-            this._radians = radians;
-            if (this._radians < 0) this._radians += (2 * Math.PI);
-        }
-
-        degrees = () => this._radians * 180 / Math.PI;
-        radians = () => this._radians;
-
-        static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
-            var delta = b.subtract(a);
-            var theta = Math.atan2(delta.y, delta.x);
-            return new Angle(theta);
-        }
-
-        static FromRadians(radians: number): Angle {
-            return new Angle(radians);
-        }
-
-        static FromDegrees(degrees: number): Angle {
-            return new Angle(degrees * Math.PI / 180);
-        }
-    }
-
-    
-    
-    export class Path {
-        private _points : Vector2[] = [];
-
-        constructor(x: number, y: number) {
-            this._points.push(new Vector2(x, y));
-        }
-        
-        addLineTo(x: number, y: number): Path {
-            this._points.push(new Vector2(x, y));
-            return this;
-        }
-
-        addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36) : Path {
-            var startPoint = this._points[this._points.length - 1];
-            var midPoint = new Vector2(midX, midY);
-            var endPoint = new Vector2(endX, endY);
-            
-            var arc = new Arc(startPoint, midPoint, endPoint);
-
-            var increment = arc.angle.radians() / numberOfSegments;
-            if (arc.orientation === Orientation.CW) increment *= -1;
-            var currentAngle = arc.startAngle.radians() + increment;
-
-            for (var i = 0; i < numberOfSegments; i++) {
-                var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
-                var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
-                this.addLineTo(x, y);
-                currentAngle += increment;
-            }
-            return this;
-        }
-
-        close() : Vector2[] {
-            return this._points;
-        }
-
-        static StartingAt(x: number, y: number): Path {
-            return new Path(x, y);
-        }
-    }
+    }     
 
     export class PolygonMeshBuilder {
 

+ 179 - 45
babylon.2.0-alpha.debug.js

@@ -2553,6 +2553,122 @@ var __extends = this.__extends || function (d, b) {
         return BezierCurve;
     })();
     BABYLON.BezierCurve = BezierCurve;
+
+    (function (Orientation) {
+        Orientation[Orientation["CW"] = 0] = "CW";
+        Orientation[Orientation["CCW"] = 1] = "CCW";
+    })(BABYLON.Orientation || (BABYLON.Orientation = {}));
+    var Orientation = BABYLON.Orientation;
+
+    var Angle = (function () {
+        function Angle(radians) {
+            var _this = this;
+            this.degrees = function () {
+                return _this._radians * 180 / Math.PI;
+            };
+            this.radians = function () {
+                return _this._radians;
+            };
+            this._radians = radians;
+            if (this._radians < 0)
+                this._radians += (2 * Math.PI);
+        }
+        Angle.BetweenTwoPoints = function (a, b) {
+            var delta = b.subtract(a);
+            var theta = Math.atan2(delta.y, delta.x);
+            return new Angle(theta);
+        };
+
+        Angle.FromRadians = function (radians) {
+            return new Angle(radians);
+        };
+
+        Angle.FromDegrees = function (degrees) {
+            return new Angle(degrees * Math.PI / 180);
+        };
+        return Angle;
+    })();
+    BABYLON.Angle = Angle;
+
+    var Arc = (function () {
+        function Arc(startPoint, midPoint, endPoint) {
+            this.startPoint = startPoint;
+            this.midPoint = midPoint;
+            this.endPoint = endPoint;
+            var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
+            var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
+            var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
+            var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
+
+            this.centerPoint = new Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
+
+            this.radius = this.centerPoint.subtract(this.startPoint).length();
+
+            this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
+
+            var a1 = this.startAngle.degrees();
+            var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
+            var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
+
+            // angles correction
+            if (a2 - a1 > +180.0)
+                a2 -= 360.0;
+            if (a2 - a1 < -180.0)
+                a2 += 360.0;
+            if (a3 - a2 > +180.0)
+                a3 -= 360.0;
+            if (a3 - a2 < -180.0)
+                a3 += 360.0;
+
+            this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
+            this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
+        }
+        return Arc;
+    })();
+    BABYLON.Arc = Arc;
+
+    var Path = (function () {
+        function Path(x, y) {
+            this._points = [];
+            this._points.push(new Vector2(x, y));
+        }
+        Path.prototype.addLineTo = function (x, y) {
+            this._points.push(new Vector2(x, y));
+            return this;
+        };
+
+        Path.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
+            if (typeof numberOfSegments === "undefined") { numberOfSegments = 36; }
+            var startPoint = this._points[this._points.length - 1];
+            var midPoint = new Vector2(midX, midY);
+            var endPoint = new Vector2(endX, endY);
+
+            var arc = new Arc(startPoint, midPoint, endPoint);
+
+            var increment = arc.angle.radians() / numberOfSegments;
+            if (arc.orientation === 0 /* CW */)
+                increment *= -1;
+            var currentAngle = arc.startAngle.radians() + increment;
+
+            for (var i = 0; i < numberOfSegments; i++) {
+                var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
+                var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
+                this.addLineTo(x, y);
+                currentAngle += increment;
+            }
+            return this;
+        };
+
+        Path.prototype.close = function () {
+            return this._points;
+        };
+
+        Path.StartingAt = function (x, y) {
+            return new Path(x, y);
+        };
+        return Path;
+    })();
+    BABYLON.Path = Path;
 })(BABYLON || (BABYLON = {}));
 //# sourceMappingURL=babylon.math.js.map
 var BABYLON;
@@ -11049,7 +11165,17 @@ var BABYLON;
 
     var Mesh = (function (_super) {
         __extends(Mesh, _super);
-        function Mesh(name, scene) {
+        /**
+        * @param {string} name - The value used by scene.getMeshByName() to do a lookup.
+        * @param {BABYLON.Scene} scene - The scene to add this mesh to.
+        * @param {BABYLON.Node} parent - The parent of this mesh, if it has one
+        * @param {BABYLON.Mesh} source - An optional Mesh from which geometry is shared, cloned.
+        * @param {boolean} doNotCloneChildren - When cloning, skip cloning child meshes of source, default False.
+        *                  When false, achieved by calling a clone(), also passing False.
+        *                  This will make creation of children, recursive.
+        */
+        function Mesh(name, scene, parent, source, doNotCloneChildren) {
+            if (typeof parent === "undefined") { parent = null; }
             _super.call(this, name, scene);
             // Members
             this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
@@ -11061,7 +11187,48 @@ var BABYLON;
             this._renderIdForInstances = new Array();
             this._batchCache = new _InstancesBatch();
             this._instancesBufferSize = 32 * 16 * 4;
+
+            if (source) {
+                // Geometry
+                if (source._geometry) {
+                    source._geometry.applyToMesh(this);
+                }
+
+                // Deep copy
+                BABYLON.Tools.DeepCopy(source, this, ["name", "material", "skeleton"], []);
+
+                // Material
+                this.material = source.material;
+
+                if (!doNotCloneChildren) {
+                    for (var index = 0; index < scene.meshes.length; index++) {
+                        var mesh = scene.meshes[index];
+
+                        if (mesh.parent === source) {
+                            // doNotCloneChildren is always going to be False
+                            var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
+                        }
+                    }
+                }
+
+                for (index = 0; index < scene.particleSystems.length; index++) {
+                    var system = scene.particleSystems[index];
+
+                    if (system.emitter === source) {
+                        system.clone(system.name, this);
+                    }
+                }
+                this.computeWorldMatrix(true);
+            }
+
+            // Parent
+            if (parent !== null) {
+                this.parent = parent;
+            }
         }
+        Mesh.prototype._clone = function () {
+        };
+
         Object.defineProperty(Mesh.prototype, "hasLODLevels", {
             // Methods
             get: function () {
@@ -11789,45 +11956,7 @@ var BABYLON;
 
         // Clone
         Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
-            var result = new BABYLON.Mesh(name, this.getScene());
-
-            // Geometry
-            if (this._geometry) {
-                this._geometry.applyToMesh(result);
-            }
-
-            // Deep copy
-            BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], []);
-
-            // Material
-            result.material = this.material;
-
-            // Parent
-            if (newParent) {
-                result.parent = newParent;
-            }
-
-            if (!doNotCloneChildren) {
-                for (var index = 0; index < this.getScene().meshes.length; index++) {
-                    var mesh = this.getScene().meshes[index];
-
-                    if (mesh.parent == this) {
-                        mesh.clone(mesh.name, result);
-                    }
-                }
-            }
-
-            for (index = 0; index < this.getScene().particleSystems.length; index++) {
-                var system = this.getScene().particleSystems[index];
-
-                if (system.emitter == this) {
-                    system.clone(system.name, result);
-                }
-            }
-
-            result.computeWorldMatrix(true);
-
-            return result;
+            return new BABYLON.Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
         };
 
         // Dispose
@@ -30194,11 +30323,12 @@ var BABYLON;
         PolygonPoints.prototype.add = function (originalPoints) {
             var _this = this;
             var result = new Array();
-
             originalPoints.forEach(function (point) {
-                var newPoint = new IndexedVector2(point, _this.elements.length);
-                result.push(newPoint);
-                _this.elements.push(newPoint);
+                if (result.length == 0 || !(BABYLON.Tools.WithinEpsilon(point.x, result[0].x) && BABYLON.Tools.WithinEpsilon(point.y, result[0].y))) {
+                    var newPoint = new IndexedVector2(point, _this.elements.length);
+                    result.push(newPoint);
+                    _this.elements.push(newPoint);
+                }
             });
 
             return result;
@@ -30273,6 +30403,10 @@ var BABYLON;
             }
             return result;
         };
+
+        Polygon.StartingAt = function (x, y) {
+            return BABYLON.Path.StartingAt(x, y);
+        };
         return Polygon;
     })();
     BABYLON.Polygon = Polygon;
@@ -30328,4 +30462,4 @@ var BABYLON;
     })();
     BABYLON.PolygonMeshBuilder = PolygonMeshBuilder;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.polygonmesh.js.map
+//# sourceMappingURL=babylon.polygonMesh.js.map

Diferenças do arquivo suprimidas por serem muito extensas
+ 10 - 10
babylon.2.0-alpha.js