| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4609a4b commit 7ac6d83
78 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,149 @@ | |||
| 1 | + import { Matrix4 } from "./Math/Matrix4"; | ||
| 2 | + import { Vector2 } from "./Math/Vector2"; | ||
| 3 | + import { Vector3 } from "./Math/Vector3"; | ||
| 4 | + | ||
| 5 | + class ArrayBuffer { | ||
| 6 | + constructor(itemSize) { | ||
| 7 | + this._array = []; | ||
| 8 | + this._itemSize = itemSize; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + get array() { | ||
| 12 | + return this._array; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + get itemSize() { | ||
| 16 | + return this._itemSize; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + get count() { | ||
| 20 | + return this._array.length / this._itemSize; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + push(...args) { | ||
| 24 | + for (let i = 0, end = args.length; i < end; i += 1) { | ||
| 25 | + this._array.push(args[i]); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + return this; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + get(index, itemIndex) { | ||
| 32 | + return this._array[index * this._itemSize + itemIndex]; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + set(index, itemIndex, value) { | ||
| 36 | + this._array[index * this._itemSize + itemIndex] = value; | ||
| 37 | + return this; | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + let _vec2 = new Vector2(); | ||
| 42 | + let _matrix$ = new Matrix4(); | ||
| 43 | + class ArrayBufferVector2 extends ArrayBuffer { | ||
| 44 | + constructor(itemSize = 2) { | ||
| 45 | + super(itemSize); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + getX(index) { | ||
| 49 | + return this.get(index, 0); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + getY(index) { | ||
| 53 | + return this.get(index, 1); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + setX(index, value) { | ||
| 57 | + return this.set(index, 0, value); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + setY(index, value) { | ||
| 61 | + return this.set(index, 0, value); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + setXY(index, x, y) { | ||
| 65 | + index *= this.itemSize; | ||
| 66 | + this._array[index + 0] = x; | ||
| 67 | + this._array[index + 1] = y; | ||
| 68 | + return this; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + pushVector2(vec2) { | ||
| 72 | + super.push(vec2.x, vec2.y); | ||
| 73 | + return this; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + getVector2(index, vec2) { | ||
| 77 | + index *= this.itemSize; | ||
| 78 | + vec2.x = this._array[index + 0]; | ||
| 79 | + vec2.y = this._array[index + 1]; | ||
| 80 | + return vec2; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + applyMatrix3(matrix) { | ||
| 84 | + const count = this.count; | ||
| 85 | + for (let i = 0; i < count; i += 1) { | ||
| 86 | + _vec2 = this.getVector2(i, _vec2); | ||
| 87 | + _vec2.applyMatrix3(matrix); | ||
| 88 | + this.setXY(i, _vec2.x, _vec2.y); | ||
| 89 | + } | ||
| 90 | + return this; | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + let _vec3 = new Vector3(); | ||
| 95 | + class ArrayBufferVector3 extends ArrayBufferVector2 { | ||
| 96 | + constructor(itemSize = 3) { | ||
| 97 | + super(itemSize); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + getZ(index) { | ||
| 101 | + return this.get(index, 2); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + setZ(index, value) { | ||
| 105 | + return this.set(index, 2, value); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + setXYZ(index, x, y, z) { | ||
| 109 | + index *= this.itemSize; | ||
| 110 | + this._array[index + 0] = x; | ||
| 111 | + this._array[index + 1] = y; | ||
| 112 | + this._array[index + 2] = z; | ||
| 113 | + return this; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + getVector3(index, vec3) { | ||
| 117 | + index *= this.itemSize; | ||
| 118 | + vec3.x = this._array[index + 0]; | ||
| 119 | + vec3.y = this._array[index + 1]; | ||
| 120 | + vec3.z = this._array[index + 2]; | ||
| 121 | + return vec3; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + pushVector3(vec3) { | ||
| 125 | + super.push(vec3.x, vec3.y, vec3.z); | ||
| 126 | + return this; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + applyMatrix4(matrix) { | ||
| 130 | + const count = this.count; | ||
| 131 | + for (let i = 0; i < count; i += 1) { | ||
| 132 | + _vec3 = this.getVector3(i, _vec3); | ||
| 133 | + _vec3.applyMatrix4(matrix); | ||
| 134 | + this.setXYZ(i, _vec3.x, _vec3.y, _vec3.z); | ||
| 135 | + } | ||
| 136 | + return this; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + translate( x, y, z ) { | ||
| 140 | + | ||
| 141 | + _matrix$.makeTranslation( x, y, z ); | ||
| 142 | + | ||
| 143 | + this.applyMatrix4( _matrix$ ); | ||
| 144 | + | ||
| 145 | + return this; | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + export { ArrayBuffer, ArrayBufferVector2, ArrayBufferVector3 }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,154 @@ | |||
| 1 | + | ||
| 2 | + /** | ||
| 3 | + * 判断value是否未定义或者为null | ||
| 4 | + * @function | ||
| 5 | + * | ||
| 6 | + * @param {*} value | ||
| 7 | + * | ||
| 8 | + * @returns {Boolean} 如果value未定义或者为null返回false,否则返回true | ||
| 9 | + */ | ||
| 10 | + export function defined(value) { | ||
| 11 | + return value !== undefined && value !== null; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + /** | ||
| 15 | + * 为参数设置默认值 | ||
| 16 | + * @function | ||
| 17 | + * @param {*} a | ||
| 18 | + * @param {*} b | ||
| 19 | + * @returns {*} 如果第一个参数未定义或为null返回b,否则返回a | ||
| 20 | + * | ||
| 21 | + * @example | ||
| 22 | + * param=defaultValue(param,"default") | ||
| 23 | + */ | ||
| 24 | + export function defaultValue(a, b) { | ||
| 25 | + if (a !== undefined && a !== null) { | ||
| 26 | + return a; | ||
| 27 | + } | ||
| 28 | + return b; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * 判断func是否是函数 | ||
| 33 | + * @function | ||
| 34 | + * @param {*} func | ||
| 35 | + * @returns {Boolean} 如果func是函数类型返回true,否则返回false | ||
| 36 | + */ | ||
| 37 | + export function isFunc(func) { | ||
| 38 | + return typeof func === "function"; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * 判断arr是否是数组类型 | ||
| 43 | + * @function | ||
| 44 | + * @param {*} arr | ||
| 45 | + * @returns {Boolean} 如果arr是数组类型返回true,否则返回false | ||
| 46 | + */ | ||
| 47 | + export function isArray(arr) { | ||
| 48 | + if (isFunc(Array.isArray)) | ||
| 49 | + return Array.isArray(arr); | ||
| 50 | + else { | ||
| 51 | + return Object.prototype.toString.call(arr) === "[object Array]"; | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + export class DeveloperError extends Error { | ||
| 56 | + constructor(message) { | ||
| 57 | + let stack = null; | ||
| 58 | + try { | ||
| 59 | + throw new Error(); | ||
| 60 | + } catch (e) { | ||
| 61 | + stack = e.stack; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + super("DeveloperError", message, stack); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + toString() { | ||
| 68 | + const str = this.name + ": " + this.message; | ||
| 69 | + | ||
| 70 | + if (defined(this.stack)) { | ||
| 71 | + str += "\n" + this.stack.toString(); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + return str; | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + function getUndefinedErrorMessage(name) { | ||
| 79 | + return name + " is required, actual value was undefined"; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + function getFailedTypeErrorMessage(actual, expected, name) { | ||
| 83 | + return ( | ||
| 84 | + "Expected " + | ||
| 85 | + name + | ||
| 86 | + " to be typeof " + | ||
| 87 | + expected + | ||
| 88 | + ", actual typeof was " + | ||
| 89 | + actual | ||
| 90 | + ); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + export class Check { | ||
| 94 | + static defined(name) { | ||
| 95 | + if (!defined(name)) { | ||
| 96 | + throw new DeveloperError(getUndefinedErrorMessage(name)); | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + static typeOfNumber(name, test) { | ||
| 101 | + if (typeof test !== "number") { | ||
| 102 | + throw new DeveloperError( | ||
| 103 | + getFailedTypeErrorMessage(typeof test, "number", name) | ||
| 104 | + ); | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + static typeOfFunc(name, test) { | ||
| 109 | + if (typeof test !== "function") { | ||
| 110 | + throw new DeveloperError( | ||
| 111 | + getFailedTypeErrorMessage(typeof test, "function", name) | ||
| 112 | + ); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + static typeOfString(name, test) { | ||
| 117 | + if (typeof test !== "string") { | ||
| 118 | + throw new DeveloperError( | ||
| 119 | + getFailedTypeErrorMessage(typeof test, "string", name) | ||
| 120 | + ); | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + static typeOfBool(name, test) { | ||
| 125 | + if (typeof test !== "boolean") { | ||
| 126 | + throw new DeveloperError( | ||
| 127 | + getFailedTypeErrorMessage(typeof test, "boolean", name) | ||
| 128 | + ); | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + static typeOfArray(name, test) { | ||
| 133 | + if (!isArray(test)) { | ||
| 134 | + throw new DeveloperError( | ||
| 135 | + getFailedTypeErrorMessage(typeof test, "array", name) | ||
| 136 | + ); | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + static typeofNumberAndLessThan(name, test, limit) { | ||
| 141 | + Check.typeOfNumber(name, test); | ||
| 142 | + if (test >= limit) { | ||
| 143 | + throw new DeveloperError( | ||
| 144 | + "Expected " + | ||
| 145 | + name + | ||
| 146 | + " to be less than " + | ||
| 147 | + limit + | ||
| 148 | + ", actual value was " + | ||
| 149 | + test | ||
| 150 | + ); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments