three.js - Three.js Cubemap reflection shader rotation
问题描述
I'm trying to write my own reflection shader however the reflection does not work correctly when I try to rotate the sphere. It works only when my sphere is static. Here is my GLSL code:
sphereMaterial = new THREE.ShaderMaterial({
uniforms: {
uCubeMap: { value: cubeCamera.renderTarget.texture },
},
vertexShader: `
varying vec2 vUv;
varying vec3 vViewVector;
varying vec3 vNormal;
void main() {
vec4 modelPosition = modelMatrix * vec4(position, 1.0);
vec4 viewPosition = viewMatrix * modelPosition;
vec4 projectedPosition = projectionMatrix * viewPosition;
vViewVector = modelPosition.xyz - cameraPosition;
vNormal = normal;
gl_Position = projectedPosition;
}
`,
fragmentShader: `
uniform samplerCube uCubeMap;
varying vec3 vViewVector;
varying vec3 vNormal;
void main() {
vec3 reflectedDirection = normalize(reflect(vViewVector, vNormal));
reflectedDirection.x = -reflectedDirection.x;
vec3 textureColor = textureCube(uCubeMap, reflectedDirection).rgb;
gl_FragColor = vec4(textureColor, 1.0);
}
`,
});
Complete example: https://codepen.io/marianban/pen/VwPeYer?editors=0010 The sphere rotation is on the line 143 sphereGroup.rotation.x += 0.01;. It works fine if you comment it. It looks like that the reflection is not taking the sphere rotation into account.
I found several similar questions: GLSL cubemap reflection shader, Texturing Spheres with Cubemaps (not reflection maps) but unfortunately they didn't help me.
解决方案
推荐阅读
- linux - 为仅接受文件的 bash 命令提供环境变量
- postgresql - 使用 Trigger PostgreSQL 计算每一行的剩余余额并插入到表上的下一个 id 行
- ruby - Ruby 应用程序告诉我安装 2.5.1 但它已经安装
- android - EditText 的 setError() 在某些版本的 Lollipop 上崩溃
- bison - Bison:如果/否则减少/减少冲突
- cqrs - Axon 框架的排序策略如何在状态方面起作用
- opencv - 无法在 Hololens 上显示来自 openCV 的网络摄像头
- c - 如何在固定类型和浮动类型之间手动转换?
- maven - 使用 Maven 获取 axis-1.4 依赖项的完整列表
- c# - Asp.net core mvc ajax post到具有多个参数的控制器返回错误请求