UNPKG

@polygonjs/polygonjs

Version:

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

128 lines (127 loc) 4.02 kB
"use strict"; import { Color, Vector2, Vector3, Vector4 } from "three"; import { isNumber, isArray } from "../../../Type"; export function uniqRelatedEntities(entities, callback, target) { target.length = 0; const entityByIndex = /* @__PURE__ */ new Map(); for (const entity of entities) { const relatedEntities = callback(entity); for (const relatedEntity of relatedEntities) { let newEntity = entityByIndex.get(relatedEntity.index()); if (!newEntity) { newEntity = relatedEntity; entityByIndex.set(newEntity.index(), newEntity); } } } entityByIndex.forEach((entity) => { target.push(entity); }); } const _relatedEntityIds = []; const _entityIndices = /* @__PURE__ */ new Set(); export function uniqRelatedEntityIds(entityIds, callback, target) { target.length = 0; _entityIndices.clear(); for (const entityId of entityIds) { callback(entityId, _relatedEntityIds); for (const _relatedEntityId of _relatedEntityIds) { _entityIndices.add(_relatedEntityId); } } _entityIndices.forEach((entityId) => { target.push(entityId); }); } export function attribValueNonPrimitive(src) { return src instanceof Color || src instanceof Vector2 || src instanceof Vector3 || src instanceof Vector4; } export function copyAttribValue(src, target) { if (target instanceof Color && src instanceof Color) { target.copy(src); } if (target instanceof Vector2 && src instanceof Vector2) { target.copy(src); } if (target instanceof Vector3 && src instanceof Vector3) { target.copy(src); } if (target instanceof Vector4 && src instanceof Vector4) { target.copy(src); } } export function cloneAttribValue(src) { if (src instanceof Color) { return src.clone(); } if (src instanceof Vector2) { return src.clone(); } if (src instanceof Vector3) { return src.clone(); } if (src instanceof Vector4) { return src.clone(); } } export function attributeNumericValues(object, entitiesCountFunction, attribSize = 1, defaultValue = 0, target) { target.values.length = 0; const values = target.values; const entitiesCount = entitiesCountFunction(object); if (isNumber(defaultValue)) { for (let i = 0; i < entitiesCount; i++) { for (let j = 0; j < attribSize; j++) { values.push(defaultValue); } } target.attributeAdded = true; } else { if (attribSize > 1) { if (isArray(defaultValue)) { for (let i = 0; i < entitiesCount; i++) { for (let j = 0; j < attribSize; j++) { values.push(defaultValue[j]); } } target.attributeAdded = true; } else { const vec2 = defaultValue; if (attribSize == 2 && vec2.x != null && vec2.y != null) { for (let i = 0; i < entitiesCount; i++) { values.push(vec2.x); values.push(vec2.y); } target.attributeAdded = true; } const vec3 = defaultValue; if (attribSize == 3 && vec3.x != null && vec3.y != null && vec3.z != null) { for (let i = 0; i < entitiesCount; i++) { values.push(vec3.x); values.push(vec3.y); values.push(vec3.z); } target.attributeAdded = true; } const col = defaultValue; if (attribSize == 3 && col.r != null && col.g != null && col.b != null) { for (let i = 0; i < entitiesCount; i++) { values.push(col.r); values.push(col.g); values.push(col.b); } target.attributeAdded = true; } const vec4 = defaultValue; if (attribSize == 4 && vec4.x != null && vec4.y != null && vec4.z != null && vec4.w != null) { for (let i = 0; i < entitiesCount; i++) { values.push(vec4.x); values.push(vec4.y); values.push(vec4.z); values.push(vec4.w); } target.attributeAdded = true; } } } } }