putting pos in coordinate space from -1,-1,-1 to 1,1,1

This commit is contained in:
buncccc 2025-04-14 20:17:36 +12:00
parent bca0869987
commit 6196971871
4 changed files with 34 additions and 12 deletions

View File

@ -104,7 +104,8 @@ public class RT3script : MonoBehaviour
//encode into pixels this camera's coordinates
// var tr = cameraList[i].transform.localToWorldMatrix;
int rowOffset = 2;
// UnityEngine.Debug.Log("Encoding Camera "+i+":\n" +cameraList[i].cameraToWorldMatrix.ToString());
// UnityEngine.Debug.Log("Encoding cameraToWorldMatrix for camera "+i+":\n" +cameraList[i].cameraToWorldMatrix.ToString());
// UnityEngine.Debug.Log("Encoding projectionMatrix for camera "+i+":\n" +cameraList[i].projectionMatrix.ToString());
rowOffset = encodeMatrix(cameraList[i].cameraToWorldMatrix, i, rowOffset);
rowOffset = encodeMatrix(cameraList[i].projectionMatrix, i, rowOffset);

View File

@ -173,7 +173,8 @@
pointSize: { value: pointSize },
cameraIndex: { value: i },
numCameras: { value: numCameras },
c2wm: { value: new THREE.Matrix4() }
c2wm: { value: new THREE.Matrix4() },
prjm: { value: new THREE.Matrix4() }
},
vertexShader: projection_vert_shader_source,
fragmentShader: projection_frag_shader_source,
@ -221,14 +222,22 @@
// this next line needs to obtain the region of the video texture with
// the appropriate pixel encoded floats for the camera matrix.
let d = context.getImageData((256*i),1,canvas.width,16); //should get data from the second line to the 17th, or 16 rows of pixels
let d = context.getImageData((256*i),1,canvas.width,32); //should get data from the second line to the 17th, or 16 rows of pixels
const c2wm_array = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
const prjm_array = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
for(let rowNr = 0; rowNr < 16; rowNr++) {
// cameraFloats[i][rowNr] = decodeUint32ToFloat(pixelArrayToUint32(d.data, c.width, rowNr))
c2wm_array[rowNr] = decodeUint32ToFloat(pixelArrayToUint32(d.data, canvas.width, rowNr))
prjm_array[rowNr] = decodeUint32ToFloat(pixelArrayToUint32(d.data, canvas.width, rowNr+16))
}
materialList[i].uniforms.c2wm.value.fromArray(c2wm_array);
materialList[i].uniforms.prjm.value.fromArray(prjm_array);
if (i==0) document.prjm0 = prjm_array; //these two lines make the projection array visible in console, for debugging
if (i==1) document.prjm1 = prjm_array;
// columns = document.prjm1; a = []; for (let row=0; row < 4; ++row) a.push([columns[0+row], columns[4+row], columns[8+row], columns[12+row]]); a
}
renderer.render(scene, camera);
}

View File

@ -2,6 +2,7 @@ uniform sampler2D map;
uniform float cameraIndex;
uniform float numCameras;
uniform mat4 c2wm;
uniform mat4 prjm;
varying vec2 vUv;
varying float paintfordiscard;

View File

@ -12,7 +12,7 @@ uniform float cameraIndex;
uniform float numCameras;
uniform mat4 c2wm;
uniform mat4 prjm;
varying vec2 vUv;
varying vec2 vUv1pxOffset;
@ -21,7 +21,7 @@ varying float paintfordiscard;
void main() {
vUv = vec2( position.x / width, position.y / height ); // -> vec2(0, 0)
vUv = vec2( position.x / width, position.y / height );
vUv1pxOffset = vec2( 1.0 / width, 1.0 / height ) * 2.0;
int skipctr = 0;
@ -29,7 +29,7 @@ void main() {
for (float xoff = -1.0; xoff < 2.0; xoff++) {
for (float yoff = -1.0; yoff < 2.0; yoff++) {
vec2 thisUv = vUv + (vec2 (xoff, yoff) * vUv1pxOffset);
vec2 thisUv = vUv + (vec2(xoff, yoff) * vUv1pxOffset);
vec4 color = texture2D(map, thisUv*vec2(1.0/numCameras,(1.0/3.0))+vec2(cameraIndex/numCameras,(1.0/3.0)));
float depth = ( color.r + color.g + color.b ) / 3.0;
if (depth < 0.01) {
@ -50,15 +50,26 @@ void main() {
// guess the projection.
// Undoes the perspective division
//float z = depth * (farClipping-nearClipping) + nearClipping;
float z = depth;
// float z = depth;
// vec4 pos = vec4(
// ( position.x / width - 0.5 )*boxSize,
// ( position.y / height - 0.5 )*boxSize,
// z*boxSize*0.5, // we had applied a (-1.0) multiplier to try to flip element [2,2]
// 1.0);
vec4 pos = vec4(
( position.x / width - 0.5 )*boxSize,
( position.y / height - 0.5 )*boxSize,
z*boxSize*0.5, // we had applied a (-1.0) multiplier to try to flip element [2,2]
1.0);
2.*(position.x / float(width)) - 1.,
2.*(position.y / float(height)) - 1.,
2.*depth - 1.,
1.0
);
//vec4 pos2 = pos;
vec4 pos2 = c2wm * pos;
// vec4 pos2 = c2wm * prjm * pos; // order matters here! First undo projection, then undo view, then below model is undone
vec4 pos2 = c2wm * inverse(prjm) * pos; // order matters here! First undo projection, then undo view, then below model is undone
pos2.xyz *= boxSize;
// pos2 = pos2 * vec4(20000., 20000., 20000., 20000.); // bad idea! the last point needs to be 1 for translation to work
// pos2 = pos2 * boxSize;
//vec4 pos2 = inverse(c2wm) * pos;
// float(cameraIndex)
gl_PointSize = pointSize;