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

feat: support env loader #577

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/src/asset/AssetType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ export enum AssetType {
/** Cube Compress Texture. */
KTXCube = "ktx-cube",
/** Sprite Atlas. */
SpriteAtlas = "sprite-atlas"
SpriteAtlas = "sprite-atlas",
/** ambient light */
Env = "environment"
}
59 changes: 59 additions & 0 deletions packages/loader/src/EnvLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
AmbientLight,
AssetPromise,
AssetType,
DiffuseMode,
Loader,
LoadItem,
resourceLoader,
ResourceManager,
TextureCubeFace,
TextureCubeMap
} from "@oasis-engine/core";
import { SphericalHarmonics3 } from "@oasis-engine/math";

@resourceLoader(AssetType.Env, ["env"])
class EnvLoader extends Loader<AmbientLight> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<AmbientLight> {
return new AssetPromise((resolve, reject) => {
resourceManager
.load<ArrayBuffer>({
type: AssetType.Buffer,
url: item.url
})
.then((arraybuffer) => {
const shArray = new Float32Array(arraybuffer, 0, 27);
const shByteLength = 27 * 4;
const size = new Uint16Array(arraybuffer, shByteLength, 1)?.[0];

const texture = new TextureCubeMap(resourceManager.engine, size);
const mipmapCount = texture.mipmapCount;
let offset = shByteLength + 2;

for (let mipLevel = 0; mipLevel < mipmapCount; mipLevel++) {
const mipSize = size >> mipLevel;

for (let face = 0; face < 6; face++) {
const dataSize = mipSize * mipSize * 4;
const data = new Uint8Array(arraybuffer, offset, dataSize);
offset += dataSize;
texture.setPixelBuffer(TextureCubeFace.PositiveX + face, data, mipLevel);
}
}

const ambientLight = new AmbientLight();
const sh = new SphericalHarmonics3();

ambientLight.diffuseMode = DiffuseMode.SphericalHarmonics;
sh.setValueByArray(shArray);
ambientLight.diffuseSphericalHarmonics = sh;
ambientLight.specularTexture = texture;

resolve(ambientLight);
})
.catch((e) => {
reject(e);
});
});
}
}
1 change: 1 addition & 0 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./KTXLoader";
import "./Texture2DLoader";
import "./TextureCubeLoader";
import "./SpriteAtlasLoader";
import "./EnvLoader";
import "./gltf/extensions/index";

export { GLTFResource } from "./gltf/GLTFResource";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResourceManager } from "@oasis-engine/core";
import { AssetType, ResourceManager } from "@oasis-engine/core";
import { Oasis } from "../Oasis";
import { AssetConfig } from "../types";
import { SchemaResource } from "./SchemaResource";
Expand All @@ -8,7 +8,7 @@ export class AmbientLightResource extends SchemaResource {
return new Promise((resolve, reject) => {
const { url } = assetConfig;
resourceManager
.load({ url, type: "env" })
.load({ url, type: AssetType.Env })
.then((res) => {
this._resource = res;
resolve(this);
Expand Down