[英]How can I pass along an array of floats as a uniform to shaders from Three JS
To clarify I am using Three JS, React-Three-Fiber, React为了澄清我正在使用三个 JS,React-Three-Fiber,React
I have disc with points mapped on it, I have calculated the distance of every point to the center of the desk and stored results in an array.我有一个上面有点的圆盘,我计算了每个点到桌子中心的距离,并将结果存储在一个数组中。 I would like to pass this array to the shaders but I am struggling to understand how to do it.
我想将这个数组传递给着色器,但我很难理解如何去做。 I read in the docs you can also pass along
vec3,mat3,float
essentially.我在文档中读到你也可以传递
vec3,mat3,float
本质上。 My array has a length of 1000 so I cannot use it with any of the data types uniforms can be used with, if I store the array as a define
how can I go about using it in the shader file?我的数组的长度为 1000,所以我不能将它用于制服可以使用的任何数据类型,如果我将数组存储为
define
,我 go 如何在着色器文件中使用它?
uniforms制服
const uniforms = useMemo(() => ({
uTime: {
value: 0.0
},
uRadius: {
value: radius
},
uDistance: {
value: normalDistances
}
}), [])
fragment shader片段着色器
const fragmentShader = `
uniform vec3 uDistance;
varying float vDistance;
void main() {
float x = uDistance[52];
vec3 color = vec3(0.5, 0.5, x);
gl_FragColor = vec4(color, 1.0);
}
`
export default fragmentShader
I cannot import a uniform without specifying the data type, and I cannot find how to import a define which was passed along from javascript. Does anyone know what my issue is?我无法在不指定数据类型的情况下导入制服,而且我找不到如何导入从 javascript 传递的定义。有人知道我的问题是什么吗? Thanks!
谢谢!
uniform vec3 uDistance;
This is no valid declaration of a uniform array.这不是统一数组的有效声明。 You always have to define the length like so:
你总是必须像这样定义长度:
uniform vec3 uDistance[8];
However, keep in mind that uniform arrays require a uniform vector for each entry.但是,请记住,uniform arrays 要求每个条目都有一个 uniform 向量。 If you want to use a length of 1000, you easily hit the maximum supported uniform vectors on devices.
如果你想使用 1000 的长度,你很容易达到设备支持的最大统一向量。
I suggest you encode your distance data into a texture and then sample it in your shader.我建议您将距离数据编码为纹理,然后在着色器中对其进行采样。 In
three.js
, you can use theDataTexture class for this.在
three.js
中,您可以为此使用DataTexture class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.