@@ -14,6 +14,16 @@ export default /* wgsl */`
1414 varying vViewDir : vec3f;
1515 uniform skyboxHighlightMultiplier : f32;
1616
17+ #if defined(SKY_FISHEYE) && !defined(SKYMESH)
18+ uniform fisheye_k : f32;
19+ uniform fisheye_invK : f32;
20+ uniform fisheye_projMat00 : f32;
21+ uniform fisheye_projMat11 : f32;
22+ uniform matrix_view : mat4x4f;
23+ uniform cubeMapRotationMatrix : mat3x3f;
24+ varying vClipXYW : vec3f;
25+ #endif
26+
1727 #ifdef SKY_CUBEMAP
1828
1929 var texture_cubeMap : texture_cube<f32>;
@@ -52,22 +62,53 @@ export default /* wgsl */`
5262 var linear : vec3f;
5363 var dir : vec3f;
5464
55- #ifdef SKY_CUBEMAP
65+ // --- compute view direction ---
66+
67+ #if defined(SKY_FISHEYE) && !defined(SKYMESH)
68+
69+ let ndc : vec2f = input.vClipXYW.xy / input.vClipXYW.z;
70+ let px : f32 = ndc.x / uniform.fisheye_projMat00;
71+ let py : f32 = ndc.y / uniform.fisheye_projMat11;
72+ let r : f32 = sqrt(px * px + py * py);
73+ let theta : f32 = uniform.fisheye_k * atan(r * uniform.fisheye_invK);
74+ let sinT : f32 = sin(theta);
75+ let cosT : f32 = cos(theta);
76+ var camDir : vec3f;
77+ if (r > 1e-6) {
78+ camDir = vec3f(px / r * sinT, py / r * sinT, -cosT);
79+ } else {
80+ camDir = vec3f(0.0, 0.0, -1.0);
81+ }
82+ let viewMat3 : mat3x3f = mat3x3f(
83+ uniform.matrix_view[0].xyz,
84+ uniform.matrix_view[1].xyz,
85+ uniform.matrix_view[2].xyz
86+ );
87+ dir = transpose(viewMat3) * camDir;
88+ dir = dir * uniform.cubeMapRotationMatrix;
89+
90+ #elif defined(SKY_CUBEMAP) && defined(SKYMESH)
91+
92+ // get vector from world space pos to tripod origin
93+ var envDir : vec3f = normalize(input.vWorldPos - uniform.projectedSkydomeCenter);
94+ dir = envDir * uniform.cubeMapRotationMatrix;
95+
96+ #else
97+
98+ dir = input.vViewDir;
5699
57- #ifdef SKYMESH
58- // get vector from world space pos to tripod origin
59- var envDir : vec3f = normalize(input.vWorldPos - uniform.projectedSkydomeCenter);
60- dir = envDir * uniform.cubeMapRotationMatrix;
61- #else
62- dir = input.vViewDir;
63- #endif
100+ #endif
101+
102+ // --- sample environment ---
103+
104+ #ifdef SKY_CUBEMAP
64105
65106 dir.x *= -1.0;
66107 linear = {SKYBOX_DECODE_FNC}(textureSample(texture_cubeMap, texture_cubeMap_sampler, dir));
67108
68109 #else // env-atlas
69110
70- dir = input.vViewDir * vec3f(-1.0, 1.0, 1.0);
111+ dir *= vec3f(-1.0, 1.0, 1.0);
71112 let uv : vec2f = toSphericalUv(normalize(dir));
72113 linear = {SKYBOX_DECODE_FNC}(textureSample(texture_envAtlas, texture_envAtlas_sampler, mapRoughnessUv(uv, uniform.mipLevel)));
73114
0 commit comments