瀏覽代碼

Move slice in its own class

Popov72 4 年之前
父節點
當前提交
f87e92c3af
共有 3 個文件被更改,包括 41 次插入21 次删除
  1. 3 11
      src/Meshes/buffer.ts
  2. 35 0
      src/Misc/sliceTools.ts
  3. 3 10
      src/Misc/tools.ts

+ 3 - 11
src/Meshes/buffer.ts

@@ -1,6 +1,7 @@
 import { Nullable, DataArray, FloatArray } from "../types";
 import { ThinEngine } from "../Engines/thinEngine";
 import { DataBuffer } from "./dataBuffer";
+import { SliceTools } from "../Misc/sliceTools";
 
 /**
  * Class used to store data that will be store in GPU memory
@@ -211,15 +212,6 @@ export class VertexBuffer {
     private _instanced: boolean;
     private _instanceDivisor: number;
 
-    // Copied from Tools. We don't import Tools to avoid raising the size of the package too much
-    private static _Slice<T>(data: T, start?: number, end?: number): T {
-        if ((data as any).slice) {
-            return (data as any).slice(start, end);
-        }
-
-        return Array.prototype.slice.call(data, start, end);
-    }
-
     /**
      * The byte type.
      */
@@ -424,7 +416,7 @@ export class VertexBuffer {
         if (!(data instanceof Array || data instanceof Float32Array) || vertexBuffer.byteOffset !== 0 || data.length !== count) {
             if (data instanceof Array) {
                 const offset = vertexBuffer.byteOffset / 4;
-                return VertexBuffer._Slice(data, offset, offset + count);
+                return SliceTools.Slice(data, offset, offset + count);
             } else if (data instanceof ArrayBuffer) {
                 return new Float32Array(data, vertexBuffer.byteOffset, count);
             } else {
@@ -450,7 +442,7 @@ export class VertexBuffer {
         }
 
         if (forceCopy) {
-            return VertexBuffer._Slice(data);
+            return SliceTools.Slice(data);
         }
 
         return data;

+ 35 - 0
src/Misc/sliceTools.ts

@@ -0,0 +1,35 @@
+/**
+ * Class used to provide helpers for slicing
+ */
+export class SliceTools {
+    /**
+     * Provides a slice function that will work even on IE
+     * @param data defines the array to slice
+     * @param start defines the start of the data (optional)
+     * @param end defines the end of the data (optional)
+     * @returns the new sliced array
+     */
+    public static Slice<T>(data: T, start?: number, end?: number): T {
+        if ((data as any).slice) {
+            return (data as any).slice(start, end);
+        }
+
+        return Array.prototype.slice.call(data, start, end);
+    }
+
+    /**
+     * Provides a slice function that will work even on IE
+     * The difference between this and Slice is that this will force-convert to array
+     * @param data defines the array to slice
+     * @param start defines the start of the data (optional)
+     * @param end defines the end of the data (optional)
+     * @returns the new sliced array
+     */
+    public static SliceToArray<T, P>(data: T, start?: number, end?: number): Array<P> {
+        if (Array.isArray(data)) {
+            return (data as Array<P>).slice(start, end);
+        }
+
+        return Array.prototype.slice.call(data, start, end);
+    }
+}

+ 3 - 10
src/Misc/tools.ts

@@ -16,6 +16,7 @@ import { TimingTools } from "./timingTools";
 import { InstantiationTools } from "./instantiationTools";
 import { GUID } from "./guid";
 import { IScreenshotSize } from "./interfaces/screenshotSize";
+import { SliceTools } from "./sliceTools";
 
 declare type Camera = import("../Cameras/camera").Camera;
 declare type Engine = import("../Engines/engine").Engine;
@@ -163,11 +164,7 @@ export class Tools {
      * @returns the new sliced array
      */
     public static Slice<T>(data: T, start?: number, end?: number): T {
-        if ((data as any).slice) {
-            return (data as any).slice(start, end);
-        }
-
-        return Array.prototype.slice.call(data, start, end);
+        return SliceTools.Slice(data, start, end);
     }
 
     /**
@@ -179,11 +176,7 @@ export class Tools {
      * @returns the new sliced array
      */
     public static SliceToArray<T, P>(data: T, start?: number, end?: number): Array<P> {
-        if (Array.isArray(data)) {
-            return (data as Array<P>).slice(start, end);
-        }
-
-        return Array.prototype.slice.call(data, start, end);
+        return SliceTools.SliceToArray(data, start, end);
     }
 
     /**