@luma.gl/shadertools
Version:
Shader module system for luma.gl
109 lines (93 loc) • 2.92 kB
JavaScript
import lightingShader from './lights.glsl';
const INITIAL_MODULE_OPTIONS = {
lightSources: {}
};
function convertColor() {
let {
color = [0, 0, 0],
intensity = 1.0
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return color.map(component => component * intensity / 255.0);
}
function getLightSourceUniforms(_ref) {
let {
ambientLight,
pointLights = [],
directionalLights = []
} = _ref;
const lightSourceUniforms = {};
if (ambientLight) {
lightSourceUniforms['lighting_uAmbientLight.color'] = convertColor(ambientLight);
} else {
lightSourceUniforms['lighting_uAmbientLight.color'] = [0, 0, 0];
}
pointLights.forEach((pointLight, index) => {
lightSourceUniforms["lighting_uPointLight[".concat(index, "].color")] = convertColor(pointLight);
lightSourceUniforms["lighting_uPointLight[".concat(index, "].position")] = pointLight.position;
lightSourceUniforms["lighting_uPointLight[".concat(index, "].attenuation")] = pointLight.attenuation || [1, 0, 0];
});
lightSourceUniforms.lighting_uPointLightCount = pointLights.length;
directionalLights.forEach((directionalLight, index) => {
lightSourceUniforms["lighting_uDirectionalLight[".concat(index, "].color")] = convertColor(directionalLight);
lightSourceUniforms["lighting_uDirectionalLight[".concat(index, "].direction")] = directionalLight.direction;
});
lightSourceUniforms.lighting_uDirectionalLightCount = directionalLights.length;
return lightSourceUniforms;
}
function getUniforms() {
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : INITIAL_MODULE_OPTIONS;
if ('lightSources' in opts) {
const {
ambientLight,
pointLights,
directionalLights
} = opts.lightSources || {};
const hasLights = ambientLight || pointLights && pointLights.length > 0 || directionalLights && directionalLights.length > 0;
if (!hasLights) {
return {
lighting_uEnabled: false
};
}
return Object.assign({}, getLightSourceUniforms({
ambientLight,
pointLights,
directionalLights
}), {
lighting_uEnabled: true
});
}
if ('lights' in opts) {
const lightSources = {
pointLights: [],
directionalLights: []
};
for (const light of opts.lights || []) {
switch (light.type) {
case 'ambient':
lightSources.ambientLight = light;
break;
case 'directional':
lightSources.directionalLights.push(light);
break;
case 'point':
lightSources.pointLights.push(light);
break;
default:
}
}
return getUniforms({
lightSources
});
}
return {};
}
export const lights = {
name: 'lights',
vs: lightingShader,
fs: lightingShader,
getUniforms,
defines: {
MAX_LIGHTS: 3
}
};
//# sourceMappingURL=lights.js.map