From 7423a86a1bb7b87c70b5ccc3dbfb13ebb4f28668 Mon Sep 17 00:00:00 2001
From: Graham Linden <graham@lindenlab.com>
Date: Thu, 21 Mar 2019 10:39:14 -0700
Subject: [PATCH] SL-10751

Fix skydome VB generation to cover entire range of phi (give sky pants).

Add shader code to fade out clouds to simulate old look at altitude.
---
 .../shaders/class1/deferred/cloudsF.glsl        |  8 ++++----
 .../shaders/class1/deferred/cloudsV.glsl        |  5 ++++-
 .../shaders/class2/windlight/cloudsF.glsl       |  3 +++
 .../shaders/class2/windlight/cloudsV.glsl       |  5 +++++
 indra/newview/llvowlsky.cpp                     | 17 +++++++++++++----
 indra/newview/llvowlsky.h                       |  2 ++
 6 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl b/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
index 60ccfa64dbc..3fcb416601c 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
@@ -51,6 +51,7 @@ VARYING vec2 vary_texcoord0;
 VARYING vec2 vary_texcoord1;
 VARYING vec2 vary_texcoord2;
 VARYING vec2 vary_texcoord3;
+VARYING float altitude_blend_factor;
 
 /// Soft clips the light with a gamma correction
 vec3 scaleSoftClip(vec3 light);
@@ -102,10 +103,8 @@ void main()
     alpha1 = 1. - alpha1 * alpha1;
     alpha1 = 1. - alpha1 * alpha1;  
 
-    if (alpha1 < 0.001f)
-    {
-        discard;
-    }
+    alpha1 *= altitude_blend_factor;
+    alpha1 = clamp(alpha1, 0.0, 1.0);
 
     // Compute alpha2, for self shadowing effect
     // (1 - alpha2) will later be used as percentage of incoming sunlight
@@ -119,6 +118,7 @@ void main()
     // Combine
     vec4 color;
     color = (cloudColorSun*(1.-alpha2) + cloudColorAmbient);
+    color.rgb= max(vec3(0), color.rgb);
     color *= 2.;
 
     /// Gamma correct for WL (soft clip effect).
diff --git a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
index 4beb334f5a0..cfe5632ec08 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
@@ -41,6 +41,7 @@ VARYING vec2 vary_texcoord0;
 VARYING vec2 vary_texcoord1;
 VARYING vec2 vary_texcoord2;
 VARYING vec2 vary_texcoord3;
+VARYING float altitude_blend_factor;
 
 // Inputs
 uniform vec3 camPosLocal;
@@ -77,12 +78,15 @@ void main()
 	// Get relative position
 	vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 
+        altitude_blend_factor = (P.y > -4096.0) ? 1.0 : 1.0 - clamp(abs(P.y) / max_y, 0.0, 1.0);
+
 	// Set altitude
 	if (P.y > 0.)
 	{
 		P *= (max_y / P.y);
 	}
 	else
+        if (P.y <= 0.0)
 	{
 		P *= (-32000. / P.y);
 	}
@@ -122,7 +126,6 @@ void main()
 	// compiler gets confused.
 	temp1 = exp(-temp1 * temp2.z);
 
-
 	// Compute haze glow
 	temp2.x = dot(Pn, lightnorm.xyz);
 	temp2.x = 1. - temp2.x;
diff --git a/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
index 93024bf4e71..666ae84e5c1 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
@@ -52,6 +52,7 @@ VARYING vec2 vary_texcoord0;
 VARYING vec2 vary_texcoord1;
 VARYING vec2 vary_texcoord2;
 VARYING vec2 vary_texcoord3;
+VARYING float altitude_blend_factor;
 
 /// Soft clips the light with a gamma correction
 vec3 scaleSoftClip(vec3 light);
@@ -103,6 +104,8 @@ void main()
     alpha1 = 1. - alpha1 * alpha1;
     alpha1 = 1. - alpha1 * alpha1;  
 
