Explorar el Código

Merge pull request #535 from jbousquie/feature.DashedLines

Feature.dashed lines
David Catuhe hace 10 años
padre
commit
d1bd14bf3c
Se han modificado 2 ficheros con 103 adiciones y 0 borrados
  1. 60 0
      Babylon/Mesh/babylon.mesh.ts
  2. 43 0
      Babylon/Mesh/babylon.mesh.vertexData.ts

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

@@ -1345,6 +1345,66 @@
             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
+                var positionsOfLines = function(points) {
+                    var positionFunction = function(positions) {
+                        var curvect = Vector3.Zero();
+                        var nbSeg = positions.length / 6;
+                        var lg = 0;
+                        var nb = 0;
+                        var shft = 0;
+                        var dashshft = 0;
+                        var curshft = 0;
+                        var p = 0;
+                        var i = 0;
+                        var j = 0;
+                        for (i = 0; i < points.length - 1; i++) {
+                            points[i + 1].subtractToRef(points[i], curvect);
+                            lg += curvect.length();
+                        }
+                        shft = lg / nbSeg;
+                        dashshft = (<any>linesInstance).dashSize * shft / ((<any>linesInstance).dashSize + (<any>linesInstance).gapSize);
+                        for (i = 0; i < points.length - 1; i++) {
+                            points[i + 1].subtractToRef(points[i], curvect);
+                            curvect.normalize();
+                            nb = Math.floor(curvect.length() / shft);
+                            j = 0;
+                            while (j < nb && p < positions.length) {
+                                curshft = shft * j;
+                                positions[p] = points[i].x + curshft * curvect.x;
+                                positions[p + 1] = points[i].y + curshft * curvect.y;
+                                positions[p + 2] = points[i].z + curshft * curvect.z;
+                                positions[p + 3] = points[i].x + (curshft + dashshft)* curvect.x;
+                                positions[p + 4] = points[i].y + (curshft + dashshft) * curvect.y;
+                                positions[p + 5] = points[i].z + (curshft + dashshft) * curvect.z;
+                                p += 6;
+                                j++;
+                            }
+                        }
+                        while (p < positions.length) {
+                            positions[p] = points[i].x;
+                            positions[p + 1] = points[i].y;
+                            positions[p + 2] = points[i].z;
+                            p += 3;
+                        }
+                    };
+                    return positionFunction;   
+                };
+                var positionFunction = positionsOfLines(points);
+                linesInstance.updateMeshPositions(positionFunction, false);
+                return linesInstance;
+            }
+            // dashed lines creation
+            var dashedLines = new LinesMesh(name, scene, updatable);
+            var vertexData = VertexData.CreateDashedLines(points, dashSize, gapSize, dashNb);
+            vertexData.applyToMesh(dashedLines, updatable);
+            (<any>dashedLines).dashSize = dashSize;
+            (<any>dashedLines).gapSize = gapSize;
+            return dashedLines;
+        }
+
         // Extrusion
         public static ExtrudeShape(name: string, shape: Vector3[], path: Vector3[], scale: number, rotation: number, cap: number, scene: Scene, updatable?: boolean, sideOrientation: number = Mesh.DEFAULTSIDE, extrudedInstance: Mesh = null): Mesh {
             scale = scale || 1;

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

@@ -793,6 +793,49 @@
             return vertexData;
         }
 
+        public static CreateDashedLines(points: Vector3[], dashSize: number, gapSize: number, dashNb: number): VertexData {
+            dashSize = dashSize || 3;
+            gapSize = gapSize || 1;
+            dashNb = dashNb || 200;
+
+            var positions = [];
+            var indices = [];
+
+            var curvect = Vector3.Zero();
+            var lg = 0;
+            var nb = 0;
+            var shft = 0;
+            var dashshft = 0;
+            var curshft = 0;
+            var idx = 0;
+            var i = 0;
+            for (i = 0; i < points.length - 1; i++) {
+                points[i + 1].subtractToRef(points[i], curvect);
+                lg += curvect.length();
+            }
+            shft = lg / dashNb;
+            dashshft = dashSize * shft / (dashSize + gapSize);
+            for (i = 0; i < points.length - 1; i++) {
+                points[i + 1].subtractToRef(points[i], curvect);
+                curvect.normalize();
+                nb = Math.floor(curvect.length() / shft);
+                for (var j = 0; j < nb; j++) {
+                    curshft = shft * j;
+                    positions.push(points[i].x + curshft * curvect.x , points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
+                    positions.push(points[i].x + (curshft + dashshft)* curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
+                    indices.push(idx, idx + 1);
+                    idx += 2;
+                }
+            }
+
+            // Result
+            var vertexData = new VertexData();
+            vertexData.positions = positions;
+            vertexData.indices = indices;
+
+            return vertexData;
+        }
+
         public static CreateGround(width: number, height: number, subdivisions: number): VertexData {
             var indices = [];
             var positions = [];