1
0
Fork 0

Surface light effect and shaders for ALS

This commit is contained in:
Thorsten Renk 2014-02-25 10:49:37 +02:00
parent 638f5b7787
commit 1d25257604
3 changed files with 241 additions and 0 deletions

View file

@ -8,8 +8,105 @@
<wrap-s>clamp</wrap-s>
<wrap-t>clamp</wrap-t>
</texture>
<visibility><use>/environment/ground-visibility-m</use></visibility>
<avisibility><use>/environment/visibility-m</use></avisibility>
<lthickness><use>/environment/ground-haze-thickness-m</use></lthickness>
<eye_alt><use>/sim/rendering/eye-altitude-m</use></eye_alt>
</parameters>
<technique n="10">
<!-- ALS -->
<predicate>
<and>
<property>/sim/rendering/point-sprites</property>
<property>/sim/rendering/shaders/skydome</property>
<or>
<less-equal>
<value type="float">2.0</value>
<glversion/>
</less-equal>
<and>
<extension-supported>GL_ARB_point_sprite</extension-supported>
<extension-supported>GL_ARB_point_parameters</extension-supported>
<extension-supported>GL_ARB_shader_objects</extension-supported>
<extension-supported>GL_ARB_shading_language_100</extension-supported>
<extension-supported>GL_ARB_vertex_shader</extension-supported>
<extension-supported>GL_ARB_fragment_shader</extension-supported>
</and>
</or>
</and>
</predicate>
<pass>
<render-bin>
<bin-number>8</bin-number>
<bin-name>DepthSortedBin</bin-name>
</render-bin>
<lighting>false</lighting>
<blend>
<source>src-alpha</source>
<destination>one-minus-src-alpha</destination>
</blend>
<alpha-test>
<comparison>gequal</comparison>
<reference type="float">0.1</reference>
</alpha-test>
<cull-face><use>directional</use></cull-face>
<polygon-mode>
<front>point</front>
<back>point</back>
</polygon-mode>
<point>
<min-size><use>min-size</use></min-size>
<max-size><use>max-size</use></max-size>
<size><use>size</use></size>
<attenuation><use>attenuation</use></attenuation>
</point>
<texture-unit>
<unit>0</unit>
<point-sprite>true</point-sprite>
<type><use>texture[0]/type</use></type>
<wrap-s><use>texture[0]/wrap-s</use></wrap-s>
<wrap-t><use>texture[0]/wrap-t</use></wrap-t>
</texture-unit>
<program>
<vertex-shader>Shaders/surface-light-lightfield.vert</vertex-shader>
<fragment-shader>Shaders/surface-light-lightfield.frag</fragment-shader>
</program>
<uniform>
<name>size</name>
<type>float</type>
<value><use>size</use></value>
</uniform>
<uniform>
<name>visibility</name>
<type>float</type>
<value><use>visibility</use></value>
</uniform>
<uniform>
<name>avisibility</name>
<type>float</type>
<value><use>avisibility</use></value>
</uniform>
<uniform>
<name>hazeLayerAltitude</name>
<type>float</type>
<value><use>lthickness</use></value>
</uniform>
<uniform>
<name>eye_alt</name>
<type>float</type>
<value><use>eye_alt</use></value>
</uniform>
<uniform>
<name>texture</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
<vertex-program-point-size>true</vertex-program-point-size>
</pass>
</technique>
<technique n="17">
<!-- Combined technique -->
<predicate>

View file

@ -0,0 +1,120 @@
// -*-C++-*-
uniform sampler2D texture;
uniform float visibility;
uniform float avisibility;
uniform float hazeLayerAltitude;
uniform float eye_alt;
varying vec3 relPos;
varying float pixelSize;
float alt;
float fog_func (in float targ)
{
float fade_mix;
// for large altitude > 30 km, we switch to some component of quadratic distance fading to
// create the illusion of improved visibility range
targ = 1.25 * targ * smoothstep(0.04,0.06,targ); // need to sync with the distance to which terrain is drawn
if (alt < 30000.0)
{return exp(-targ - targ * targ * targ * targ);}
else if (alt < 50000.0)
{
fade_mix = (alt - 30000.0)/20000.0;
return fade_mix * exp(-targ*targ - pow(targ,4.0)) + (1.0 - fade_mix) * exp(-targ - pow(targ,4.0));
}
else
{
return exp(- targ * targ - pow(targ,4.0));
}
}
void main()
{
float dist = length(relPos);
float delta_z = hazeLayerAltitude - eye_alt;
float transmission;
float vAltitude;
float delta_zv;
float H;
float distance_in_layer;
float transmission_arg;
// angle with horizon
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
// we solve the geometry what part of the light path is attenuated normally and what is through the haze layer
if (delta_z > 0.0) // we're inside the layer
{
if (ct < 0.0) // we look down
{
distance_in_layer = dist;
vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
delta_zv = delta_z - vAltitude;
}
else // we may look through upper layer edge
{
H = dist * ct;
if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
else {distance_in_layer = dist;}
vAltitude = min(distance_in_layer,visibility) * ct;
delta_zv = delta_z - vAltitude;
}
}
else // we see the layer from above, delta_z < 0.0
{
H = dist * -ct;
if (H < (-delta_z)) // we don't see into the layer at all, aloft visibility is the only fading
{
distance_in_layer = 0.0;
delta_zv = 0.0;
}
else
{
vAltitude = H + delta_z;
distance_in_layer = vAltitude/H * dist;
vAltitude = min(distance_in_layer,visibility) * (-ct);
delta_zv = vAltitude;
}
}
// ground haze cannot be thinner than aloft visibility in the model,
// so we need to use aloft visibility otherwise
transmission_arg = (dist-distance_in_layer)/avisibility;
if (visibility < avisibility)
{
transmission_arg = transmission_arg + (distance_in_layer/visibility);
}
else
{
transmission_arg = transmission_arg + (distance_in_layer/avisibility);
}
transmission = fog_func(transmission_arg);
float dist_att = exp(-0.3/pixelSize);
vec4 texel = texture2D(texture,gl_TexCoord[0].st);
gl_FragColor = vec4 (gl_Color.rgb, texel.a * transmission * dist_att);
}

View file

@ -0,0 +1,24 @@
// -*-C++-*-
// Shader that uses OpenGL state values to do per-pixel lighting
uniform float size;
varying vec3 relPos;
varying float pixelSize;
void main()
{
gl_FrontColor= gl_Color;
gl_Position = ftransform();
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
relPos = gl_Vertex.xyz - ep.xyz;
float dist = length(relPos);
float lightScale = size * size * size * size * size / 1000.0;
pixelSize = min(size * size/25.0,lightScale/dist);
gl_PointSize = pixelSize;
}