Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScriptLoader #2417

Open
wants to merge 18 commits into
base: dev/1.4
Choose a base branch
from
20 changes: 10 additions & 10 deletions packages/core/src/animation/Keyframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export class Keyframe<
T = V extends number
? number
: V extends Vector2
? Vector2
: V extends Vector3
? Vector3
: V extends Vector4 | Color | Quaternion | Rect
? Vector4
: V extends number[] | Float32Array
? number[]
: V extends ReferResource
? ReferResource
: never
? Vector2
: V extends Vector3
? Vector3
: V extends Vector4 | Color | Quaternion | Rect
? Vector4
: V extends number[] | Float32Array
? number[]
: V extends ReferResource
? ReferResource
: never
> {
/** The time of the Keyframe. */
time: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ export class AnimationCurveOwner<V extends KeyframeValueType> {
? srcCurve._evaluateAdditive(srcTime, this.baseEvaluateData)
: srcCurve._evaluate(srcTime, this.baseEvaluateData)
: additive
? this.cureType._getZeroValue(this.baseEvaluateData.value)
: this.defaultValue;
? this.cureType._getZeroValue(this.baseEvaluateData.value)
: this.defaultValue;

const destValue =
destCurve && destCurve.keys.length
? additive
? destCurve._evaluateAdditive(destTime, this.crossEvaluateData)
: destCurve._evaluate(destTime, this.crossEvaluateData)
: additive
? this.cureType._getZeroValue(this.crossEvaluateData.value)
: this.defaultValue;
? this.cureType._getZeroValue(this.crossEvaluateData.value)
: this.defaultValue;

return this._lerpValue(srcValue, destValue, crossWeight);
}
Expand All @@ -126,8 +126,8 @@ export class AnimationCurveOwner<V extends KeyframeValueType> {
? destCurve._evaluateAdditive(destTime, this.crossEvaluateData)
: destCurve._evaluate(destTime, this.crossEvaluateData)
: additive
? this.cureType._getZeroValue(this.crossEvaluateData.value)
: this.defaultValue;
? this.cureType._getZeroValue(this.crossEvaluateData.value)
: this.defaultValue;

return this._lerpValue(srcValue, destValue, crossWeight);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/asset/AssetType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ export enum AssetType {
/** Source Font, include ttf、 otf and woff. */
SourceFont = "SourceFont",
/** Project asset. */
Project = "project"
Project = "project",
/** Script in ES module. */
Script = "Script"
}
20 changes: 20 additions & 0 deletions packages/loader/src/ScriptLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AssetPromise, AssetType, Loader, LoadItem, resourceLoader, Script } from "@galacean/engine-core";

export interface ESModule {
default?: any;
[key: string]: any;
}
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

@resourceLoader(AssetType.Script, ["js", "mjs"], false)
export class ScriptLoader extends Loader<ESModule> {
load(item: LoadItem): AssetPromise<ESModule> {
return new AssetPromise((resolve, reject) => {
const { url } = item;
(import(/* @vite-ignore */ url) as Promise<ESModule>)
.then((esModule) => {
resolve(esModule);
})
.catch(reject);
});
}
}
2 changes: 2 additions & 0 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import "./SpriteLoader";
import "./Texture2DLoader";
import "./TextureCubeLoader";
import "./ktx2/KTX2Loader";
import "./ScriptLoader";

export { GLTFLoader } from "./GLTFLoader";
export type { GLTFParams } from "./GLTFLoader";
export * from "./SceneLoader";
export type { Texture2DParams } from "./Texture2DLoader";
export { parseSingleKTX } from "./compressed-texture";
export { ScriptLoader, type ESModule } from "./ScriptLoader";
export * from "./gltf";
export { KTX2Loader, KTX2Transcoder } from "./ktx2/KTX2Loader";
export { KTX2TargetFormat } from "./ktx2/KTX2TargetFormat";
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ function getAll() {
return [...getModule(), ...getMini(), ...getUMD()];
}

export default Promise.all(promises);
export default Promise.all(promises);
55 changes: 55 additions & 0 deletions tests/src/loader/ScriptLoader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { WebGLEngine } from "@galacean/engine-rhi-webgl";
import { AssetType } from "@galacean/engine-core";
import "@galacean/engine-loader";
import type { ESModule } from "@galacean/engine-loader";
import { describe, it, expect, beforeAll, afterAll } from "vitest";

let engine: WebGLEngine;

beforeAll(async () => {
const canvasDOM = document.createElement("canvas");
canvasDOM.width = 1024;
canvasDOM.height = 1024;
engine = await WebGLEngine.create({ canvas: canvasDOM });
});

describe("ScriptLoader test", function () {
it("loader from string url", async () => {

engine.resourceManager.load<ESModule>({
url: "https://cdn.jsdelivr.net/npm/[email protected]/+esm",
type: AssetType.Script
})
.then((script) => {
expect(script).not.to.be.null;
expect(script.default).not.to.be.null;
expect(script.colord).not.to.be.null;
expect(script.getFormat).not.to.be.null;
expect(script.random).not.to.be.null;
});
});
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

it("loader from blob raw script text", async () => {
const esModuleString = `
export const colord = "colord";
export const getFormat = () => "getFormat";
export default colord;
`
engine.resourceManager.load<ESModule>({
url: URL.createObjectURL(new Blob([esModuleString], { type: "application/javascript" })),
type: AssetType.Script
})
.then((script) => {
expect(script).not.to.be.null;
expect(script.colord).not.to.be.null;
expect(script.getFormat).not.to.be.null;
expect(script.default).not.to.be.null;
expect(script.default).equal(script.colord)
})
});

afterAll(function () {
engine.resourceManager.gc();
engine.destroy();
});
});
Loading