| 1 |
uniform sampler3D baseTexture; |
|---|
| 2 |
uniform float sampleDensity; |
|---|
| 3 |
uniform float transparency; |
|---|
| 4 |
uniform float alphaCutOff; |
|---|
| 5 |
|
|---|
| 6 |
varying vec4 cameraPos; |
|---|
| 7 |
varying vec4 vertexPos; |
|---|
| 8 |
varying mat4 texgen; |
|---|
| 9 |
|
|---|
| 10 |
void main(void) |
|---|
| 11 |
{ |
|---|
| 12 |
|
|---|
| 13 |
vec3 t0 = (texgen * vertexPos).xyz; |
|---|
| 14 |
vec3 te = (texgen * cameraPos).xyz; |
|---|
| 15 |
|
|---|
| 16 |
vec3 eyeDirection = normalize(te-t0); |
|---|
| 17 |
|
|---|
| 18 |
if (te.x>=0.0 && te.x<=1.0 && |
|---|
| 19 |
te.y>=0.0 && te.y<=1.0 && |
|---|
| 20 |
te.z>=0.0 && te.z<=1.0) |
|---|
| 21 |
{ |
|---|
| 22 |
// do nothing... te inside volume |
|---|
| 23 |
} |
|---|
| 24 |
else |
|---|
| 25 |
{ |
|---|
| 26 |
if (te.x<0.0) |
|---|
| 27 |
{ |
|---|
| 28 |
float r = -te.x / (t0.x-te.x); |
|---|
| 29 |
te = te + (t0-te)*r; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
if (te.x>1.0) |
|---|
| 33 |
{ |
|---|
| 34 |
float r = (1.0-te.x) / (t0.x-te.x); |
|---|
| 35 |
te = te + (t0-te)*r; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
if (te.y<0.0) |
|---|
| 39 |
{ |
|---|
| 40 |
float r = -te.y / (t0.y-te.y); |
|---|
| 41 |
te = te + (t0-te)*r; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
if (te.y>1.0) |
|---|
| 45 |
{ |
|---|
| 46 |
float r = (1.0-te.y) / (t0.y-te.y); |
|---|
| 47 |
te = te + (t0-te)*r; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
if (te.z<0.0) |
|---|
| 51 |
{ |
|---|
| 52 |
float r = -te.z / (t0.z-te.z); |
|---|
| 53 |
te = te + (t0-te)*r; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
if (te.z>1.0) |
|---|
| 57 |
{ |
|---|
| 58 |
float r = (1.0-te.z) / (t0.z-te.z); |
|---|
| 59 |
te = te + (t0-te)*r; |
|---|
| 60 |
} |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
const float max_iteratrions = 2048.0; |
|---|
| 64 |
float num_iterations = ceil(length(te-t0)/sampleDensity); |
|---|
| 65 |
if (num_iterations<2.0) num_iterations = 2.0; |
|---|
| 66 |
|
|---|
| 67 |
if (num_iterations>max_iteratrions) |
|---|
| 68 |
{ |
|---|
| 69 |
num_iterations = max_iteratrions; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
vec3 deltaTexCoord=(t0-te)/float(num_iterations-1.0); |
|---|
| 73 |
vec3 texcoord = te; |
|---|
| 74 |
|
|---|
| 75 |
vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0); |
|---|
| 76 |
vec4 previousColor = texture3D( baseTexture, texcoord); |
|---|
| 77 |
|
|---|
| 78 |
float normalSampleDistance = 1.0/512.0; |
|---|
| 79 |
vec3 deltaX = vec3(normalSampleDistance, 0.0, 0.0); |
|---|
| 80 |
vec3 deltaY = vec3(0.0, normalSampleDistance, 0.0); |
|---|
| 81 |
vec3 deltaZ = vec3(0.0, 0.0, normalSampleDistance); |
|---|
| 82 |
|
|---|
| 83 |
while(num_iterations>0.0) |
|---|
| 84 |
{ |
|---|
| 85 |
vec4 color = texture3D( baseTexture, texcoord); |
|---|
| 86 |
|
|---|
| 87 |
float m = (previousColor.a-alphaCutOff) * (color.a-alphaCutOff); |
|---|
| 88 |
if (m <= 0.0) |
|---|
| 89 |
{ |
|---|
| 90 |
float r = (alphaCutOff-color.a)/(previousColor.a-color.a); |
|---|
| 91 |
texcoord = texcoord - r*deltaTexCoord; |
|---|
| 92 |
|
|---|
| 93 |
float a = color.a; |
|---|
| 94 |
float px = texture3D( baseTexture, texcoord + deltaX).a; |
|---|
| 95 |
float py = texture3D( baseTexture, texcoord + deltaY).a; |
|---|
| 96 |
float pz = texture3D( baseTexture, texcoord + deltaZ).a; |
|---|
| 97 |
|
|---|
| 98 |
float nx = texture3D( baseTexture, texcoord - deltaX).a; |
|---|
| 99 |
float ny = texture3D( baseTexture, texcoord - deltaY).a; |
|---|
| 100 |
float nz = texture3D( baseTexture, texcoord - deltaZ).a; |
|---|
| 101 |
|
|---|
| 102 |
vec3 grad = vec3(px-nx, py-ny, pz-nz); |
|---|
| 103 |
vec3 normal = normalize(grad); |
|---|
| 104 |
|
|---|
| 105 |
float lightScale = 0.1 + abs(dot(normal.xyz, eyeDirection)); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
#if 0 |
|---|
| 109 |
color.x *= lightScale; |
|---|
| 110 |
color.y *= lightScale; |
|---|
| 111 |
color.z *= lightScale; |
|---|
| 112 |
#else |
|---|
| 113 |
color.x = lightScale; |
|---|
| 114 |
color.y = lightScale; |
|---|
| 115 |
color.z = lightScale; |
|---|
| 116 |
#endif |
|---|
| 117 |
|
|---|
| 118 |
fragColor = vec4(lightScale, lightScale, lightScale, 1.0); |
|---|
| 119 |
|
|---|
| 120 |
break; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
previousColor = color; |
|---|
| 124 |
|
|---|
| 125 |
texcoord += deltaTexCoord; |
|---|
| 126 |
|
|---|
| 127 |
--num_iterations; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
if (fragColor.w>1.0) fragColor.w = 1.0; |
|---|
| 131 |
//if (fragColor.w<alphaCutOff) discard; |
|---|
| 132 |
gl_FragColor = fragColor; |
|---|
| 133 |
} |
|---|