webgl - Webgl - 如何使镜面光不改变大小
问题描述
我正在尝试实现镜面光照(来自前面),但光线总是以不自然的方式改变大小。我该如何解决?我硬编码 viewerPos 来测试。我使用的是中途矢量“快捷方式”,所以我必须计算更少的东西,如下所述:https ://webglfundamentals.org/webgl/lessons/webgl-3d-lighting-point.html
实现了我的照明的视频:https ://streamable.com/j95bz7
// 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);
}
`;
解决方案
推荐阅读
- apache-spark - Spark 连接到独立集群上的从属设备
- python - 如何使用python替换多个svg文件中的文本
- java - 在 Main Activity 的 on create 方法中执行的计算未执行
- javascript - 导航栏类型错误:无法读取 null 的属性“classList”
- python - PySpark: py4j.protocol.Py4JJavaError: 调用 o215.save 时出错
- e2e-testing - Playwright 测试库 - 选择器的父元素
- python - 您可以将变量传递给python的导入函数吗?
- visual-studio-code - VS 代码片段转换
- emacs - 如何在 org 模式下为 noweb 引用创建超链接
- css - 标题元素对齐