+    alpha1 *= altitude_blend_factor;
+
     if (alpha1 < 0.001f)
     {
         discard;
diff --git a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
index fb978691dad..3cb69c33a1c 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
@@ -40,6 +40,7 @@ VARYING vec2 vary_texcoord0;
 VARYING vec2 vary_texcoord1;
 VARYING vec2 vary_texcoord2;
 VARYING vec2 vary_texcoord3;
+VARYING float altitude_blend_factor;
 
 // Inputs
 uniform vec3 camPosLocal;
@@ -76,6 +77,9 @@ void main()
     // Get relative position
     vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 
+    // fade clouds beyond a certain point so the bottom of the sky dome doesn't look silly at high altitude
+    altitude_blend_factor = (P.y > -4096.0) ? 1.0 : 1.0 - clamp(abs(P.y) / max_y, 0.0, 1.0);
+
     // Set altitude
     if (P.y > 0.)
     {
@@ -86,6 +90,7 @@ void main()
         P *= (-32000. / P.y);
     }
 
+
     // Can normalize then
     vec3 Pn = normalize(P);
     float  Plen = length(P);
diff --git a/indra/newview/llvowlsky.cpp b/indra/newview/llvowlsky.cpp
index 37689e06837..40cb0d2c242 100644
--- a/indra/newview/llvowlsky.cpp
+++ b/indra/newview/llvowlsky.cpp
@@ -111,7 +111,7 @@ inline F32 LLVOWLSky::calcPhi(U32 i)
 	t = t*t;
 	t = 1.f - t;
 
-	return F_PI_BY_TWO * t;
+	return F_PI * t;
 }
 
 void LLVOWLSky::resetVertexBuffers()
@@ -248,8 +248,11 @@ BOOL LLVOWLSky::updateGeometry(LLDrawable * drawable)
 				LL_ERRS() << "Failed updating WindLight sky geometry." << LL_ENDL;
 			}
 
+            U32 vertex_count = 0;
+            U32 index_count  = 0;
+
 			// fill it
-			buildStripsBuffer(begin_stack, end_stack,  vertices, texCoords, indices);
+			buildStripsBuffer(begin_stack, end_stack, vertex_count, index_count, vertices, texCoords, indices);
 
 			// and unlock the buffer
 			segment->flush();
@@ -360,7 +363,10 @@ void LLVOWLSky::initStars()
 	}
 }
 
-void LLVOWLSky::buildStripsBuffer(U32 begin_stack, U32 end_stack,
+void LLVOWLSky::buildStripsBuffer(U32 begin_stack,
+                                  U32 end_stack,
+                                  U32& vertex_count,
+                                  U32& index_count,
 								  LLStrider<LLVector3> & vertices,
 								  LLStrider<LLVector2> & texCoords,
 								  LLStrider<U16> & indices)
@@ -380,7 +386,7 @@ void LLVOWLSky::buildStripsBuffer(U32 begin_stack, U32 end_stack,
 	llassert(end_stack <= num_stacks);
 
 	// stacks are iterated one-indexed since phi(0) was handled by the fan above
-	for(i = begin_stack + 1; i <= end_stack+1; ++i) 
+	for(i = begin_stack; i <= end_stack; ++i) 
 	{
 		phi0 = calcPhi(i);
 
@@ -434,6 +440,9 @@ void LLVOWLSky::buildStripsBuffer(U32 begin_stack, U32 end_stack,
 		*indices++ = i * num_slices + k ;
 		count_indices++ ;
 	}
+
+    vertex_count = count_verts;
+    index_count = count_indices;
 }
 
 void LLVOWLSky::updateStarColors()
diff --git a/indra/newview/llvowlsky.h b/indra/newview/llvowlsky.h
index 73ecf1a7178..2b7ebe75dd7 100644
--- a/indra/newview/llvowlsky.h
+++ b/indra/newview/llvowlsky.h
@@ -66,6 +66,8 @@ class LLVOWLSky : public LLStaticViewerObject {
 	// begin_stack is the first stack to be included, end_stack is the first
 	// stack not to be included.
 	static void buildStripsBuffer(U32 begin_stack, U32 end_stack,
+                                  U32& vertex_count,
+                                  U32& index_count,
 								  LLStrider<LLVector3> & vertices,
 								  LLStrider<LLVector2> & texCoords,
 								  LLStrider<U16> & indices);
-- 
GitLab