Fisheye Skybox Shader

问题描述 投票:1回答:1

我正在尝试编写全景查看器。通常,这涉及将图像映射到四边形以模拟天空盒。

对于立方体贴图图像,它很简单。 copy the 6 parts of the image to a 6 planes of a cube map或创建一个执行the cubemap math as specified in the OpenGL ES spec

的着色器

对于像来自理光Theta的等矩形图像

“”

您可以使用此数学运算

      // convert from direction (n) to texcoord (uv)
      float latitude = acos(n.y);
      float longitude = atan(n.z, n.x);
      vec2 sphereCoords = vec2(longitude, latitude) * vec2(0.5 / PI, 1.0 / PI);
      vec2 uv = fract(vec2(0.5,1.0) - sphereCoords);

const m4 = twgl.m4;
const gl = document.querySelector('canvas').getContext('webgl');

const vs = `
attribute vec4 position;
varying vec4 v_position;
void main() {
  v_position = position;
  gl_Position = position;
  gl_Position.z = 1.0;
}
`;

const fs = `
precision highp float;
 
uniform sampler2D u_skybox;
uniform mat4 u_viewDirectionProjectionInverse;
 
varying vec4 v_position;

#define PI radians(180.0)

void main() {
  vec4 t = u_viewDirectionProjectionInverse * v_position;
  vec3 n = normalize(t.xyz / t.w);

  // convert from direction (n) to texcoord (uv)
  float latitude = acos(n.y);
  float longitude = atan(n.z, n.x);
  vec2 sphereCoords = vec2(longitude, latitude) * vec2(0.5 / PI, 1.0 / PI);
  vec2 uv = fract(vec2(0.5,1.0) - sphereCoords);

  // multiply u by 2 because we only have a 180degree view
  gl_FragColor = texture2D(u_skybox, uv * vec2(-2, 1));
}
`;


const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
const tex = twgl.createTexture(gl, {
  src: 'https://i.imgur.com/ChIfXM0.jpg',
  flipY: true,
});
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl, 2);

function render(time) {
  time *= 0.001;
  
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

  const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  const projectionMatrix = m4.perspective(45 * Math.PI / 180, aspect, 1, 20);
 
  const cameraMatrix = m4.rotationY(time * 0.1);
  m4.rotateX(cameraMatrix, Math.sin(time * 0.3) * 0.5, cameraMatrix);
 
  const viewMatrix = m4.inverse(cameraMatrix);
  viewMatrix[12] = 0;
  viewMatrix[13] = 0;
  viewMatrix[14] = 0;
 
  const viewDirectionProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
  const viewDirectionProjectionInverseMatrix = m4.inverse(viewDirectionProjectionMatrix);  
  
  gl.useProgram(programInfo.program);
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, {
    u_viewDirectionProjectionInverse: viewDirectionProjectionInverseMatrix,
    u_skyBox: tex,
  });
  twgl.drawBufferInfo(gl, bufferInfo);

  requestAnimationFrame(render);
}
requestAnimationFrame(render);
body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<canvas></canvas>

但是有些图像不是矩形的,是鱼眼的?

“”

我一直在试图找出做相同事情所需的数学运算(将其映射到基于四边形的天空盒),但是我一直没有运气

作为参考,我发现this page具有从3d到鱼眼坐标的转换。它说

      // convert from direction (n) to texcoord (uv)
      float r = 2.0 * atan(length(n.xy), n.z) / PI;
      float theta = atan(n.y, n.x);
      vec2 uv = vec2(cos(theta), sin(theta)) * r * 0.5 + 0.5;

const m4 = twgl.m4;
const gl = document.querySelector('canvas').getContext('webgl');

const vs = `
attribute vec4 position;
varying vec4 v_position;
void main() {
  v_position = position;
  gl_Position = position;
  gl_Position.z = 1.0;
}
`;

const fs = `
precision highp float;
 
uniform sampler2D u_skybox;
uniform mat4 u_viewDirectionProjectionInverse;
 
varying vec4 v_position;

#define PI radians(180.0)

void main() {
  vec4 t = u_viewDirectionProjectionInverse * v_position;
  vec3 n = normalize(t.xyz / t.w);
  
  // convert from direction (n) to texcoord (uv)
  float r = 2.0 * atan(length(n.xy), n.z) / PI;
  float theta = atan(n.y, n.x);
  vec2 uv = vec2(cos(theta), sin(theta)) * r * 0.5 + 0.5;

  #if 0
	// Calculate fisheye angle and radius
	float theta = atan(n.z, n.x);
	float phi = atan(length(n.xz), n.y);
	float r = phi / PI; 

	// Pixel in fisheye space
	vec2 uv = vec2(0.5) + r * vec2(cos(theta), sin(theta));
  #endif

  // multiply u by 2 because we only have a 180degree view
  gl_FragColor = texture2D(u_skybox, uv * vec2(-2, 1));
}
`;


