Skip to content

Commit

Permalink
refactor(ecs): cleanup skybox system
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed May 23, 2023
1 parent 5b5922f commit 51ac3be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions components/skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
export default (options) => ({
backgroundBlur: false,
// envMap,
// rgbm: false,
...options,
});
46 changes: 20 additions & 26 deletions systems/skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ import { pipeline, skybox } from "./renderer/pex-shaders/index.js";
import { vec3 } from "pex-math";
import { quad } from "../utils.js";

function initSkybox(ctx, skybox) {
skybox._skyTexture = ctx.texture2D({
width: 512,
height: 256,
// pixelFormat: this.rgbm ? ctx.PixelFormat.RGBA8 : ctx.PixelFormat.RGBA16F,
pixelFormat: skybox.rgbm ? ctx.PixelFormat.RGBA8 : ctx.PixelFormat.RGBA, // TODO: these are the same values
encoding: skybox.rgbm ? ctx.Encoding.RGBM : ctx.Encoding.Linear,
min: ctx.Filter.Linear,
mag: ctx.Filter.Linear,
});
skybox._updateSkyTexturePass = ctx.pass({
name: "Skybox.updateSkyTexturePass",
color: [skybox._skyTexture],
clearColor: [0, 0, 0, 0],
});
}

export default function createSkyboxSystem(opts) {
const { ctx } = opts;

const skyboxSystem = {
type: "skybox-system",
cache: {},
debug: false,
};


const updateSkyTextureCmd = {
name: "Skybox.updateSkyTexture",
name: "Skybox.updateSkyTextureCmd",
pipeline: ctx.pipeline({
vert: pipeline.fullscreen.vert,
frag: skybox.skyEnvMap.frag,
Expand All @@ -27,44 +44,21 @@ export default function createSkyboxSystem(opts) {
indices: ctx.indexBuffer(quad.cells),
};

function initSkybox(ctx, entity, skybox) {
skybox._skyTexture = ctx.texture2D({
width: 512,
height: 256,
// pixelFormat: this.rgbm ? ctx.PixelFormat.RGBA8 : ctx.PixelFormat.RGBA16F,
pixelFormat: skybox.rgbm ? ctx.PixelFormat.RGBA8 : ctx.PixelFormat.RGBA,
encoding: skybox.rgbm ? ctx.Encoding.RGBM : ctx.Encoding.Linear,
min: ctx.Filter.Linear,
mag: ctx.Filter.Linear,
});
skybox._updateSkyTexturePass = ctx.pass({
name: "Skybox.updateSkyTexture",
color: [skybox._skyTexture],
clearColor: [0, 0, 0, 0],
});
}

skyboxSystem.update = (entities) => {
const skyboxEntities = entities.filter((e) => e.skybox);
for (let skyboxEntity of skyboxEntities) {
const { skybox } = skyboxEntity;
let needsUpdate = false;
let cachedProps = skyboxSystem.cache[skyboxEntity.id];
if (!cachedProps) {
initSkybox(ctx, skyboxEntity, skyboxEntity.skybox);
initSkybox(ctx, skyboxEntity.skybox);
cachedProps = skyboxSystem.cache[skyboxEntity.id] = {};
skyboxSystem.cache[skyboxEntity.id].sunPosition = [
...skybox.sunPosition,
];
needsUpdate = true;
// const skybox = new Skybox({
// ...skyboxEntity.skybox,
// ctx: opts.ctx,
// });
// skyboxSystem.cache[skyboxEntity.id] = skybox;
// skyboxEntity._skybox = skybox; //TODO: why do we need it

// skybox.updateSkyTexture();
}

if (vec3.distance(cachedProps.sunPosition, skybox.sunPosition) > 0) {
Expand Down
1 change: 1 addition & 0 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
* @typedef {object} SkyboxComponentOptions
* @property {number[]} [sunPosition]
* @property {ctx.texture2D} [envMap]
* @property {boolean} [rgbm=false]
* @property {boolean} [backgroundBlur=false]
*/
/**
Expand Down

0 comments on commit 51ac3be

Please sign in to comment.