getUniformLocation return 空
getUniformLocation return null
我的片段着色器中有一个制服,当我尝试获取制服位置的值时,它 returns 为空。
我检查了拼写,没有发现任何拼写错误,着色器代码中也使用了这种制服。
我的错误是:
getUniformLocation([object WebGLProgram],materialAmbient)中的错误:统一'materialAmbient'在WebGLProgram(“未命名”)
这是我在 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);
}
`;
制服 materialAmbient
、ambientLight
和 specularLight
未在着色器程序中“使用”。它们不需要计算输出 outColor
(实际上 materialAmbient
是 ambientLight
用于计算 effectiveAmbient
。effectiveAmbient
,但未使用)。
编译器和链接器优化程序,去除不必要的计算和不必要的变量。因此,此变量变为非活动程序资源,您无法获取此变量的资源索引(统一位置)。
我的片段着色器中有一个制服,当我尝试获取制服位置的值时,它 returns 为空。
我检查了拼写,没有发现任何拼写错误,着色器代码中也使用了这种制服。
我的错误是:
getUniformLocation([object WebGLProgram],materialAmbient)中的错误:统一'materialAmbient'在WebGLProgram(“未命名”)
这是我在 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);
}
`;
制服 materialAmbient
、ambientLight
和 specularLight
未在着色器程序中“使用”。它们不需要计算输出 outColor
(实际上 materialAmbient
是 ambientLight
用于计算 effectiveAmbient
。effectiveAmbient
,但未使用)。
编译器和链接器优化程序,去除不必要的计算和不必要的变量。因此,此变量变为非活动程序资源,您无法获取此变量的资源索引(统一位置)。