gpt4 book ai didi

opengl-es - getUniformLocation 返回空

转载 作者:行者123 更新时间:2023-12-02 02:09:04 25 4
gpt4 key购买 nike

我的片段着色器中有一个制服,当我尝试获取制服位置的值时,它返回 null。

我检查了拼写,没有发现任何拼写错误,着色器代码中也使用了这种制服。

我的错误是:

getUniformLocation([object WebGLProgram], materialAmbient) 中的错误:WebGLProgram("unnamed") 中不存在统一的“materialAmbient”

enter image description here

这是我在 WebGL 中获取统一位置的代码。

let materialAmbientLocation = gl.getUniformLocation(programScene,"materialAmbient");

let materialDiffuseLocation = gl.getUniformLocation(programScene,"materialDiffuse");

let materialSpecularLocation = gl.getUniformLocation(programScene,"materialSpecular");

let shininessLocation = gl.getUniformLocation(programScene, "shininess");

let ambiemtLightLocation = gl.getUniformLocation(programScene, "ambientLight");

let diffuseLightLocation = gl.getUniformLocation(programScene, "diffuseLight");

let specularLightLocation = gl.getUniformLocation(programScene,"specularLight");

我的片段着色器:

我的 glsl

const fsSkybox = `#version 300 es

precision highp float;

in vec3 texPosition;
in vec3 v_normal;
in vec3 f_position;

out vec4 outColor;

// uniform samplerCube u_SkyTexture;

uniform vec3 materialAmbient;

uniform vec3 emission;

uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform float shininess;

uniform vec3 lightDirection;
uniform vec3 cameraPosition;

uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;

void main(){

vec3 f_normal = normalize(v_normal);

vec3 effectiveAmbient = ambientLight*materialAmbient;

vec3 f_lightDirection = normalize(lightDirection);

float lambertCofficient = max(dot(f_lightDirection, f_normal), 0.0);
vec3 effectiveDiffuse = lambertCofficient*materialDiffuse*diffuseLight;

vec3 surfacetoView = normalize(cameraPosition - f_position);
vec3 halfVector = lightDirection + surfacetoView;
float specular = dot(halfVector, f_normal);
vec3 effectiveSpecular = materialSpecular*pow(specular, shininess);

outColor = vec4(emission + effectiveDiffuse + effectiveSpecular, 1.0);

// outColor = texture(u_SkyTexture, texPosition);
// outColor = vec4(0.0, 1.0, 0.0, 1.0);
}
`;

最佳答案

制服 materialAmbientambientLightspecularLight 未在着色器程序中“使用”。它们不需要计算输出 outColor(实际上 materialAmbientambientLight 用于计算 effectiveAmbient effectiveAmbient,但未使用)。
编译器和链接器优化程序,去除不必要的计算和不必要的变量。因此,此变量成为非事件程序资源,您无法获取此变量的资源索引(统一位置)。

关于opengl-es - getUniformLocation 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67955038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com