UNPKG

@polygonjs/polygonjs

Version:

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

186 lines (185 loc) 4.88 kB
"use strict"; import { ensureFloat, ensureInteger } from "./String"; import { Color, Vector2, Vector3, Vector4 } from "three"; import { isString, isBoolean, isNumber, isArray } from "./Type"; import { GlConnectionPointType } from "../engine/nodes/utils/io/connections/Gl"; export const COMPONENTS_BY_GL_TYPE = { [GlConnectionPointType.BOOL]: void 0, [GlConnectionPointType.INT]: void 0, [GlConnectionPointType.FLOAT]: void 0, [GlConnectionPointType.VEC2]: ["x", "y"], [GlConnectionPointType.VEC3]: ["x", "y", "z"], [GlConnectionPointType.VEC4]: ["x", "y", "z", "w"], [GlConnectionPointType.MAT3]: void 0, [GlConnectionPointType.MAT4]: void 0, [GlConnectionPointType.SAMPLER_2D]: void 0, [GlConnectionPointType.SAMPLER_2D_ARRAY]: void 0, [GlConnectionPointType.SAMPLER_3D]: void 0, [GlConnectionPointType.SAMPLER_CUBE]: void 0, [GlConnectionPointType.SSS_MODEL]: void 0, [GlConnectionPointType.SDF_CONTEXT]: void 0, [GlConnectionPointType.SDF_MATERIAL]: void 0 }; export class ThreeToGl { static glType(glType, value) { switch (glType) { case GlConnectionPointType.BOOL: return this.bool(value); case GlConnectionPointType.INT: return this.integer(value); case GlConnectionPointType.FLOAT: return this.float(value); case GlConnectionPointType.VEC2: return this.vector2(value); case GlConnectionPointType.VEC3: return this.vector3(value); case GlConnectionPointType.VEC4: return this.vector4(value); } return `no matching implementation for glType '${glType}' in ThreeToGl.glType`; } static any(value) { if (isString(value)) { return value; } if (isBoolean(value)) { return `${value}`; } if (isNumber(value)) { return `${ensureFloat(value)}`; } if (isArray(value)) { return this.numeric_array(value); } if (value instanceof Vector2 || value instanceof Vector3 || value instanceof Vector4 || value instanceof Color) { 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] = `${ensureFloat(values[i])}`; } const gl_type = `vec${values.length}`; return `${gl_type}(${values_str.join(", ")})`; } static mat4(vec) { if (isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${ensureFloat(v)}`; }); return `mat4(${values.join(", ")})`; } static mat3(vec) { if (isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${ensureFloat(v)}`; }); return `mat3(${values.join(", ")})`; } static vector4(vec) { if (isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${ensureFloat(v)}`; }); return `vec4(${values.join(", ")})`; } static vector3(vec) { if (isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${ensureFloat(v)}`; }); return `vec3(${values.join(", ")})`; } static vector2(vec) { if (isString(vec)) { return vec; } const values = vec.toArray().map((v) => { return `${ensureFloat(v)}`; }); return `vec2(${values.join(", ")})`; } static vector3_float(vec, num) { if (isNumber(num)) { num = ensureFloat(num); } return `vec4(${this.vector3(vec)}, ${num})`; } static float4(x, y, z, w) { if (isNumber(x)) { x = ensureFloat(x); } if (isNumber(y)) { y = ensureFloat(y); } if (isNumber(z)) { z = ensureFloat(z); } if (isNumber(w)) { w = ensureFloat(w); } return `vec4(${x}, ${y}, ${z}, ${w})`; } static float3(x, y, z) { if (isNumber(x)) { x = ensureFloat(x); } if (isNumber(y)) { y = ensureFloat(y); } if (isNumber(z)) { z = ensureFloat(z); } return `vec3(${x}, ${y}, ${z})`; } static float2(x, y) { if (isNumber(x)) { x = ensureFloat(x); } if (isNumber(y)) { y = ensureFloat(y); } return `vec2(${x}, ${y})`; } static float(x) { if (isNumber(x)) { return ensureFloat(x); } else { const converted = parseFloat(x); if (isNaN(converted)) { return x; } else { return ensureFloat(converted); } } } static integer(x) { if (isNumber(x)) { return ensureInteger(x); } else { const converted = parseInt(x); if (isNaN(converted)) { return x; } else { return ensureInteger(converted); } } } static bool(x) { if (isBoolean(x)) { return `${x}`; } else { return x; } } }