| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
// Shader that takes a list of GL_POINTS and draws a light (point-sprite like |
| 5 |
// texture, more accurately a light halo) at the given point. This shader |
| 6 |
// provides support for light animations like blinking, time period handling |
| 7 |
// for lights on only during night time or in low visiblity and directional |
| 8 |
// lighting. |
| 9 |
// |
| 10 |
// The actual rendering code is inspired from an existing implementation |
| 11 |
// found at: |
| 12 |
// FGData commit 9355d464c175bd5d51ba32527180ed4e94e86fbb |
| 13 |
// Shaders/surface-lights-ALS.vert |
| 14 |
// with major changes. |
| 15 |
// |
| 16 |
// Licence: GPL v2+ |
| 17 |
// Written by Fahim Dalvi, January 2021 |
| 18 |
|
| 19 |
attribute vec3 lightParams; |
| 20 |
attribute vec4 animationParams; |
| 21 |
attribute vec3 directionParams1; |
| 22 |
attribute vec2 directionParams2; |
| 23 |
uniform float osg_SimulationTime; |
| 24 |
|
| 25 |
uniform float avisibility; |
| 26 |
uniform float sun_angle; |
| 27 |
uniform float fov; |
| 28 |
|
| 29 |
varying vec3 relativePosition; |
| 30 |
varying vec2 rawPosition; |
| 31 |
|
| 32 |
varying float apparentSize; |
| 33 |
varying float haloSize; |
| 34 |
varying float lightSize; |
| 35 |
varying float lightIntensity; |
| 36 |
|
| 37 |
const float epsilon = 1e-7; |
| 38 |
|
| 39 |
// rand2D sourced from noise.frag, since *.vert files |
| 40 |
// cannot access functions defined in *.frag files |
| 41 |
// Git commit: b8ddf517f4495219da7675d81bed59a378e2d78a |
| 42 |
// File: fgdata/Shaders/noise.frag |
| 43 |
float rand2D(in vec2 co){ |
| 44 |
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); |
| 45 |
} |
| 46 |
|
| 47 |
void main() |
| 48 |
{ |
| 49 |
/***************************** Initialization ****************************/ |
| 50 |
float random = rand2D(vec2(gl_Vertex.x, gl_Vertex.yz)) * 10; |
| 51 |
float random_1 = floor(random); // random_1 can take 10 values |
| 52 |
float random_2 = fract(random); // random_2 takes the remaining random bits |
| 53 |
|
| 54 |
/*************** Night and low visibility lights handling ****************/ |
| 55 |
int on_period = int(lightParams.z + 0.5); // round() not supported by glsl 1.2 |
| 56 |
const float sun_angle_min = 1.57; |
| 57 |
const float sun_angle_max = 1.61; |
| 58 |
float target_sun_angle = sun_angle_min + random_1/10 * (sun_angle_max - sun_angle_min); |
| 59 |
if (on_period == 1 && sun_angle < sun_angle_min) { |
| 60 |
// Lights will switch on exactly at ~89 degree sun angle |
| 61 |
gl_Position = vec4(0.0,0.0,10.0,1.0); |
| 62 |
gl_FrontColor.a = 0.0; |
| 63 |
return; |
| 64 |
} else if (on_period == 2 && sun_angle < target_sun_angle) { |
| 65 |
// Lights will switch on randomly between 90 and 92 degree sun angle |
| 66 |
// corresponding to a ~10 minute period around sunset |
| 67 |
gl_Position = vec4(0.0,0.0,10.0,1.0); |
| 68 |
gl_FrontColor.a = 0.0; |
| 69 |
return; |
| 70 |
} else if (on_period == 3 && (sun_angle < sun_angle_min && avisibility > 5000)) { |
| 71 |
// Lights will switch on exactly at ~89 degree sun angle or when visibility |
| 72 |
// is less than 5000m |
| 73 |
gl_Position = vec4(0.0,0.0,10.0,1.0); |
| 74 |
gl_FrontColor.a = 0.0; |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
/****************************** Animations *******************************/ |
| 79 |
float interval = animationParams.x; |
| 80 |
if (interval > 0) { |
| 81 |
float on_portion = animationParams.y; |
| 82 |
float strobe_rate = animationParams.z; |
| 83 |
float offset = animationParams.w; |
| 84 |
|
| 85 |
// Randomize offset if its less than 0 |
| 86 |
if (offset < 0) { |
| 87 |
// rand2D returns a value from 0 to 1, multiplying it with |
| 88 |
// the interval chooses an offset within the entire animation |
| 89 |
// window |
| 90 |
offset = random_2 * interval; |
| 91 |
} |
| 92 |
|
| 93 |
float strobe_interval = interval/strobe_rate; |
| 94 |
float interval_fraction = mod(osg_SimulationTime + offset, interval)/interval; |
| 95 |
float strobe_fraction = mod(osg_SimulationTime + offset, strobe_interval)/strobe_interval; |
| 96 |
|
| 97 |
if (interval_fraction > on_portion || (strobe_fraction < 0.5 && strobe_rate > 0.0000001)) { |
| 98 |
gl_Position = vec4(0.0,0.0,10.0,1.0); |
| 99 |
gl_FrontColor.a = 0.0; |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/***************************** Light visuals *****************************/ |
| 105 |
gl_FrontColor = gl_Color; |
| 106 |
gl_Position = ftransform(); |
| 107 |
|
| 108 |
vec4 eyePosition = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0); |
| 109 |
relativePosition = gl_Vertex.xyz - eyePosition.xyz; |
| 110 |
rawPosition = gl_Vertex.xy; |
| 111 |
float dist = length(relativePosition); |
| 112 |
float angularAttenuationFactor = 1.0; |
| 113 |
|
| 114 |
/************************** Direction handling ***************************/ |
| 115 |
if (directionParams2.x < 359.999999 || directionParams2.y < 359.999999) { |
| 116 |
vec3 eyeVector = normalize(-relativePosition); |
| 117 |
vec3 lightNormal = normalize(directionParams1); |
| 118 |
vec3 upVec = normalize(vec3(0,0,1)); |
| 119 |
|
| 120 |
vec3 horizontalVector, verticalVector; |
| 121 |
|
| 122 |
if (abs(dot(lightNormal, upVec)) > (1 - epsilon)) { |
| 123 |
// Light direction is directly up or down |
| 124 |
horizontalVector = normalize(vec3(1, 0, 0)); |
| 125 |
verticalVector = normalize(vec3(0, 1, 0)); |
| 126 |
} else { |
| 127 |
horizontalVector = normalize(cross(lightNormal, upVec)); |
| 128 |
verticalVector = normalize(cross(lightNormal, horizontalVector)); |
| 129 |
} |
| 130 |
|
| 131 |
vec3 projectionOnHorizontal = lightNormal; |
| 132 |
vec3 projectionOnVertical = lightNormal; |
| 133 |
|
| 134 |
if (dot(lightNormal, eyeVector) < (-1 + epsilon)) { |
| 135 |
// If the view direction is directly opposite to the light normal |
| 136 |
projectionOnHorizontal = eyeVector; |
| 137 |
projectionOnVertical = eyeVector; |
| 138 |
} else { |
| 139 |
// If the view vector is not perpendicular to the horizontal axis |
| 140 |
if (abs(dot(horizontalVector, eyeVector)) > (0 + epsilon)) { |
| 141 |
projectionOnHorizontal = normalize(eyeVector - dot(verticalVector, eyeVector) * verticalVector); |
| 142 |
} |
| 143 |
|
| 144 |
// If the view vector is not perpendicular to the vertical axis |
| 145 |
if (abs(dot(verticalVector, eyeVector)) > (0 + epsilon)) { |
| 146 |
projectionOnVertical = normalize(eyeVector - dot(horizontalVector, eyeVector) * horizontalVector); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
float horizontalAngle = dot(projectionOnHorizontal, lightNormal); |
| 151 |
float verticalAngle = dot(projectionOnVertical, lightNormal); |
| 152 |
|
| 153 |
float minHoriz = cos(radians(directionParams2.x * 0.5)); |
| 154 |
float minVert = cos(radians(directionParams2.y * 0.5)); |
| 155 |
|
| 156 |
// Light is 0 intensity below [specified angle] |
| 157 |
// Increases softmax-ly between [specified angle] and [1/2 of difference of specified angle and 0] (head on viewing) |
| 158 |
// Light is 1 intensity after [1/2 of difference of specified angle and 0] to [0 degrees] |
| 159 |
// Note: difference of angles is computed linearly after applying the cosine function, but it works well enough as an approximation |
| 160 |
horizontalAngle = smoothstep(minHoriz, minHoriz + (1 - minHoriz)/2.0, horizontalAngle); |
| 161 |
verticalAngle = smoothstep(minVert, minVert + (1 - minVert)/2.0, verticalAngle); |
| 162 |
angularAttenuationFactor = horizontalAngle*verticalAngle; |
| 163 |
|
| 164 |
// Debug animation code |
| 165 |
// float ra = mod(osg_SimulationTime*30, 20); |
| 166 |
// gl_FrontColor = vec4(verticalAngle, 0, 0, 1); |
| 167 |
// if (ra < 10) { |
| 168 |
// gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + (ra)/2 * normalize(vec4(directionParams1, 0))); |
| 169 |
// gl_FrontColor = vec4(1.0, 0.0, 0.0, 1.0); |
| 170 |
// } else if (ra < 20) { |
| 171 |
// // gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + (ra-15) * normalize(vec4(proj_on_horizontal, 0))); |
| 172 |
// // gl_FrontColor = vec4(0.0, 0.0, 1.0, 1.0); |
| 173 |
// } else if (ra < 30) { |
| 174 |
// // gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + (ra-25) * normalize(vec4(vertical_vec, 0))); |
| 175 |
// gl_FrontColor = vec4(0.0, 1.0, 0.0, 1.0); |
| 176 |
// } else if (ra < 40) { |
| 177 |
// gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + (ra-35) * normalize(vec4(horizontal_vec, 0))); |
| 178 |
// gl_FrontColor = vec4(0.0, 1.0, 1.0, 1.0); |
| 179 |
// } else { |
| 180 |
// gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + (ra-45) * normalize(vec4(proj_on_vertical, 0))); |
| 181 |
// gl_FrontColor = vec4(0.0, 0.0, 1.0, 1.0); |
| 182 |
// } |
| 183 |
} |
| 184 |
|
| 185 |
lightSize = lightParams.x; |
| 186 |
lightIntensity = lightParams.y; |
| 187 |
|
| 188 |
/******** |
| 189 |
* Each light is made up of a base circle, a circular-ish halo around the base and a bunch of |
| 190 |
* star-like rays |
| 191 |
* baseLightSize is tuned using reference objects of sizes 10cm, 50cm, 100cm, 500cm and 1000cm |
| 192 |
* under the assumption that the "bright center" part of the light will be the approximately the |
| 193 |
* same size as the light itself |
| 194 |
*/ |
| 195 |
float baseLightSize = lightSize / (dist/80) * 60/fov; |
| 196 |
|
| 197 |
|
| 198 |
/******** |
| 199 |
* Decide how big the halo + star like structure can get relative to the actual light size |
| 200 |
* This has been done by fitting various curve (using random parameter search to fit the |
| 201 |
* following data): |
| 202 |
|
| 203 |
dist/intensity -> haloSize |
| 204 |
0/2070 -> 2 |
| 205 |
0/15000 -> 2 |
| 206 |
1900/2070 -> 10 |
| 207 |
33250/15000 -> 30 |
| 208 |
|
| 209 |
* The real world distance to dist mapping is around the following: |
| 210 |
1nm = 1930 |
| 211 |
2nm = 3780 |
| 212 |
3nm = 5630 |
| 213 |
4nm = 7480 |
| 214 |
5nm = 9330 |
| 215 |
*/ |
| 216 |
|
| 217 |
// Various fits that are better at different "zones" of intensity/distance combinations |
| 218 |
// haloSize = 1 + log(1 + 8.207628166987313e-05 + pow(0.00935009645108105 * dist, 0.7229519420159332)) * log(1 + 0.008611494896181404 + pow(9.987873482714503e-08 * lightIntensity, 0.5460367551326879)) * 188.78730222257022; |
| 219 |
// haloSize = 1 + log(1 + pow(0.00344640493737296 * dist, 43.666719413543746)) * log(1 + pow(6.174965900415324e-07 * lightIntensity, 0.1915282228627938)) * log(1 + pow(48.81816078788492 * dist * lightIntensity, 0.39087987152530046)) * 0.03982200390091456; |
| 220 |
// haloSize = 1 + 1 + log(1 + pow(0.003022850828231838 * dist, 81.88510919372303)) * log(1 + pow(2.8538041872684384e-05 * lightIntensity, 0.2798979878622515)) * log(1 + pow(6.125105317094489 * dist * lightIntensity, 9.486990540357818e-06)) * 0.17726981739920522; |
| 221 |
// haloSize = 1 + (log(1 + 0.0009319617220954881 * dist) * log(1 + 0.3853503865089568 * lightIntensity)) * 0.8677850896527736; |
| 222 |
haloSize = 1 + (log(1 + 0.0030356535475020265 * dist) * log(1 + 0.00964994652970935 * lightIntensity)) * 1.1927528593388748; |
| 223 |
apparentSize = baseLightSize * haloSize * angularAttenuationFactor; |
| 224 |
|
| 225 |
gl_PointSize = apparentSize; |
| 226 |
} |