فهرست منبع

Merge pull request #23186 from BastiaanOlij/fix_gles2_stereo_sky

Fixed stereoscopic (VR) sky in GLES2
Rémi Verschelde 6 سال پیش
والد
کامیت
cfa8ef1f77
2فایلهای تغییر یافته به همراه32 افزوده شده و 1 حذف شده
  1. 10 1
      drivers/gles2/rasterizer_scene_gles2.cpp
  2. 22 0
      drivers/gles2/shaders/copy.glsl

+ 10 - 1
drivers/gles2/rasterizer_scene_gles2.cpp

@@ -2455,13 +2455,20 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C
 	glEnableVertexAttribArray(VS::ARRAY_VERTEX);
 	glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
 
+	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, asymmetrical);
+	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, !asymmetrical);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, true);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
-	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, true);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false);
 	storage->shaders.copy.bind();
 	storage->shaders.copy.set_uniform(CopyShaderGLES2::MULTIPLIER, p_energy);
+	if (asymmetrical) {
+		// pack the bits we need from our projection matrix
+		storage->shaders.copy.set_uniform(CopyShaderGLES2::ASYM_PROJ, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1]);
+		///@TODO I couldn't get mat3 + p_transform.basis to work, that would be better here.
+		storage->shaders.copy.set_uniform(CopyShaderGLES2::PANO_TRANSFORM, p_transform);
+	}
 
 	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
@@ -2469,6 +2476,8 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C
 	glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
 	glBindBuffer(GL_ARRAY_BUFFER, 0);
 
+	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, false);
+	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false);
 	storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
 }

+ 22 - 0
drivers/gles2/shaders/copy.glsl

@@ -35,6 +35,8 @@ void main() {
 
 #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
 	cube_interp = cube_in;
+#elif defined(USE_ASYM_PANO)
+	uv_interp = vertex_attrib.xy;
 #else
 	uv_interp = uv_in;
 #endif
@@ -68,6 +70,11 @@ varying vec2 uv_interp;
 #endif
 /* clang-format on */
 
+#ifdef USE_ASYM_PANO
+uniform highp mat4 pano_transform;
+uniform highp vec4 asym_proj;
+#endif
+
 #ifdef USE_CUBEMAP
 uniform samplerCube source_cube; // texunit:0
 #else
@@ -108,6 +115,21 @@ void main() {
 
 	vec4 color = texturePanorama(source, normalize(cube_interp));
 
+#elif defined(USE_ASYM_PANO)
+
+	// When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result.
+	// Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1.
+	// The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image.
+
+	vec3 cube_normal;
+	cube_normal.z = -1000000.0;
+	cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y;
+	cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a;
+	cube_normal = mat3(pano_transform) * cube_normal;
+	cube_normal.z = -cube_normal.z;
+
+	vec4 color = texturePanorama(source, normalize(cube_normal.xyz));
+
 #elif defined(USE_CUBEMAP)
 	vec4 color = textureCube(source_cube, normalize(cube_interp));
 #else