UNPKG

lingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

92 lines 3.4 kB
import { applyMixins, forceGet } from "@lincode/utils"; import { ExtrudeGeometry, Group, Mesh } from "three"; import Loaded from "./core/Loaded"; import TexturedStandardMixin from "./core/mixins/TexturedStandardMixin"; import fit from "./utils/fit"; import { svgMeshDefaults, svgMeshSchema } from "../interface/ISvgMesh"; import { decreaseLoadingCount, increaseLoadingCount } from "../states/useLoadingCount"; import { standardMaterial } from "./utils/reusables"; import { M2CM } from "../globals"; import { measure } from "../memo/measure"; import { shadowModePtr } from "../pointers/shadowModePtr"; import isOpaque from "../memo/isOpaque"; const svgGeometryCache = new WeakMap(); class SvgMesh extends Loaded { static componentName = "svgMesh"; static defaults = svgMeshDefaults; static schema = svgMeshSchema; async $load(url) { increaseLoadingCount(); const module = await import("./utils/loaders/loadSVG"); let result; try { result = await module.default(url); } catch { decreaseLoadingCount(); throw new Error("Failed to load svg, check if src is correct"); } decreaseLoadingCount(); return result; } $resolveLoaded(svgData, src) { const loadedObject3d = new Group(); loadedObject3d.scale.y *= -1; const geometries = forceGet(svgGeometryCache, svgData, () => { const shapes = []; for (const path of svgData.paths) for (const shape of path.toShapes(true)) shapes.push(shape); if (!shapes.length) return []; const testGroup = new Group(); for (const shape of shapes) { const geom = new ExtrudeGeometry(shape, { depth: 0, bevelEnabled: false }); geom.dispose(); testGroup.add(new Mesh(geom)); } const [{ y }] = measure(src, { target: testGroup }); const result = []; for (const shape of shapes) result.push(new ExtrudeGeometry(shape, { depth: y, bevelEnabled: false })); return result; }); for (const geometry of geometries) { const mesh = new Mesh(geometry, this._material); loadedObject3d.add(mesh); mesh.receiveShadow = true; if (shadowModePtr[0] === true && isOpaque(mesh)) mesh.castShadow = true; } const [{ x, y, z }] = 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; } _material = standardMaterial; get $material() { return this._material; } set $material(val) { this._material = val; const children = (this.$loadedObject3d?.children ?? []); for (const mesh of children) mesh.material = val; } } applyMixins(SvgMesh, [TexturedStandardMixin]); export default SvgMesh; //# sourceMappingURL=SvgMesh.js.map