Skip to content

Commit

Permalink
feat: support env loader (galacean#577)
Browse files Browse the repository at this point in the history
Co-authored-by: shensi.zxd <[email protected]>
  • Loading branch information
zhuxudong and shensi.zxd authored Nov 11, 2021
1 parent 9ff36e8 commit 44f24c0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
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

0 comments on commit 44f24c0

Please sign in to comment.