将结构体数组的值从 JS 设置为 GLSL
我一直在尝试创建一个包含 WebGL 应用程序所有灯光的结构,但在从 JS 设置它的值时遇到了麻烦。结构如下:
struct Light {
vec4 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 spotDirection;
float spotCutOff;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float spotExponent;
float spotLightCosCutOff;
};
uniform Light lights[numLights];
在测试了很多东西之后,我让它工作了,但我对我编写的代码不满意:
program.uniform.lights = [];
program.uniform.lights.push({
position: "",
diffuse: "",
specular: "",
ambient: "",
spotDirection: "",
spotCutOff: "",
constantAttenuation: "",
linearAttenuation: "",
quadraticAttenuation: "",
spotExponent: "",
spotLightCosCutOff: ""
});
program.uniform.lights[0].position = gl.getUniformLocation(program, "lights[0].position");
program.uniform.lights[0].diffuse = gl.getUniformLocation(program, "lights[0].diffuse");
program.uniform.lights[0].specular = gl.getUniformLocation(program, "lights[0].specular");
program.uniform.lights[0].ambient = gl.getUniformLocation(program, "lights[0].ambient");
... and so on
我很抱歉让你看这个代码,我知道它很糟糕,但我找不到更好的方法。
是否有正确执行此操作的标准或推荐方法?谁能启发我吗?
I've been trying to make a structure that will contain all the lights of my WebGL app, and I'm having troubles setting up it's values from JS. The structure is as follows:
struct Light {
vec4 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 spotDirection;
float spotCutOff;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float spotExponent;
float spotLightCosCutOff;
};
uniform Light lights[numLights];
After testing LOTS of things I made it work but I'm not happy with the code I wrote:
program.uniform.lights = [];
program.uniform.lights.push({
position: "",
diffuse: "",
specular: "",
ambient: "",
spotDirection: "",
spotCutOff: "",
constantAttenuation: "",
linearAttenuation: "",
quadraticAttenuation: "",
spotExponent: "",
spotLightCosCutOff: ""
});
program.uniform.lights[0].position = gl.getUniformLocation(program, "lights[0].position");
program.uniform.lights[0].diffuse = gl.getUniformLocation(program, "lights[0].diffuse");
program.uniform.lights[0].specular = gl.getUniformLocation(program, "lights[0].specular");
program.uniform.lights[0].ambient = gl.getUniformLocation(program, "lights[0].ambient");
... and so on
I'm sorry for making you look at this code, I know it's horrible but I can't find a better way.
Is there a standard or recommended way of doing this properly? Can anyone enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做对了。您可以尝试将其收紧一点,如下所示
You're doing it right. You could try to tighten it up a bit as in