UNPKG

@lightningtv/renderer

Version:
334 lines 10.3 kB
import { getNormalizedRgbaComponents } from '../../lib/utils.js'; import { CoreShaderNode } from '../CoreShaderNode.js'; export class WebGlShaderNode extends CoreShaderNode { program; updater = undefined; valueKey = ''; uniforms = { single: {}, vec2: {}, vec3: {}, vec4: {}, }; constructor(shaderKey, config, program, stage, props) { super(shaderKey, config, stage, props); this.program = program; if (config.update !== undefined) { this.updater = config.update; this.update = () => { if (this.props === undefined) { this.updater(this.node, this.props); return; } const prevKey = this.valueKey; this.valueKey = ''; for (const key in this.resolvedProps) { this.valueKey += `${key}:${this.resolvedProps[key]};`; } if (prevKey === this.valueKey) { return; } if (prevKey.length > 0) { this.stage.shManager.mutateShaderValueUsage(prevKey, -1); } const values = this.stage.shManager.getShaderValues(this.valueKey); if (values !== undefined) { this.uniforms = values; return; } //create empty uniform collection when calculating new values this.uniforms = { single: {}, vec2: {}, vec3: {}, vec4: {}, }; this.updater(this.node); this.stage.shManager.setShaderValues(this.valueKey, this.uniforms); }; } } /** * Sets the value of a RGBA variable * @param location * @param value */ uniformRGBA(location, value) { this.uniform4fv(location, new Float32Array(getNormalizedRgbaComponents(value))); } /** * Sets the value of a single float uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The value to set. */ uniform1f(location, value) { this.uniforms.single[location] = { method: 'uniform1f', value, }; } /** * Sets the value of a float array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of values to set. */ uniform1fv(location, value) { this.uniforms.single[location] = { method: 'uniform1fv', value, }; } /** * Sets the value of a single integer uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The value to set. */ uniform1i(location, value) { this.uniforms.single[location] = { method: 'uniform1i', value, }; } /** * Sets the value of an integer array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of values to set. */ uniform1iv(location, value) { this.uniforms.single[location] = { method: 'uniform1iv', value, }; } /** * Sets the value of a vec2 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. */ uniform2f(location, v0, v1) { this.uniforms.vec2[location] = { method: 'uniform2f', value: [v0, v1], }; } /** * Sets the value of a vec2 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of vec2 values to set as FloatArray. */ uniform2fv(location, value) { this.uniforms.single[location] = { method: 'uniform2fv', value, }; } /** * Sets the value of a vec2 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of vec2 values to set. */ uniform2fa(location, value) { this.uniforms.vec2[location] = { method: 'uniform2f', value, }; } /** * Sets the value of a ivec2 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. */ uniform2i(location, v0, v1) { this.uniforms.vec2[location] = { method: 'uniform2i', value: [v0, v1], }; } /** * Sets the value of an ivec2 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of ivec2 values to set. */ uniform2iv(location, value) { this.uniforms.single[location] = { method: 'uniform2iv', value, }; } /** * Sets the value of a vec3 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. * @param v2 - The third component of the vector. */ uniform3f(location, v0, v1, v2) { this.uniforms.vec3[location] = { method: 'uniform3f', value: [v0, v1, v2], }; } /** * Sets the value of a vec3 uniform variable. * * @param location - The location of the uniform variable. * @param */ uniform3fa(location, value) { this.uniforms.vec3[location] = { method: 'uniform3f', value, }; } /** * Sets the value of a vec3 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of vec3 values to set. */ uniform3fv(location, value) { this.uniforms.single[location] = { method: 'uniform3fv', value, }; } /** * Sets the value of a ivec3 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. * @param v2 - The third component of the vector. */ uniform3i(location, v0, v1, v2) { this.uniforms.vec3[location] = { method: 'uniform3i', value: [v0, v1, v2], }; } /** * Sets the value of an ivec3 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of ivec3 values to set. */ uniform3iv(location, value) { this.uniforms.single[location] = { method: 'uniform3iv', value, }; } /** * Sets the value of a vec4 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. * @param v2 - The third component of the vector. * @param v3 - The fourth component of the vector. */ uniform4f(location, v0, v1, v2, v3) { this.uniforms.vec4[location] = { method: 'uniform4f', value: [v0, v1, v2, v3], }; } /** * Sets an array of numbers * @param location The location of the uniform variable. * @param value */ uniform4fa(location, value) { this.uniforms.vec4[location] = { method: 'uniform4f', value, }; } /** * Sets the value of a vec4 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of vec4 values to set. */ uniform4fv(location, value) { this.uniforms.single[location] = { method: 'uniform4fv', value, }; } /** * Sets the value of a ivec4 uniform variable. * * @param location - The location of the uniform variable. * @param v0 - The first component of the vector. * @param v1 - The second component of the vector. * @param v2 - The third component of the vector. * @param v3 - The fourth component of the vector. */ uniform4i(location, v0, v1, v2, v3) { this.uniforms.vec4[location] = { method: 'uniform4i', value: [v0, v1, v2, v3], }; } /** * Sets the value of an ivec4 array uniform variable. * * @param location - The location of the uniform variable. * @param value - The array of ivec4 values to set. */ uniform4iv(location, value) { this.uniforms.single[location] = { method: 'uniform4iv', value, }; } /** * Sets the value of a mat2 uniform variable. * * @param location - The location of the uniform variable. * @param transpose - Whether to transpose the matrix. * @param value - The array of mat2 values to set. */ uniformMatrix2fv(location, value) { this.uniforms.single[location] = { method: 'uniformMatrix2fv', value, }; } /** * Sets the value of a mat2 uniform variable. * @param location - The location of the uniform variable. * @param value - The array of mat2 values to set. */ uniformMatrix3fv(location, value) { this.uniforms.single[location] = { method: 'uniformMatrix3fv', value, }; } /** * Sets the value of a mat4 uniform variable. * @param location - The location of the uniform variable. * @param value - The array of mat4 values to set. */ uniformMatrix4fv(location, value) { this.uniforms.single[location] = { method: 'uniformMatrix4fv', value, }; } } //# sourceMappingURL=WebGlShaderNode.js.map