Compare commits

...

2 Commits

8 changed files with 129 additions and 18 deletions

View File

@ -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>

View File

@ -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 };

View File

@ -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;
@ -61,7 +61,7 @@ type WebGLCanvasEventMap = {
class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> { class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> {
public readonly canvas: HTMLCanvasElement | OffscreenCanvas; public readonly element: HTMLCanvasElement | OffscreenCanvas;
public readonly context: WebGLRenderingContext | WebGL2RenderingContext; public readonly context: WebGLRenderingContext | WebGL2RenderingContext;
@ -214,7 +214,7 @@ class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> {
} }
} }
this.canvas = targetCanvas; this.element = targetCanvas;
this.context = targetContext; this.context = targetContext;
this.glVersion = targetGLVersion; this.glVersion = targetGLVersion;
this.isOffScreen = targetIsOffScreen; this.isOffScreen = targetIsOffScreen;
@ -238,12 +238,12 @@ class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> {
let hasResized = false; let hasResized = false;
if (width !== void 0) { if (width !== void 0) {
this.canvas.width = width; this.element.width = width;
hasResized = true; hasResized = true;
} }
if (height !== void 0) { if (height !== void 0) {
this.canvas.height = height; this.element.height = height;
hasResized = true; hasResized = true;
} }
@ -280,8 +280,8 @@ class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> {
} }
} }
if (targetcontainer === null && this.canvas instanceof HTMLCanvasElement) { if (targetcontainer === null && this.element instanceof HTMLCanvasElement) {
targetcontainer = this.canvas.parentElement; targetcontainer = this.element.parentElement;
} }
if (targetcontainer === null) { if (targetcontainer === null) {
@ -307,11 +307,11 @@ class WebGLCanvas extends EventEmitter<WebGLCanvasEventMap> {
this.resizeObserver = resizeObserver; this.resizeObserver = resizeObserver;
if (this.canvas instanceof HTMLElement) { if (this.element instanceof HTMLElement) {
this.canvas.style.width = "100%"; this.element.style.width = "100%";
this.canvas.style.height = "100%"; this.element.style.height = "100%";
this.canvas.style.boxSizing = "border-box"; this.element.style.boxSizing = "border-box";
this.canvas.style.display = "block"; this.element.style.display = "block";
} }
resizeObserver.observe(targetcontainer); resizeObserver.observe(targetcontainer);

View 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 };

View 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 };

View File

@ -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";

View File

@ -13,7 +13,8 @@
], ],
"paths": { "paths": {
"kernel/*": ["./source/kernel/*"], "kernel/*": ["./source/kernel/*"],
"renderer/*": ["./source/renderer/*"] "renderer/*": ["./source/renderer/*"],
"state/*": ["./source/state/*"]
} }
} }
} }