const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
const tex = twgl.createTexture(gl, {
  src: 'https://i.imgur.com/dzXCQwM.jpg',
  flipY: true,
});
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl, 2);

function render(time) {
  time *= 0.001;
  
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

  const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  const projectionMatrix = m4.perspective(45 * Math.PI / 180, aspect, 1, 20);
 
  const cameraMatrix = m4.rotationY(time * 0.1);
  m4.rotateX(cameraMatrix, 0.7 + Math.sin(time * 0.3) * .7, cameraMatrix);
 
  const viewMatrix = m4.inverse(cameraMatrix);
  viewMatrix[12] = 0;
  viewMatrix[13] = 0;
  viewMatrix[14] = 0;
 
  const viewDirectionProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
  const viewDirectionProjectionInverseMatrix = m4.inverse(viewDirectionProjectionMatrix);  
  
  gl.useProgram(programInfo.program);
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, {
    u_viewDirectionProjectionInverse: viewDirectionProjectionInverseMatrix,
    u_skyBox: tex,
  });
  twgl.drawBufferInfo(gl, bufferInfo);

  requestAnimationFrame(render);
}
requestAnimationFrame(render);
body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<canvas></canvas>

this

很明显我缺少什么。

glsl webgl
1个回答
0
投票

我相信错误在于此逻辑:

// multiply u by 2 because we only have a 180degree view
gl_FragColor = texture2D(u_skybox, uv * vec2(-2, 1));

尽管这在等角矩形情况下有效,因为数学计算得出z分量仅影响经度,但在鱼眼镜头情况下它不再有效,因为n.z影响两个轴。

您可以通过取n.z的绝对值并在z为负数时翻转n.x来计算公式中的负z分量:

  // convert from direction (n) to texcoord (uv)
  float r = 2.0 * atan(length(n.xy), abs(n.z)) / PI;
  float theta = atan(n.y, -n.x * sign(n.z));
  vec2 uv = vec2(cos(theta), sin(theta)) * r * 0.5 + vec2(0.5);

这里正在起作用:

const m4 = twgl.m4;
const gl = document.querySelector('canvas').getContext('webgl');

const vs = `
attribute vec4 position;
varying vec4 v_position;
void main() {
  v_position = position;
  gl_Position = position;
  gl_Position.z = 1.0;
}
`;

const fs = `
precision highp float;
 
uniform sampler2D u_skybox;
uniform mat4 u_viewDirectionProjectionInverse;
 
varying vec4 v_position;

#define PI radians(180.0)

void main() {
  vec4 t = u_viewDirectionProjectionInverse * v_position;
  vec3 n = normalize(t.xyz / t.w);
  
  // convert from direction (n) to texcoord (uv)
  float r = 2.0 * atan(length(n.xy), abs(n.z)) / PI;
  float theta = atan(n.y, -n.x * sign(n.z));
  vec2 uv = vec2(cos(theta), sin(theta)) * r * 0.5 + vec2(0.5);

  gl_FragColor = texture2D(u_skybox, uv);
}
`;


const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
const tex = twgl.createTexture(gl, {
  src: 'https://i.imgur.com/dzXCQwM.jpg',
  flipY: true,
});
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl, 2);

function render(time) {
  time *= 0.001;
  
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

  const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  const projectionMatrix = m4.perspective(45 * Math.PI / 180, aspect, 1, 20);
 
  const cameraMatrix = m4.rotationY(time * 0.1);
  m4.rotateX(cameraMatrix, 0.7 + Math.sin(time * 0.3) * .7, cameraMatrix);
 
  const viewMatrix = m4.inverse(cameraMatrix);
  viewMatrix[12] = 0;
  viewMatrix[13] = 0;
  viewMatrix[14] = 0;
 
  const viewDirectionProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
  const viewDirectionProjectionInverseMatrix = m4.inverse(viewDirectionProjectionMatrix);  
  
  gl.useProgram(programInfo.program);
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, {
    u_viewDirectionProjectionInverse: viewDirectionProjectionInverseMatrix,
    u_skyBox: tex,
  });
  twgl.drawBufferInfo(gl, bufferInfo);

  requestAnimationFrame(render);
}
requestAnimationFrame(render);
body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<canvas></canvas>
© www.soinside.com 2019 - 2024. All rights reserved.