lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
189 lines • 6.81 kB
JavaScript
import { PropertyBinding } from "three";
import fit from "./utils/fit";
import Loaded from "./core/Loaded";
import { modelDefaults, modelSchema } from "../interface/IModel";
import AnimationManager from "./core/AnimatedObjectManager/AnimationManager";
import { M2CM } from "../globals";
import { reflectionDataMap, reflectionChangedSet } from "../collections/reflectionCollections";
import { measure } from "../memo/measure";
import { indexChildrenNames } from "../memo/indexChildrenNames";
import { getFoundManager } from "./core/utils/getFoundManager";
import { indexMeshChildrenNames } from "../memo/indexMeshChildrenNames";
import findFirst from "../memo/findFirst";
import findFirstMesh from "../memo/findFirstMesh";
import findAll from "../memo/findAll";
import findAllMeshes from "../memo/findAllMeshes";
import loadModel from "./utils/loaders/loadModel";
import { idRenderCheckMap, idRenderCheckModelMap } from "../collections/idCollections";
import getRendered from "../throttle/getRendered";
import { refreshFactorsSystem } from "../systems/configLoadedSystems/refreshFactorsSystem";
import { configCastShadowSystem } from "../systems/configLoadedSystems/configCastShadowSystem";
class Model extends Loaded {
static componentName = "model";
static defaults = modelDefaults;
static schema = modelSchema;
serializeAnimations;
async loadAnimation(url, name = url) {
;
(this.serializeAnimations ??= {})[name] = url;
const clip = (await loadModel(url, false)).animations[0];
if (!clip)
return;
this.append((this.animations[name] = new AnimationManager(name, clip, await new Promise((resolve) => this.$events.once("loaded", resolve)), this.$animationStates)));
if (name === this.animation)
this.animation = name;
}
get animations() {
return super.animations;
}
set animations(val) {
for (const [key, value] of Object.entries(val))
if (typeof value === "string")
this.loadAnimation(value, key);
else
super.animations[key] = value;
}
$load(url) {
return loadModel(url, true);
}
_resize;
get resize() {
return this._resize ?? true;
}
set resize(val) {
this._resize = val;
this.$loadedObject3d && (this.src = this._src);
}
$resolveLoaded(loadedObject3d, src) {
if (loadedObject3d.animations.length) {
for (const clip of loadedObject3d.animations) {
const animation = (this.animations[clip.name] =
new AnimationManager(clip.name, clip, loadedObject3d, this.$animationStates));
this.append(animation);
}
}
const [{ x, y, z }] = this._resize === false
? measure(src, { target: loadedObject3d })
: fit(loadedObject3d, src);
this.runtimeDefaults = {
width: x * M2CM,
height: y * M2CM,
depth: z * M2CM
};
!this.widthSet && (this.object3d.scale.x = x);
!this.heightSet && (this.object3d.scale.y = y);
!this.depthSet && (this.object3d.scale.z = z);
return loadedObject3d;
}
disposeNode() {
super.disposeNode();
reflectionDataMap.get(this)?.[1].cancel();
}
_metalnessFactor;
get metalnessFactor() {
return this._metalnessFactor;
}
set metalnessFactor(val) {
this._metalnessFactor = val;
refreshFactorsSystem.add(this);
}
_roughnessFactor;
get roughnessFactor() {
return this._roughnessFactor;
}
set roughnessFactor(val) {
this._roughnessFactor = val;
refreshFactorsSystem.add(this);
}
_opacityFactor;
get opacityFactor() {
return this._opacityFactor;
}
set opacityFactor(val) {
this._opacityFactor = val;
refreshFactorsSystem.add(this);
configCastShadowSystem.add(this);
}
_envFactor;
get envFactor() {
return this._envFactor;
}
set envFactor(val) {
this._envFactor = val;
refreshFactorsSystem.add(this);
}
_reflection;
get reflection() {
return this._reflection ?? false;
}
set reflection(val) {
val !== this._reflection && reflectionChangedSet.add(this);
this._reflection = val;
refreshFactorsSystem.add(this);
}
find(name) {
if (!this.$loadedObject3d)
return;
const child = indexChildrenNames(this.$loadedObject3d).get(PropertyBinding.sanitizeNodeName(name));
if (child)
return getFoundManager(child, this);
}
_findFirst(name, children) {
for (const child of children.values())
if (name(child.name))
return getFoundManager(child, this);
}
findFirst(name) {
if (!this.$loadedObject3d)
return;
if (typeof name === "string")
return findFirst(this, name);
return this._findFirst(name, indexChildrenNames(this.$loadedObject3d));
}
findFirstMesh(name) {
if (!this.$loadedObject3d)
return;
if (typeof name === "string")
return findFirstMesh(this, name);
return this._findFirst(name, indexMeshChildrenNames(this.$loadedObject3d));
}
_findAll(name, children) {
const result = [];
for (const child of children.values())
name(child.name) && result.push(getFoundManager(child, this));
return result;
}
findAll(name) {
if (!this.$loadedObject3d)
return [];
if (!name)
return findAll(this.$loadedObject3d, "", { owner: this });
if (typeof name === "string")
return findAll(this.$loadedObject3d, name, { owner: this });
return this._findAll(name, indexChildrenNames(this.$loadedObject3d));
}
findAllMeshes(name) {
if (!this.$loadedObject3d)
return [];
if (!name)
return findAllMeshes(this.$loadedObject3d, "", { owner: this });
if (typeof name === "string")
return findAllMeshes(this.$loadedObject3d, name, { owner: this });
return this._findAll(name, indexMeshChildrenNames(this.$loadedObject3d));
}
initRenderCheck;
get isRendered() {
if (!this.$loadedObject3d)
return false;
if (!this.initRenderCheck) {
this.initRenderCheck = true;
for (const child of this.findAllMeshes()) {
idRenderCheckMap.set(child.object3d.id, child);
idRenderCheckModelMap.set(child.object3d.id, this);
}
}
return getRendered().has(this);
}
}
export default Model;
//# sourceMappingURL=Model.js.map