silphius/app/react-components/pixi/lights.js
2024-07-19 01:27:47 -05:00

64 lines
1.6 KiB
JavaScript

import {Group, Layer} from '@pixi/layers';
class LightLayer extends Layer {
$$diffuseTexture;
$$normalTexture;
constructor(group, diffuseLayer, normalLayer) {
super(group);
this.$$diffuseLayer = diffuseLayer;
this.$$diffuseTexture = diffuseLayer.getRenderTexture();
this.$$normalTexture = normalLayer.getRenderTexture();
}
get diffuseTexture() {
return this.$$diffuseTexture;
}
get normalTexture() {
return this.$$normalTexture;
}
}
class DeferredLighting {
diffuseGroup = new Group(0, false);
lightGroup = new Group(0, false);
normalGroup = new Group(0, false);
constructor() {
this.diffuseGroup.useRenderTexture = true;
this.diffuseLayer = new Layer(this.diffuseGroup);
this.normalGroup.useRenderTexture = true;
this.normalLayer = new Layer(this.normalGroup);
this.lightLayer = new LightLayer(
this.lightGroup,
this.diffuseLayer,
this.normalLayer,
);
this.lightLayer.alpha = 0.5;
}
addToStage(stage) {
stage.addChild(this.diffuseLayer);
stage.addChild(this.normalLayer);
stage.addChild(this.lightLayer);
}
}
export const deferredLighting = new DeferredLighting();
let AmbientLight, PointLight;
if ('undefined' !== typeof window) {
const Lights = await import('@pixi/lights');
AmbientLight = class AmbientLight extends Lights.AmbientLight {
constructor(...args) {
super(...args);
this.parentGroup = deferredLighting.lightGroup;
}
}
PointLight = class PointLight extends Lights.PointLight {
constructor(...args) {
super(...args);
this.parentGroup = deferredLighting.lightGroup;
}
}
}
export {AmbientLight, PointLight};