@lightningjs/renderer
Version:
Lightning 3 Renderer
82 lines • 3.54 kB
JavaScript
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2024 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 { DynamicShaderController, } from '../../../main-api/DynamicShaderController.js';
import { CoreShaderManager } from '../../CoreShaderManager.js';
import { DefaultShader } from './shaders/DefaultShader.js';
import { DynamicShader, } from './shaders/DynamicShader.js';
import { SdfShader } from './shaders/SdfShader.js';
export class WebGlCoreShaderManager extends CoreShaderManager {
effectConstructors = {};
constructor(renderer) {
super();
this.renderer = renderer;
this.registerShaderType('DefaultShader', DefaultShader);
this.registerShaderType('DynamicShader', DynamicShader);
this.registerShaderType('SdfShader', SdfShader);
}
registerEffectType(effectType, effectClass) {
this.effectConstructors[effectType] = effectClass;
}
getRegisteredEffects() {
return this.effectConstructors;
}
loadShader(shType, props) {
if (!this.renderer) {
throw new Error(`Renderer is not been defined`);
}
const ShaderClass = this.shConstructors[shType];
if (!ShaderClass) {
throw new Error(`Shader type "${shType}" is not registered`);
}
if (shType === 'DynamicShader') {
return this.loadDynamicShader(props);
}
const resolvedProps = ShaderClass.resolveDefaults(props);
const cacheKey = ShaderClass.makeCacheKey(resolvedProps) || ShaderClass.name;
if (cacheKey && this.shCache.has(cacheKey)) {
return this._createShaderCtr(shType, this.shCache.get(cacheKey), resolvedProps);
}
// @ts-expect-error ShaderClass WILL accept a Renderer
const shader = new ShaderClass(this.renderer, props);
if (cacheKey) {
this.shCache.set(cacheKey, shader);
}
return this._createShaderCtr(shType, shader, resolvedProps);
}
loadDynamicShader(props) {
if (!this.renderer) {
throw new Error(`Renderer is not been defined`);
}
const resolvedProps = DynamicShader.resolveDefaults(props, this.effectConstructors);
const cacheKey = DynamicShader.makeCacheKey(resolvedProps, this.effectConstructors);
if (cacheKey && this.shCache.has(cacheKey)) {
return this._createDynShaderCtr(this.shCache.get(cacheKey), resolvedProps);
}
const shader = new DynamicShader(this.renderer, props, this.effectConstructors);
if (cacheKey) {
this.shCache.set(cacheKey, shader);
}
return this._createDynShaderCtr(shader, resolvedProps);
}
_createDynShaderCtr(shader, props) {
shader.bindUniformMethods(props);
return new DynamicShaderController(shader, props, this);
}
}
//# sourceMappingURL=WebGlCoreShaderManager.js.map