aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/user/ThreeVideo.ts59
-rw-r--r--src/user/index.ts20
2 files changed, 11 insertions, 68 deletions
diff --git a/src/user/ThreeVideo.ts b/src/user/ThreeVideo.ts
index 828c6c9..b7ed438 100644
--- a/src/user/ThreeVideo.ts
+++ b/src/user/ThreeVideo.ts
@@ -1,64 +1,7 @@
-import { Video as BaseVideo, type FrameTime, type InitConfig } from '$/lib/Player/Video';
+import { Video as BaseVideo, type InitConfig } from '$/lib/Player/Video';
import * as THREE from 'three';
import type { OrbitControls } from 'three/examples/jsm/Addons.js';
-export type Proxy<T> = T
-export const OnceCell = <T>(create: () => T): Proxy<T> => {
- let value = null as unknown as T;
- let created = false;
- let createOnce = (): T extends object ? T : never => {
- // @ts-ignore
- if (created) return value; else return (created = true, value = create())
- }
- return new Proxy({
- get value() {
- return createOnce()
- }
- }, {
- get(_, ...args) {
- return Reflect.get(createOnce(), ...args)
- },
- set(_, ...args) {
- return Reflect.set(createOnce(), ...args)
- },
- has(_, ...args) {
- return Reflect.has(createOnce(), ...args)
- },
- deleteProperty(_, ...args) {
- return Reflect.deleteProperty(createOnce(), ...args)
- },
- isExtensible(_, ...args) {
- return Reflect.isExtensible(createOnce(), ...args)
- },
- ownKeys(_, ...args) {
- return Reflect.ownKeys(createOnce(), ...args)
- },
- defineProperty(_, ...args) {
- return Reflect.defineProperty(createOnce(), ...args)
- },
- getOwnPropertyDescriptor(_, ...args) {
- return Reflect.getOwnPropertyDescriptor(createOnce(), ...args)
- },
- preventExtensions(_, ...args) {
- return Reflect.preventExtensions(createOnce(), ...args)
- },
- getPrototypeOf(_, ...args) {
- return Reflect.getPrototypeOf(createOnce(), ...args)
- },
- setPrototypeOf(_, ...args) {
- return Reflect.setPrototypeOf(createOnce(), ...args)
- },
- apply(_, ...args) {
- type F = (this: any, ...args: any[]) => any
- return Reflect.apply(createOnce() as unknown as T extends F ? F : never, ...args)
- },
- construct(_, ...args) {
- type F = (this: any, ...args: any[]) => any
- return Reflect.construct(createOnce() as unknown as T extends F ? F : never, ...args)
- },
- }) as Proxy<T>
-}
-
export default abstract class ThreeVideo extends BaseVideo {
protected abstract ctx: CanvasRenderingContext2D
protected scene!: THREE.Scene;
diff --git a/src/user/index.ts b/src/user/index.ts
index b4f0501..c6969c3 100644
--- a/src/user/index.ts
+++ b/src/user/index.ts
@@ -1,8 +1,8 @@
import { type FrameTime, type InitConfig } from '$/lib/Player/Video';
-import ThreeVideo, { OnceCell } from './ThreeVideo';
+import ThreeVideo from './ThreeVideo';
import AudioURL from './03. Lemaitre, Jennie A. - Closer - 40sec version.flac?url'
import * as THREE from 'three';
-import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry.js';
+import { LazyCell } from '$/lib/vendor/lazy-cell';
type FontInfo = {
family: string, size: number, weight?: number
@@ -64,17 +64,17 @@ export default class Video extends ThreeVideo {
const rs = await threeInit
if (rs && rs[0] === 1) { console.error(rs[1]); throw new Error('Failed to initialize ThreeJS!'); }
}
- protected uiGeometry = OnceCell(() => new THREE.BoxGeometry(0.1, 5, 7));
- protected uiDarkMaterial = OnceCell(() => new THREE.MeshStandardMaterial({
+ protected uiGeometry = new LazyCell(() => new THREE.BoxGeometry(0.1, 5, 7));
+ protected uiDarkMaterial = new LazyCell(() => new THREE.MeshStandardMaterial({
roughness: 0.8,
color: 0xffffff,
metalness: 0.2,
bumpScale: 1
}));
- protected uiDark = OnceCell(() => new THREE.Mesh(this.uiGeometry, this.uiDarkMaterial))
- protected uiLightMaterial = OnceCell(() => new THREE.MeshBasicMaterial({ color: 0x000000 }))
- protected uiLight = OnceCell(() => new THREE.Mesh(this.uiGeometry, this.uiLightMaterial))
- protected lighting = OnceCell(() => {
+ protected uiDark = new LazyCell(() => new THREE.Mesh(this.uiGeometry, this.uiDarkMaterial))
+ protected uiLightMaterial = new LazyCell(() => new THREE.MeshBasicMaterial({ color: 0x000000 }))
+ protected uiLight = new LazyCell(() => new THREE.Mesh(this.uiGeometry, this.uiLightMaterial))
+ protected lighting = new LazyCell(() => {
const dirLight = new THREE.DirectionalLight(0xffffff, 3);
dirLight.castShadow = true;
dirLight.shadow.camera.top = 0;
@@ -93,13 +93,13 @@ export default class Video extends ThreeVideo {
return dirLight;
})
- protected uiCanvas = OnceCell(() => {
+ protected uiCanvas = new LazyCell(() => {
const c = document.createElement('canvas');
c.width = this.px(1000);
c.height = c.width / 5 * 7;
return c;
});
- protected uiCanvasCtx = OnceCell(() => this.uiCanvas.getContext('2d', {
+ protected uiCanvasCtx = new LazyCell(() => this.uiCanvas.getContext('2d', {
alpha: true,
}))
protected drawUiCanvas() { }