|
| 1 | +import { LitElement, html, css } from "lit-element"; |
| 2 | +import { customElement, property } from "lit/decorators.js"; |
| 3 | +import { createRef, ref } from "lit/directives/ref.js"; |
| 4 | + |
| 5 | +@customElement("shader-layer") |
| 6 | +export class ShaderLayer extends LitElement { |
| 7 | + @property({ type: String }) shader = ''; |
| 8 | + @property({ type: Number }) width = 640; |
| 9 | + @property({ type: Number }) height = 480; |
| 10 | + |
| 11 | + private canvasRef = createRef<HTMLCanvasElement>(); |
| 12 | + private gl?: WebGLRenderingContext; |
| 13 | + private program?: WebGLProgram; |
| 14 | + private timeLocation?: WebGLUniformLocation; |
| 15 | + private resolutionLocation?: WebGLUniformLocation; |
| 16 | + private animationFrame?: number; |
| 17 | + private startTime = performance.now(); |
| 18 | + |
| 19 | + static override styles = css` |
| 20 | + :host { |
| 21 | + display: block; |
| 22 | + width: 100%; |
| 23 | + height: 100%; |
| 24 | + position: absolute; |
| 25 | + left: 0; |
| 26 | + right: 0; |
| 27 | + top: 0; |
| 28 | + bottom: 0; |
| 29 | + } |
| 30 | + canvas { |
| 31 | + width: 100%; |
| 32 | + height: 100%; |
| 33 | + position: absolute; |
| 34 | + top: 0; |
| 35 | + left: 0; |
| 36 | + mix-blend-mode: hard-light; |
| 37 | + } |
| 38 | + `; |
| 39 | + |
| 40 | + private setupWebGL() { |
| 41 | + const canvas = this.canvasRef.value; |
| 42 | + if (!canvas) return; |
| 43 | + |
| 44 | + canvas.width = this.width; |
| 45 | + canvas.height = this.height; |
| 46 | + |
| 47 | + this.gl = canvas.getContext('webgl', { |
| 48 | + alpha: true, |
| 49 | + premultipliedAlpha: false |
| 50 | + })!; |
| 51 | + if (!this.gl) return; |
| 52 | + |
| 53 | + // Enable blending for transparency |
| 54 | + this.gl.enable(this.gl.BLEND); |
| 55 | + this.gl.blendFunc(this.gl.SRC_ALPHA, this.gl.ONE_MINUS_SRC_ALPHA); |
| 56 | + |
| 57 | + const vertexShader = this.gl.createShader(this.gl.VERTEX_SHADER); |
| 58 | + if (!vertexShader) return; |
| 59 | + |
| 60 | + this.gl.shaderSource(vertexShader, ` |
| 61 | + attribute vec2 position; |
| 62 | + varying vec2 v_texCoord; |
| 63 | + void main() { |
| 64 | + v_texCoord = (position + 1.0) * 0.5; |
| 65 | + gl_Position = vec4(position, 0.0, 1.0); |
| 66 | + } |
| 67 | + `); |
| 68 | + this.gl.compileShader(vertexShader); |
| 69 | + |
| 70 | + const positions = new Float32Array([ |
| 71 | + -1, -1, |
| 72 | + 1, -1, |
| 73 | + -1, 1, |
| 74 | + -1, 1, |
| 75 | + 1, -1, |
| 76 | + 1, 1 |
| 77 | + ]); |
| 78 | + const positionBuffer = this.gl.createBuffer(); |
| 79 | + this.gl.bindBuffer(this.gl.ARRAY_BUFFER, positionBuffer); |
| 80 | + this.gl.bufferData(this.gl.ARRAY_BUFFER, positions, this.gl.STATIC_DRAW); |
| 81 | + |
| 82 | + if (!this.shader) return; |
| 83 | + |
| 84 | + this.program = this.gl.createProgram(); |
| 85 | + if (!this.program) return; |
| 86 | + |
| 87 | + const fragmentShader = this.gl.createShader(this.gl.FRAGMENT_SHADER); |
| 88 | + if (!fragmentShader) return; |
| 89 | + |
| 90 | + this.gl.shaderSource(fragmentShader, this.shader); |
| 91 | + this.gl.compileShader(fragmentShader); |
| 92 | + |
| 93 | + if (!this.gl.getShaderParameter(fragmentShader, this.gl.COMPILE_STATUS)) { |
| 94 | + console.error('Fragment shader compilation error:', this.gl.getShaderInfoLog(fragmentShader)); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + this.gl.attachShader(this.program, vertexShader); |
| 99 | + this.gl.attachShader(this.program, fragmentShader); |
| 100 | + this.gl.linkProgram(this.program); |
| 101 | + |
| 102 | + if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) { |
| 103 | + console.error('Program linking error:', this.gl.getProgramInfoLog(this.program)); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const positionLocation = this.gl.getAttribLocation(this.program, "position"); |
| 108 | + this.gl.enableVertexAttribArray(positionLocation); |
| 109 | + this.gl.vertexAttribPointer(positionLocation, 2, this.gl.FLOAT, false, 0, 0); |
| 110 | + |
| 111 | + this.timeLocation = this.gl.getUniformLocation(this.program, "iTime"); |
| 112 | + this.resolutionLocation = this.gl.getUniformLocation(this.program, "iResolution"); |
| 113 | + } |
| 114 | + |
| 115 | + private renderGl() { |
| 116 | + if (!this.gl || !this.program) return; |
| 117 | + |
| 118 | + const time = (performance.now() - this.startTime) / 1000; |
| 119 | + |
| 120 | + this.gl.viewport(0, 0, this.width, this.height); |
| 121 | + this.gl.useProgram(this.program); |
| 122 | + this.gl.clear(this.gl.COLOR_BUFFER_BIT); |
| 123 | + |
| 124 | + if (this.timeLocation) { |
| 125 | + this.gl.uniform1f(this.timeLocation, time); |
| 126 | + } |
| 127 | + if (this.resolutionLocation) { |
| 128 | + this.gl.uniform2f(this.resolutionLocation, this.width, this.height); |
| 129 | + } |
| 130 | + |
| 131 | + this.gl.drawArrays(this.gl.TRIANGLES, 0, 6); |
| 132 | + |
| 133 | + this.animationFrame = requestAnimationFrame(() => this.renderGl()); |
| 134 | + } |
| 135 | + |
| 136 | + override firstUpdated() { |
| 137 | + this.setupWebGL(); |
| 138 | + this.renderGl(); |
| 139 | + } |
| 140 | + |
| 141 | + override disconnectedCallback() { |
| 142 | + super.disconnectedCallback(); |
| 143 | + if (this.animationFrame) { |
| 144 | + cancelAnimationFrame(this.animationFrame); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + override render() { |
| 149 | + return html` |
| 150 | + <canvas ${ref(this.canvasRef)}></canvas> |
| 151 | + `; |
| 152 | + } |
| 153 | +} |
0 commit comments