Debian Codegraph @

avogadro/rendering/volume_fs.glsl

raw
1 #version 400
2 precision highp float;
3
4 // The ray entry/exit attachments from bounding-box passes:
5 uniform sampler2D inFrontPosTex;
6 uniform sampler2D inBackPosTex;
7 uniform vec3 positiveColor;
8 uniform vec3 negativeColor;
9
10 // Our 3D volume (wavefunction):
11 uniform sampler3D uVolumeData;
12
13 // Cube bounds in world space:
14 uniform vec3 uBoxMin;
15 uniform vec3 uBoxMax;
16
17 // Camera position for ray computation when back faces are clipped
18 uniform vec3 uCameraPos;
19
20 // View direction for orthographic projection (all rays parallel)
21 uniform vec3 uViewDir;
22
23 // Projection type: 0 = perspective, 1 = orthographic
24 uniform int uProjectionType;
25
26 // Screen size:
27 uniform float width;
28 uniform float height;
29
30 // Ray-march parameters:
31 uniform int numSteps; // e.g. 256
32 uniform float alphaScale; // overall alpha multiplier
33
34 out vec4 colorOut;
35
36 // Ray-box intersection: returns exit t value for ray origin + t * dir
37 float rayBoxExit(vec3 origin, vec3 dir, vec3 boxMin, vec3 boxMax)
38 {
39 vec3 invDir = 1.0 / dir;
40 vec3 t1 = (boxMin - origin) * invDir;
41 vec3 t2 = (boxMax - origin) * invDir;
42 vec3 tMax = max(t1, t2);
43 return min(min(tMax.x, tMax.y), tMax.z);
44 }
45
46 // Ray-box intersection: returns entry t value for ray origin + t * dir
47 float rayBoxEntry(vec3 origin, vec3 dir, vec3 boxMin, vec3 boxMax)
48 {
49 vec3 invDir = 1.0 / dir;
50 vec3 t1 = (boxMin - origin) * invDir;
51 vec3 t2 = (boxMax - origin) * invDir;
52 vec3 tMin = min(t1, t2);
53 return max(max(tMin.x, tMin.y), tMin.z);
54 }
55
56 void main()
57 {
58 vec2 UV = gl_FragCoord.xy / vec2(width, height);
59
60 vec3 entryPos = texture(inFrontPosTex, UV).xyz;
61 vec3 exitPos = texture(inBackPosTex, UV).xyz;
62
63 // Get the expected diagonal length of the box
64 vec3 boxSize = uBoxMax - uBoxMin;
65 float boxDiagonal = length(boxSize);
66
67 // Check for sentinel values (positions set to -1e6 when no hit)
68 bool entryIsSentinel = entryPos.x < -1e5;
69 bool exitIsSentinel = exitPos.x < -1e5;
70
71 // Compute ray direction
72 vec3 rayDir;
73 if (uProjectionType == 1) {
74 // Orthographic: all rays are parallel to view direction
75 rayDir = uViewDir;
76 } else {
77 // Perspective: rays diverge from camera position
78 if (!entryIsSentinel) {
79 rayDir = normalize(entryPos - uCameraPos);
80 } else if (!exitIsSentinel) {
81 rayDir = normalize(exitPos - uCameraPos);
82 } else {
83 // Both sentinel - no valid ray
84 discard;
85 }
86 }
87
88 // When either position is sentinel (clipped), compute analytically
89 if (entryIsSentinel || exitIsSentinel) {
90 if (uProjectionType == 1) {
91 // Orthographic: use the valid position as reference point
92 vec3 refPoint;
93 if (!entryIsSentinel) {
94 refPoint = entryPos;
95 } else {
96 refPoint = exitPos;
97 }
98 // Trace ray through reference point to find both entry and exit
99 // Move reference point back along ray to ensure we're outside the box
100 vec3 rayOrigin = refPoint - rayDir * boxDiagonal * 2.0;
101 float tEntry = rayBoxEntry(rayOrigin, rayDir, uBoxMin, uBoxMax);
102 float tExit = rayBoxExit(rayOrigin, rayDir, uBoxMin, uBoxMax);
103
104 if (tExit <= tEntry) {
105 discard; // Ray misses box
106 }
107
108 entryPos = rayOrigin + rayDir * tEntry;
109 exitPos = rayOrigin + rayDir * tExit;
110 } else {
111 // Perspective: use camera position as ray origin
112 float tEntry = rayBoxEntry(uCameraPos, rayDir, uBoxMin, uBoxMax);
113 float tExit = rayBoxExit(uCameraPos, rayDir, uBoxMin, uBoxMax);
114
115 if (tExit <= tEntry || tExit <= 0.0) {
116 discard; // Ray misses box or is behind camera
117 }
118
119 if (tEntry > 0.0) {
120 entryPos = uCameraPos + rayDir * tEntry;
121 } else {
122 // Camera is inside the box
123 entryPos = uCameraPos;
124 }
125 exitPos = uCameraPos + rayDir * tExit;
126 }
127 entryIsSentinel = false;
128 exitIsSentinel = false;
129 }
130
131 vec3 dir = exitPos - entryPos;
132 float rayLength = length(dir);
133
134 // Discard if ray length is invalid:
135 // - Too short (no real volume to traverse)
136 // - Too long (positions are garbage/sentinel values)
137 if (rayLength < 0.0001 || rayLength > boxDiagonal * 2.0) {
138 discard;
139 }
140
141 vec3 stepDir = normalize(dir);
142 float stepSize = rayLength / float(numSteps);
143 vec3 step = stepDir * stepSize;
144
145 vec3 accumulatedColor = vec3(0.0);
146 float accumulatedAlpha = 0.0;
147 vec3 currentPosition = entryPos;
148
149 for (int i = 0; i < numSteps; i++)
150 {
151 if (accumulatedAlpha > 0.95)
152 break;
153
154 // Map world position to texture coordinates [0, 1]
155 vec3 volumeUV = (currentPosition - uBoxMin) / boxSize;
156
157 // Skip samples that are clearly outside (with small tolerance for edge precision)
158 if (any(lessThan(volumeUV, vec3(-0.01))) ||
159 any(greaterThan(volumeUV, vec3(1.01))))
160 {
161 currentPosition += step;
162 continue;
163 }
164
165 // Clamp to valid range for texture sampling (handles edge precision issues)
166 volumeUV = clamp(volumeUV, vec3(0.0), vec3(1.0));
167
168 float psi = texture(uVolumeData, volumeUV).r;
169
170 float amplitude = abs(psi);
171 vec3 color = (psi >= 0.0) ? positiveColor : negativeColor;
172
173 float alphaSample = amplitude * alphaScale;
174
175 // Front-to-back compositing with premultiplied alpha
176 accumulatedColor += (1.0 - accumulatedAlpha) * color * alphaSample;
177 accumulatedAlpha += (1.0 - accumulatedAlpha) * alphaSample;
178
179 currentPosition += step;
180 }
181
182 // Output premultiplied color and alpha - let GL blending composite over scene
183 colorOut = vec4(accumulatedColor, accumulatedAlpha);
184 }