瀏覽代碼

Shaders: added equirectUv() method

WestLangley 5 年之前
父節點
當前提交
0531b2ce34

+ 1 - 3
examples/webgl_materials_envmaps_parallax.html

@@ -175,9 +175,7 @@
 
 					#elif defined( ENVMAP_TYPE_EQUIREC )
 
-						vec2 sampleUV;
-						sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
-						sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
+						vec2 sampleUV = equirectUv( reflectVec );
 
 						#ifdef TEXTURE_LOD_EXT
 

+ 2 - 5
src/extras/PMREMGenerator.js

@@ -707,15 +707,12 @@ uniform vec2 texelSize;
 
 ${_getEncodings()}
 
-#define RECIPROCAL_PI 0.31830988618
-#define RECIPROCAL_PI2 0.15915494
+#include <common>
 
 void main() {
 	gl_FragColor = vec4(0.0);
 	vec3 outputDirection = normalize(vOutputDirection);
-	vec2 uv;
-	uv.y = asin(clamp(outputDirection.y, -1.0, 1.0)) * RECIPROCAL_PI + 0.5;
-	uv.x = atan(outputDirection.z, outputDirection.x) * RECIPROCAL_PI2 + 0.5;
+	vec2 uv = equirectUv( outputDirection );
 	vec2 f = fract(uv / texelSize - 0.5);
 	uv -= f * texelSize;
 	vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb;

+ 2 - 7
src/renderers/WebGLCubeRenderTarget.js

@@ -72,18 +72,13 @@ WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer
 
 			"varying vec3 vWorldDirection;",
 
-			"#define RECIPROCAL_PI 0.31830988618",
-			"#define RECIPROCAL_PI2 0.15915494",
+			"#include <common>",
 
 			"void main() {",
 
 			"	vec3 direction = normalize( vWorldDirection );",
 
-			"	vec2 sampleUV;",
-
-			"	sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;",
-
-			"	sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
+			"	vec2 sampleUV = equirectUv( direction );",
 
 			"	gl_FragColor = texture2D( tEquirect, sampleUV );",
 

+ 12 - 0
src/renderers/shaders/ShaderChunk/common.glsl.js

@@ -117,5 +117,17 @@ bool isPerspectiveMatrix( mat4 m ) {
 
   return m[ 2 ][ 3 ] == - 1.0;
 
+}
+
+vec2 equirectUv( in vec3 dir ) {
+
+	// dir is assumed to be unit length
+
+	float u = atan( dir.z, dir.x ) * RECIPROCAL_PI2 + 0.5;
+
+	float v = asin( clamp( dir.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
+
+	return vec2( u, v );
+
 }
 `;

+ 1 - 5
src/renderers/shaders/ShaderChunk/envmap_fragment.glsl.js

@@ -44,13 +44,9 @@ export default /* glsl */`
 
 	#elif defined( ENVMAP_TYPE_EQUIREC )
 
-		vec2 sampleUV;
-
 		reflectVec = normalize( reflectVec );
 
-		sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
-
-		sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
+		vec2 sampleUV = equirectUv( reflectVec );
 
 		vec4 envColor = texture2D( envMap, sampleUV );
 

+ 1 - 3
src/renderers/shaders/ShaderChunk/envmap_physical_pars_fragment.glsl.js

@@ -97,9 +97,7 @@ export default /* glsl */`
 
 		#elif defined( ENVMAP_TYPE_EQUIREC )
 
-			vec2 sampleUV;
-			sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
-			sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
+			vec2 sampleUV = equirectUv( reflectVec );
 
 			#ifdef TEXTURE_LOD_EXT
 

+ 1 - 5
src/renderers/shaders/ShaderLib/equirect_frag.glsl.js

@@ -9,11 +9,7 @@ void main() {
 
 	vec3 direction = normalize( vWorldDirection );
 
-	vec2 sampleUV;
-
-	sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
-
-	sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;
+	vec2 sampleUV = equirectUv( direction );
 
 	vec4 texColor = texture2D( tEquirect, sampleUV );