New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating textures from arrays #14
Comments
gl-react not only supports textures as uniforms but also supports types like: int, float, vecN, matN, ... You can also send an array via uniforms.
Here is an example: const React = require("react");
const GL = require("gl-react");
const shaders = GL.Shaders.create({
arrayExample: {
frag: `
precision highp float;
varying vec2 uv;
uniform float array[6];
void main () {
gl_FragColor = vec4(
array[0] + array[1],
array[2] + array[3],
array[4] + array[5],
1.0);
}
`
}
});
class ArrayExample extends GL.Component {
render () {
const { width, height } = this.props;
return <GL.View
shader={shaders.arrayExample}
width={width}
height={height}
uniforms={{ array: [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] }}
/>;
}
}
module.exports = ArrayExample; |
Unfortunately, WebGL does not support dynamically indexing arrays. In my case (I am implementing a bilateral filter) I need to lookup values based on differences in values in the source image, which makes using an array not possible due to this limitation of WebGL. This is why I have been using a texture to store the data instead (which also gives the benefit of allowing for interpolation on the pre-calculated function). It sounds like the functionality I am looking for is not explicitly supported, so do you have any suggested way of hacking around it? Or a suggested way of adding this ability? It looks like both |
oh, so you want to use a texture for this. Sorry I missed your initial needs. You want to create a texture from your array. Now we just needs a special case for this if we want it to work in gl-react. In the meantime, as a workaround here is what you could do:
|
That worked great, thanks! Are there any plans to add this into |
that sounds like a great feature to add. |
I've got it implemented. A Use-case example: // var ndarray = require("ndarray");
var t1 = ndarray(new Float64Array([
1, 0, 0, 1,
0, 1, 0, 1,
0, 0, 1, 1
]), [3, 1, 4]); // here, using Float*Array & [ w, h, 4 ] shape will result of RGBA & FLOAT mode
var t2 = {
disableLinearInterpolation: true,
array: ndarray(new Float64Array([
1, 0, 0, 1,
0, 1, 0, 1,
0, 0, 1, 1
]), [3, 1, 4])
};
var t3 = require("baboon-image"); (all of these ndarray shapes are supported: https://github.com/stackgl/gl-texture2d#var-tex--createtexturegl-array , in your use-case you might just need the |
Awesome! This should be perfect.
Another feature I've realized I will be needing though is the ability to set the texture wrapping to |
gl.CLAMP_TO_EDGE will be here by default. |
will be in incoming 1.1.0 |
I have a shader which I'm trying to adapt to work with gl-react, but I've run into a problem. My shader uses a texture effectively as a gaussian function lookup table. In my previous implementation, the gaussian function was calculated beforehand as an array of values which was then used to create a GL texture to be used in the shader.
However, the only way I can see of creating textures using gl-react is by specifying an image source path. Is there any way of using an array of precomputed values instead? Or any sort of work around?
The text was updated successfully, but these errors were encountered: