Webgl-如何使镜面反射光不改变大小
我试图实现镜面照明(从前面来),但灯光总是以不自然的方式改变大小。我该如何解决这个问题? 我硬编码viewerPos进行测试。我使用的是一个中间向量“快捷方式”,因此我必须计算更少的东西,如下所述: 实施“我的灯光”的视频:Webgl-如何使镜面反射光不改变大小,webgl,fragment-shader,lighting,specular,Webgl,Fragment Shader,Lighting,Specular,我试图实现镜面照明(从前面来),但灯光总是以不自然的方式改变大小。我该如何解决这个问题? 我硬编码viewerPos进行测试。我使用的是一个中间向量“快捷方式”,因此我必须计算更少的东西,如下所述: 实施“我的灯光”的视频: 如果不看得太深,这似乎是错误的surfaceToViewDirection=(vPos-viewerPos)并且可能应该是surfaceToViewDirection=normalize(vPos-viewerPos)?是的,你是对的,这让我滑倒了谢谢:)。不幸的是,这对我
如果不看得太深,这似乎是错误的
surfaceToViewDirection=(vPos-viewerPos)代码>并且可能应该是surfaceToViewDirection=normalize(vPos-viewerPos)代码>?是的,你是对的,这让我滑倒了谢谢:)。不幸的是,这对我的bug没有帮助。我会继续尝试,不要看得太深这似乎是错误的surfaceToViewDirection=(vPos-viewerPos)代码>并且可能应该是surfaceToViewDirection=normalize(vPos-viewerPos)代码>?是的,你是对的,这让我滑倒了谢谢:)。不幸的是,这对我的bug没有帮助。我会继续努力的
// Vertex shader program
const vsSource = `
attribute vec4 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uNormalMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform highp vec3 uViewPos;
varying highp vec2 vTextureCoord;
varying highp vec4 vNormal;
varying highp mat4 vModelViewMatrix;
varying highp vec3 vPos;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord; //Textura
vModelViewMatrix = uModelViewMatrix;
vPos = (uModelViewMatrix * aVertexPosition).xyz;
vNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
}
`;
// Fragment shader program
const fsSource = `
varying highp vec2 vTextureCoord;
varying highp vec4 vNormal;
varying highp mat4 vModelViewMatrix;
varying highp vec3 vPos;
uniform sampler2D uSampler;
void main(void) {
// Apply lighting effect
highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
//Luz Ambiente
highp vec3 ambientLight = 0.3 * vec3(1.0, 1.0, 1.0);
//Luz Difusa
highp vec3 directionalLightColor = vec3(1, 1, 1);
highp vec3 directionalVector = vec3(0.0, 0.0, 1.0);
highp float directional = max(dot(vNormal.xyz, normalize(directionalVector)), 0.0);
//Luz Especular
highp vec3 viewerPos = vec3(0, 0, -6); //NOTA: PASSAR PARA SHADERS, NAO DAR HARDCODE
highp vec3 surfaceToLightDirection = (-1.0 * directionalVector);
highp vec3 surfaceToViewDirection = (vPos - viewerPos);
highp vec3 halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection);
highp float specular = max(dot(vNormal.xyz, halfVector), 0.0);
highp vec3 vLighting = ambientLight;// + (directionalLightColor * directional);
gl_FragColor = vec4(texelColor.rgb * vLighting + (specular * 0.5), texelColor.a);
}
`;