Skip to content

Commit

Permalink
Draw Sphere in the blind spot
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostKiwi committed Jan 18, 2024
1 parent 0b3d8e2 commit 0610b89
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/js/init_shaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ export function updateShaderAttributes(ctx, ctr, gl) {
Object.assign(ctx.shaders.project, {
pos: gl.getAttribLocation(ctx.shaders.project['handle' + aaSuffix], "pos"),
viewray: gl.getAttribLocation(ctx.shaders.project['handle' + aaSuffix], "rayvtx"),
viewray_blind: gl.getAttribLocation(ctx.shaders.project['handle' + aaSuffix], "rayvtx_blind"),
scaler: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "scalar"),
scaler_blind: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "scalar_blind"),
alpha: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "alpha"),
crop: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "crop"),
split: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "split"),
area_toggle:
gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "area_toggle"),
mask_toggle:
gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "mask_toggle"),
area_f: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "area_f"),
area_b: gl.getUniformLocation(ctx.shaders.project['handle' + aaSuffix], "area_b"),
rayvbo: gl.createBuffer()
Expand Down
15 changes: 13 additions & 2 deletions src/js/render_projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,36 @@ export default function render_project(width, height, channel) {

ctx.gl.enableVertexAttribArray(ctx.shaders.project.pos);
ctx.gl.enableVertexAttribArray(ctx.shaders.project.viewray);
ctx.gl.enableVertexAttribArray(ctx.shaders.project.viewray_blind);

ctx.gl.bindBuffer(ctx.gl.ARRAY_BUFFER, ctx.shaders.project.rayvbo);
ctx.gl.bufferData(ctx.gl.ARRAY_BUFFER, ctx.shaders.viewrays,
ctx.gl.DYNAMIC_DRAW);

ctx.gl.vertexAttribPointer(
ctx.shaders.project.pos, 2, ctx.gl.FLOAT, false,
5 * Float32Array.BYTES_PER_ELEMENT, 0
8 * Float32Array.BYTES_PER_ELEMENT, 0
);

ctx.gl.vertexAttribPointer(
ctx.shaders.project.viewray, 3, ctx.gl.FLOAT, false,
5 * Float32Array.BYTES_PER_ELEMENT,
8 * Float32Array.BYTES_PER_ELEMENT,
2 * Float32Array.BYTES_PER_ELEMENT
);

ctx.gl.vertexAttribPointer(
ctx.shaders.project.viewray_blind, 3, ctx.gl.FLOAT, false,
8 * Float32Array.BYTES_PER_ELEMENT,
5 * Float32Array.BYTES_PER_ELEMENT
);

/* As per formula */
const scalar = 1.0 / Math.sin(glm.glMatrix.toRadian(channel.fov_deg) / 4.0);
const scalar_blind = 1.0 / Math.sin(glm.glMatrix.toRadian(360-channel.fov_deg) / 4.0);
ctx.gl.uniform1f(ctx.shaders.project.scaler, scalar);
ctx.gl.uniform1f(ctx.shaders.project.scaler_blind, scalar_blind);
ctx.gl.uniform1f(ctx.shaders.project.area_toggle, ctr.tog.area);
ctx.gl.uniform1f(ctx.shaders.project.mask_toggle, ctr.tog.mask);
ctx.gl.uniform1f(ctx.shaders.project.area_f,
Math.sin(glm.glMatrix.toRadian(ctr.tog.area_f) / 4.0) / 2.0);
ctx.gl.uniform1f(ctx.shaders.project.area_b,
Expand Down
8 changes: 4 additions & 4 deletions src/js/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export let ctx = {
h: 0,
},
viewrays: new Float32Array([
- 1.0, 1.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.0, 0.0, 0.0,
1.0, -1.0, 0.0, 0.0, 0.0,
-1.0, -1.0, 0.0, 0.0, 0.0
- 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
-1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
]),
border: {
handle: null,
Expand Down
11 changes: 9 additions & 2 deletions src/js/update_camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function update_camera(width, height, channel) {
const ch1_rot_z = glm.glMatrix.toRadian(channel.rot_deg[2]);

/* Update View-Rays */
for (let i = 0; i < 4 * 5; i += 5) {
for (let i = 0; i < 4 * 8; i += 8) {
let vec = glm.vec3.fromValues(
ctx.shaders.viewrays[i] * half_aspect,
ctx.shaders.viewrays[i + 1] * 0.5,
Expand All @@ -36,10 +36,17 @@ export default function update_camera(width, height, channel) {
glm.vec3.rotateX(vec, vec, zero, ch1_rot_x);
glm.vec3.rotateY(vec, vec, zero, ch1_rot_y);
glm.vec3.rotateZ(vec, vec, zero, ch1_rot_z);

/* Assign to the buffer */
ctx.shaders.viewrays[i + 2] = vec[0];
ctx.shaders.viewrays[i + 3] = vec[1];
ctx.shaders.viewrays[i + 4] = vec[2];

/* Blind spot */
glm.vec3.rotateY(vec, vec, zero, glm.glMatrix.toRadian(-180));
/* Assign to the buffer */
ctx.shaders.viewrays[i + 5] = -vec[0];
ctx.shaders.viewrays[i + 6] = vec[1];
ctx.shaders.viewrays[i + 7] = vec[2];
}
}
24 changes: 23 additions & 1 deletion src/shd/project.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
medium */
precision highp float;
varying vec3 Ray;
varying vec3 Ray_blind;
uniform vec4 crop;
uniform float scalar;
uniform float scalar_blind;
uniform sampler2D sample_projection;
uniform bool mask_toggle;
uniform bool area_toggle;
uniform float area_f;
uniform float area_b;
Expand All @@ -21,13 +24,32 @@ void main()

/* Extra scalar branch to prevent artifacts from bad GPU float precision */
if (length(iRay_scaled) >= 0.5 && scalar > 1.0)
gl_FragColor = vec4(0.0, 0.0, 0.0, alpha);
{
/* Recalc Blindspot fill with flipped Yaw */
R = normalize(Ray_blind);
/* Scalar precalculated on CPU */
iRay = R.xy / (M_2xSQRT2 * sqrt(R.z + 1.0));
iRay_scaled = scalar_blind * iRay;

/* Scale from NDC to UV space */
vec2 uv = iRay_scaled * vec2(crop.z, crop.w);
uv.x = crop.x + uv.x;
uv.y = crop.y - uv.y;

/* Toggle to black when in mask mode */
if (mask_toggle)
gl_FragColor = vec4(vec3(0.0), alpha);
else
gl_FragColor = vec4(texture2D(sample_projection, uv).rgb, alpha);
}
else
{
/* Scale from NDC to UV space */
vec2 uv = iRay_scaled * vec2(crop.z, crop.w);
uv.x = crop.x + uv.x;
uv.y = crop.y - uv.y;

/* Handle Area Viz drawing */
if (area_toggle && length(iRay) < area_f)
gl_FragColor = vec4(texture2D(sample_projection, uv).rgb, alpha) * vec4(0.5, 1, 0.5, alpha);
else if (area_toggle && length(iRay) > area_b)
Expand Down
3 changes: 3 additions & 0 deletions src/shd/project.vs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
attribute vec2 pos;
attribute vec3 rayvtx;
attribute vec3 rayvtx_blind;
varying vec3 Ray;
varying vec3 Ray_blind;
uniform vec4 split;

void main()
{
Ray = rayvtx;
Ray_blind = rayvtx_blind;
gl_Position = vec4(pos * vec2(split.z, split.w) + vec2(split.x, split.y),
0.0, 1.0);
}
21 changes: 20 additions & 1 deletion src/shd/project_antialias.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
medium */
precision highp float;
varying vec3 Ray;
varying vec3 Ray_blind;
uniform vec4 crop;
uniform float scalar;
uniform float scalar_blind;
uniform sampler2D sample_projection;
uniform float area_toggle;
uniform bool mask_toggle;
uniform float area_f;
uniform float area_b;
uniform float alpha;
Expand All @@ -35,8 +38,24 @@ void main()
vec4 greenColor = baseColor * vec4(0.5, 1, 0.5, alpha);
vec4 redColor = baseColor * vec4(1, 0.5, 0.5, alpha);
vec4 blackColor = vec4(0.0, 0.0, 0.0, alpha);

float lenDist = length(iRay);

if (!mask_toggle)
{
/* Recalc Blindspot fill with flipped Yaw */
R = normalize(Ray_blind);
/* Scalar precalculated on CPU */
iRay = R.xy / (M_2xSQRT2 * sqrt(R.z + 1.0));
iRay_scaled = scalar_blind * iRay;

/* Scale from NDC to UV space */
vec2 uv = iRay_scaled * vec2(crop.z, crop.w);
uv.x = crop.x + uv.x;
uv.y = crop.y - uv.y;

blackColor = vec4(texture2D(sample_projection, uv).rgb, alpha);
}

float factorGreen = area_toggle * clamp((area_f - lenDist) / fwidth(lenDist), 0.0, 1.0);
float factorRed = area_toggle * clamp((lenDist - area_b) / fwidth(lenDist), 0.0, 1.0);
Expand Down

0 comments on commit 0610b89

Please sign in to comment.