-
Creator 版本:2.3.2
-
目标平台: Chrome浏览器
-
重现方式:必现
-
首个报错:
-
编辑器操作系统: windows10
-
重现概率: 100%
准备做个2d的发现贴图配合点光源效果,但是无法使用struct结构定义uniform,不知道是什么问题
片段着色器里是这么定义struct的(关键代码)
uniform ARGS{
float shininess;
int lightCount;
PointLight pointLights;
};
传递参数方式如下:
materail.setProperty(“lightCount”, lightCount);
materail.setProperty(“pointLights.ambient”, new cc.Vec3(lightColor[index].r * 0.1,lightColor[index].g * 0.1, lightColor[index].b * 0.1));
materail.setProperty(“pointLights.diffuse”, lightColor[index]);
materail.setProperty(“pointLights.position”, lightPos[index]);
materail.setProperty(“pointLights.constant”, 1.0);
materail.setProperty(“pointLights.linear”, 0.9);
materail.setProperty(“pointLights.quadratic”, 0.032);
报错信息如下:
完整effect代码如下:
CCEffect %{
techniques:
- passes:
- vert: normalmap-2d-2-vs
frag: normalmap-2d-2-fs
blendState:
targets:- blend: true
rasterizerState:
cullMode: none
properties:
texture: { value: white }
pointLights : {}
}%
- blend: true
- vert: normalmap-2d-2-vs
CCProgram normalmap-2d-2-vs %{
precision highp float;
#include
#include
in vec3 a_position;
in vec4 a_color;
in vec2 a_uv0;
out vec2 v_uv0;
out vec4 v_color;
void main () {
vec4 pos = vec4(a_position, 1);
#if CC_USE_MODEL
pos = cc_matViewProj * cc_matWorld * pos;
#else
pos = cc_matViewProj * pos;
#endif
v_uv0 = a_uv0;
v_color = a_color;
gl_Position = pos;
}
}%
CCProgram normalmap-2d-2-fs %{
precision highp float;
uniform sampler2D texture;
uniform sampler2D textureNormal;
in vec2 v_uv0;
in vec4 v_color;
struct PointLight{
vec3 ambient;
vec3 diffuse;
vec3 position;
float constant;
float linear;
float quadratic;
};
uniform ARGS{
float shininess;
int lightCount;
PointLight pointLights;
};
vec3 CalcPointLight(vec3 textureColor, PointLight light, vec3 normal, vec3 fragPos)
{
vec3 lightDir = normalize(light.position - fragPos);
// 漫反射着色
float diff = max(dot(normal, lightDir), 0.0);
// 衰减
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance +
light.quadratic * (distance * distance));
// 合并结果
vec3 ambient = light.ambient * vec3(textureColor);
vec3 diffuse = light.diffuse * diff * vec3(textureColor);
ambient *= attenuation;
diffuse *= attenuation;
return (ambient + diffuse);
}
void main () {
vec4 color = texture2D(texture, v_uv0);
vec3 normalMap = texture2D(textureNormal, v_uv0).rgb;
vec3 normal = normalize(normalMap * 2.0 - 1.0);
vec3 fragPos = vec3(gl_FragCoord.xy, 0.0);
vec3 result= vec3(0.0, 0.0, 0.0);
// 点光源
// for(int i=0;i<1;i++){
// if(i >= lightCount){
// break;
// }
// result += CalcPointLight(color.rgb,, pointLights[i], normal, fragPos);
// }
result += CalcPointLight(color.rgb, pointLights, normal, fragPos);
gl_FragColor = vec4(result, color.a);
}
}%
