UNPKG

polygonjs-engine

Version:

node-based webgl 3D engine https://polygonjs.com

116 lines (115 loc) 3.13 kB
import {CoreString} from "./String"; import {Vector2 as Vector22} from "three/src/math/Vector2"; import {Vector3 as Vector32} from "three/src/math/Vector3"; import {Vector4 as Vector42} from "three/src/math/Vector4"; import {Color as Color2} from "three/src/math/Color"; import {CoreType} from "./Type"; export class ThreeToGl { static any(value) { if (CoreType.isString(value)) { return value; } if (CoreType.isBoolean(value)) { return `${value}`; } if (CoreType.isNumber(value)) { return `${CoreString.ensureFloat(value)}`; } if (CoreType.isArray(value)) { return this.numeric_array(value); } if (value instanceof Vector22 || value instanceof Vector32 || value instanceof Vector42 || value instanceof Color2) { return this.numeric_array(value.toArray()); } return `ThreeToGl error: unknown value type '${value}'`; } static numeric_array(values) { const values_str = new Array(values.length); for (let i = 0; i < values.length; i++) { values_str[i] = `${CoreString.ensureFloat(values[i])}`; } const gl_type = `vec${values.length}`; return `${gl_type}(${values_str.join(", ")})`; } static vector4(vec) { if (CoreType.isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${CoreString.ensureFloat(v)}`; }); return `vec4(${values.join(", ")})`; } static vector3(vec) { if (CoreType.isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${CoreString.ensureFloat(v)}`; }); return `vec3(${values.join(", ")})`; } static vector2(vec) { if (CoreType.isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${CoreString.ensureFloat(v)}`; }); return `vec2(${values.join(", ")})`; } static vector3_float(vec, num) { if (!CoreType.isString(num)) { num = CoreString.ensureFloat(num); } return `vec4(${this.vector3(vec)}, ${num})`; } static float4(x, y, z, w) { if (!CoreType.isString(x)) { x = CoreString.ensureFloat(x); } if (!CoreType.isString(y)) { y = CoreString.ensureFloat(y); } if (!CoreType.isString(z)) { z = CoreString.ensureFloat(z); } if (!CoreType.isString(w)) { w = CoreString.ensureFloat(w); } return `vec4(${x}, ${y}, ${z}, ${w})`; } static float3(x, y, z) { if (!CoreType.isString(x)) { x = CoreString.ensureFloat(x); } if (!CoreType.isString(y)) { y = CoreString.ensureFloat(y); } if (!CoreType.isString(z)) { z = CoreString.ensureFloat(z); } return `vec3(${x}, ${y}, ${z})`; } static float2(x, y) { if (!CoreType.isString(x)) { x = CoreString.ensureFloat(x); } if (!CoreType.isString(y)) { y = CoreString.ensureFloat(y); } return `vec2(${x}, ${y})`; } static float(x) { if (!CoreType.isString(x)) { x = CoreString.ensureFloat(x); } return `${x}`; } static int(x) { return `${x}`; } static bool(x) { return `${x}`; } }