UNPKG

@lightningjs/renderer

Version:
91 lines (76 loc) 2.78 kB
/* * If not stated otherwise in this file or this component's LICENSE file the * following copyright and licenses apply: * * Copyright 2023 Comcast Cable Communications Management, LLC. * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { WebGlCoreRenderer } from '../WebGlCoreRenderer.js'; import { WebGlCoreShader } from '../WebGlCoreShader.js'; import type { WebGlCoreCtxTexture } from '../WebGlCoreCtxTexture.js'; import type { ShaderProgramSources } from '../internal/ShaderUtils.js'; export class DefaultShader extends WebGlCoreShader { constructor(renderer: WebGlCoreRenderer) { super({ renderer, }); } override bindTextures(textures: WebGlCoreCtxTexture[]) { const { glw } = this; glw.activeTexture(0); glw.bindTexture(textures[0]!.ctxTexture); } static override shaderSources: ShaderProgramSources = { vertex: ` # ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; # else precision mediump float; # endif attribute vec2 a_position; attribute vec2 a_textureCoordinate; attribute vec2 a_nodeCoordinate; attribute vec4 a_color; uniform vec2 u_resolution; uniform float u_pixelRatio; varying vec4 v_color; varying vec2 v_textureCoordinate; varying vec2 v_nodeCoordinate; void main() { vec2 normalized = a_position * u_pixelRatio; vec2 screenSpace = vec2(2.0 / u_resolution.x, -2.0 / u_resolution.y); v_color = a_color; v_textureCoordinate = a_textureCoordinate; v_nodeCoordinate = a_nodeCoordinate; gl_Position = vec4(normalized.x * screenSpace.x - 1.0, normalized.y * -abs(screenSpace.y) + 1.0, 0.0, 1.0); gl_Position.y = -sign(screenSpace.y) * gl_Position.y; } `, fragment: ` # ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; # else precision mediump float; # endif uniform vec2 u_resolution; uniform sampler2D u_texture; varying vec4 v_color; varying vec2 v_textureCoordinate; void main() { vec4 color = texture2D(u_texture, v_textureCoordinate); gl_FragColor = vec4(v_color) * texture2D(u_texture, v_textureCoordinate); } `, }; }