Skip to content

Commit

Permalink
fix shader uniform texture arrays in style definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
bcamper committed Jan 31, 2021
1 parent 6743110 commit 13db98a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
33 changes: 11 additions & 22 deletions src/gl/glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@ export default GLSL;
for actually setting the uniforms). For example, this could be used as a key into a dictionary of
known texture names, or it could simply be used as a URL to dynamically load the texture from.
*/
GLSL.parseUniforms = function (uniforms) {
GLSL.parseUniforms = function (uniforms = {}) {
var parsed = [];

for (var name in uniforms) {
var key = name; // save the original name
var uniform = uniforms[name];
var u;

for (const [name, uniform] of Object.entries(uniforms)) {
// Single float
if (typeof uniform === 'number') {
parsed.push({
type: 'float',
method: '1f',
name,
value: uniform,
key,
uniforms
path: [name]
});
}
// Array: vector, array of floats, array of textures
Expand All @@ -47,8 +42,7 @@ GLSL.parseUniforms = function (uniforms) {
method: uniform.length + 'fv',
name,
value: uniform,
key,
uniforms
path: [name]
});
}
// float array
Expand All @@ -58,22 +52,20 @@ GLSL.parseUniforms = function (uniforms) {
method: '1fv',
name: name + '[0]',
value: uniform,
key,
uniforms
path: [name]
});
}
// TODO: assume matrix for (typeof == Float32Array && length == 16)?
}
// Array of textures
else if (typeof uniform[0] === 'string') {
for (u=0; u < uniform.length; u++) {
for (let u = 0; u < uniform.length; u++) {
parsed.push({
type: 'sampler2D',
method: '1i',
name: name + '[' + u + ']',
value: uniform[u],
key: u,
uniforms: uniform
path: [name, u]
});
}
}
Expand All @@ -82,14 +74,13 @@ GLSL.parseUniforms = function (uniforms) {
// float vectors (vec2, vec3, vec4)
if (uniform[0].length >= 2 && uniform[0].length <= 4) {
// Set each vector in the array
for (u=0; u < uniform.length; u++) {
for (let u = 0; u < uniform.length; u++) {
parsed.push({
type: 'vec' + uniform[0].length,
method: uniform[0].length + 'fv',
name: name + '[' + u + ']',
value: uniform[u],
key: u,
uniforms: uniform
path: [name, u]
});
}
}
Expand All @@ -103,8 +94,7 @@ GLSL.parseUniforms = function (uniforms) {
method: '1i',
name,
value: uniform,
key,
uniforms
path: [name]
});
}
// Texture
Expand All @@ -114,8 +104,7 @@ GLSL.parseUniforms = function (uniforms) {
method: '1i',
name,
value: uniform,
key,
uniforms
path: [name]
});
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/gl/shader_program.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,16 @@ export default class ShaderProgram {
}

// Parse uniform types and values from the JS object
const parsed = GLSL.parseUniforms(uniforms);

// Set each uniform
for (let u=0; u < parsed.length; u++) {
const uniform = parsed[u];
if (uniform.type === 'sampler2D') {
// For textures, we need to track texture units, so we have a special setter
this.setTextureUniform(uniform.name, uniform.value);
}
else {
this.uniform(uniform.method, uniform.name, uniform.value);
}
}
GLSL.parseUniforms(uniforms)
.forEach(({ name, type, value, method }) => {
if (type === 'sampler2D') {
// For textures, we need to track texture units, so we have a special setter
this.setTextureUniform(name, value);
}
else {
this.uniform(method, name, value);
}
});
}

// Cache some or all uniform values so they can be restored
Expand Down
6 changes: 3 additions & 3 deletions src/scene/scene_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ const SceneLoader = {
const style = config.styles[sn];

if (style.shaders && style.shaders.uniforms) {
GLSL.parseUniforms(style.shaders.uniforms).forEach(({ type, value, key }) => {
GLSL.parseUniforms(style.shaders.uniforms).forEach(({ type, value, path }) => {
// Texture by URL (string-named texture not referencing existing texture definition)
if (type === 'sampler2D' && typeof value === 'string' && !config.textures[value]) {
const path = ['styles', sn, 'shaders', 'uniforms', key];
this.addTextureNode(path, bundle, texture_nodes);
const texture_path = ['styles', sn, 'shaders', 'uniforms', ...path];
this.addTextureNode(texture_path, bundle, texture_nodes);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/styles/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export var Style = {
return;
}

program.setUniforms(this.shaders && this.shaders.uniforms, true); // reset texture unit to 0
program.setUniforms(this.shaders?.uniforms, true); // reset texture unit to 0
},

// Render state settings by blend mode
Expand Down

0 comments on commit 13db98a

Please sign in to comment.