Detach the GL context state to a separate module.
This commit is contained in:
parent
4cd5ebb936
commit
c494156983
@ -4,23 +4,25 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, onMounted } from "vue";
|
import { ref, onMounted } from "vue";
|
||||||
import { WebGLCanvas, EventEmitter } from "@ray-lab/renderer-webgl";
|
import { WebGLRenderer } from "@ray-lab/renderer-webgl";
|
||||||
|
|
||||||
const contentRef = ref<HTMLDivElement>();
|
const contentRef = ref<HTMLDivElement>();
|
||||||
const canvasEle = document.createElement("canvas");
|
const canvasEle = document.createElement("canvas");
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const c = new WebGLCanvas({
|
const renderer = new WebGLRenderer({
|
||||||
canvas: canvasEle,
|
canvas: canvasEle,
|
||||||
container: contentRef.value,
|
container: contentRef.value,
|
||||||
autoResize: true,
|
autoResize: true,
|
||||||
width: 800,
|
width: 800,
|
||||||
height: 600
|
height: 600
|
||||||
});
|
});
|
||||||
// c.on("resize", () => { console.log(c.canvas.width, c.canvas.height) });
|
|
||||||
if (contentRef.value) {
|
if (contentRef.value) {
|
||||||
contentRef.value.appendChild(canvasEle);
|
contentRef.value.appendChild(canvasEle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(renderer);
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -1,7 +1,22 @@
|
|||||||
|
import { WebGLCanvas } from "state/WebGLCanvas";
|
||||||
|
import { WebGLState } from "state/WebGLState";
|
||||||
|
import type { WebGLCanvasOption } from "state/WebGLCanvas";
|
||||||
|
|
||||||
|
type WebGLRendererOption = WebGLCanvasOption;
|
||||||
|
|
||||||
class WebGLRenderer {
|
class WebGLRenderer {
|
||||||
|
|
||||||
public constructor() {}
|
public canvas: WebGLCanvas;
|
||||||
|
|
||||||
|
public state: WebGLState;
|
||||||
|
|
||||||
|
public constructor(option: WebGLRendererOption) {
|
||||||
|
|
||||||
|
this.canvas = new WebGLCanvas(option);
|
||||||
|
|
||||||
|
this.state = new WebGLState();
|
||||||
|
this.state.bindCanvas(this.canvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { WebGLRenderer };
|
export { WebGLRenderer };
|
@ -1,5 +1,5 @@
|
|||||||
import { ResizeObserver as ResizeObserverPolyfill } from '@juggle/resize-observer';
|
import { ResizeObserver as ResizeObserverPolyfill } from '@juggle/resize-observer';
|
||||||
import { EventEmitter } from "./EventEmitter";
|
import { EventEmitter } from "kernel/EventEmitter";
|
||||||
|
|
||||||
const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
|
const ResizeObserver = window.ResizeObserver || ResizeObserverPolyfill;
|
||||||
|
|
37
packages/renderer-webgl/source/state/WebGLState.ts
Normal file
37
packages/renderer-webgl/source/state/WebGLState.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import type { WebGLCanvas } from "./WebGLCanvas";
|
||||||
|
import { WebGLStateAbstract } from "./WebGLStateAbstract";
|
||||||
|
|
||||||
|
class WebGLState extends WebGLStateAbstract {
|
||||||
|
|
||||||
|
private statePool: Array<WebGLStateAbstract> = [];
|
||||||
|
|
||||||
|
private registerState(state: WebGLStateAbstract) {
|
||||||
|
this.statePool.push(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override contextWillBind(newcanvas: WebGLCanvas) {
|
||||||
|
for (const state of this.statePool) {
|
||||||
|
state.bindCanvas(newcanvas);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override contextLost() {
|
||||||
|
for (const state of this.statePool) {
|
||||||
|
state.contextLost();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override resetState() {
|
||||||
|
for (const state of this.statePool) {
|
||||||
|
state.resetState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override destroy() {
|
||||||
|
for (const state of this.statePool) {
|
||||||
|
state.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { WebGLState };
|
56
packages/renderer-webgl/source/state/WebGLStateAbstract.ts
Normal file
56
packages/renderer-webgl/source/state/WebGLStateAbstract.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import type { WebGLCanvas } from "./WebGLCanvas";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used for management, caching, encapsulation, WebGL context.
|
||||||
|
*/
|
||||||
|
abstract class WebGLStateAbstract {
|
||||||
|
|
||||||
|
private _canvas: WebGLCanvas | undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* binded canvas.
|
||||||
|
*/
|
||||||
|
public get canvas(): WebGLCanvas {
|
||||||
|
if (this._canvas) {
|
||||||
|
return this._canvas;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error(
|
||||||
|
"[ray-lab WebGLStateBasic] Please get canvas instance after context binding."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind new WebGLCanvas.
|
||||||
|
*/
|
||||||
|
public bindCanvas(canvas: WebGLCanvas) {
|
||||||
|
this.contextWillBind(canvas);
|
||||||
|
this._canvas = canvas;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Life cycle hook function.
|
||||||
|
* Triggered when the context is rebound.
|
||||||
|
* @param newcanvas - New canvas object to be bound.
|
||||||
|
*/
|
||||||
|
public abstract contextWillBind(newcanvas: WebGLCanvas): any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Life cycle hook function.
|
||||||
|
* Triggered when the context is lost.
|
||||||
|
*/
|
||||||
|
public abstract contextLost(): any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset all states.
|
||||||
|
*/
|
||||||
|
public abstract resetState(): any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggered when destroyed.
|
||||||
|
*/
|
||||||
|
public abstract destroy(): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { WebGLStateAbstract };
|
@ -1,3 +1,3 @@
|
|||||||
export * from "kernel/WebGLCanvas";
|
export * from "state/WebGLCanvas";
|
||||||
export * from "kernel/EventEmitter";
|
export * from "kernel/EventEmitter";
|
||||||
export * from "renderer/WebGLRenderer";
|
export * from "renderer/WebGLRenderer";
|
@ -13,7 +13,8 @@
|
|||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"kernel/*": ["./source/kernel/*"],
|
"kernel/*": ["./source/kernel/*"],
|
||||||
"renderer/*": ["./source/renderer/*"]
|
"renderer/*": ["./source/renderer/*"],
|
||||||
|
"state/*": ["./source/state/*"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user