| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { fillRectangle} from "./2d"
- import { LayerAB, Scene } from "./Scene"
- import { createProgram, createShader } from "./utils"
- export class DebugLayer extends LayerAB {
- private vertexShaderCode = /*glsl*/ `
- attribute vec2 a_position;
- uniform mat4 u_matrix;
- void main() {
- gl_PointSize = 20.0;
- gl_Position = u_matrix * vec4(a_position, 0, 1);
- }
- `;
- private fragmentShaderCode = /*glsl*/ `
- precision mediump float;
- varying vec2 v_texCoord;
- void main() {
- gl_FragColor = vec4(0, 0, 1, 1);
- }
-
- `
-
- program: WebGLProgram
- aPositionLoc: number
- uMatrixLoc: WebGLUniformLocation
- vertexBuffer: WebGLBuffer
- vertexArray = new Float32Array(12)
- constructor(
- public readonly scene: Scene,
- ) {
- super()
- const gl = scene.gl;
- this.program = createProgram(gl,
- createShader(gl, gl.VERTEX_SHADER, this.vertexShaderCode)!,
- createShader(gl, gl.FRAGMENT_SHADER, this.fragmentShaderCode)!)!
- this.aPositionLoc = gl.getAttribLocation(this.program, "a_position")
- this.uMatrixLoc = gl.getUniformLocation(this.program, "u_matrix")!
- this.vertexBuffer = gl.createBuffer()!;
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW);
- }
-
- override draw() {
- const gl = this.scene.gl;
- gl.useProgram(this.program);
- gl.enableVertexAttribArray(this.aPositionLoc);
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
- gl.vertexAttribPointer(this.aPositionLoc, 2, gl.FLOAT, false, 0, 0)
- gl.uniformMatrix4fv(this.uMatrixLoc, false, this.scene.drawMatrix)
- gl.drawArrays(gl.POINTS, 0, 1);
- }
- override tap(cx: number, cy: number, sx: number, sy: number): void {
- console.log(`tap @${cx},${cy}`)
- const gl = this.scene.gl
- this.vertexArray[0] = cx;
- this.vertexArray[1] = cy;
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this.vertexArray, gl.STATIC_DRAW);
- this.scene.invalidate()
- }
-
- toString(): string {
- return `DebugLayer()`
- }
- }
|