Camera and reader rules corrected against a real file
The depth row maps the slab to the full clip range, not half of it, so the CPU sort and the GPU depth test agree on which fragment is nearer. The screen-right axis is the unit horizontal: built as forward x worldUp it carries a factor of cos(pitch) and compresses screen X as the camera tilts, so the pitch clamp is the edge-on guard rather than a fix for a zero vector. MAT4 is an accessor type and count is the number of elements, not floats, which is the shape a skin's inverse bind matrices actually have. node.matrix is column-major while Mat4 is row-major. A skin's joints must be resolved by node index, because CesiumMan.glb keeps its skeleton outside the scene root.
This commit is contained in:
+32
-13
@@ -218,7 +218,10 @@ check('70/30 blend lands between the two poses', [round(v, 6) for v in skinned],
|
||||
def basis(yaw_deg, pitch_deg):
|
||||
y, p = math.radians(yaw_deg), math.radians(pitch_deg)
|
||||
forward = (-math.cos(p) * math.sin(y), math.cos(p) * math.cos(y), -math.sin(p))
|
||||
right = (math.cos(p) * math.cos(y), math.cos(p) * math.sin(y), 0.0)
|
||||
# The unit horizontal, not forward x worldUp. That cross product carries a factor of cos(pitch)
|
||||
# and shrinks to a zero vector at pitch 90, which would scale screen X by cos(pitch) as the camera
|
||||
# tilts - a silent compression, not an error.
|
||||
right = (math.cos(y), math.sin(y), 0.0)
|
||||
up = (-math.sin(p) * math.sin(y), math.sin(p) * math.cos(y), math.cos(p))
|
||||
return forward, right, up
|
||||
|
||||
@@ -261,14 +264,15 @@ approx('basis stays orthonormal at yaw 37 pitch 21',
|
||||
round(f[0] * u[0] + f[1] * u[1] + f[2] * u[2], 9),
|
||||
round(r[0] * u[0] + r[1] * u[1] + r[2] * u[2], 9)], [0.0, 0.0, 0.0])
|
||||
|
||||
# Real degeneracy: at pitch == +/-90 the camera looks along the world up axis, so the
|
||||
# screen-right axis is undefined. The camera must clamp pitch to the open interval and
|
||||
# the UI must not allow a drag that reaches it.
|
||||
# At pitch == +/-90 the camera looks along the world up axis, which is the drawing plane's normal,
|
||||
# so the puppet is seen edge-on and the reference layer collapses to a line. Nothing reports it -
|
||||
# the projection still returns a number - so the clamp belongs in the model, not in the drag handler.
|
||||
# The right axis is the unit horizontal, so it does not collapse here; building it as
|
||||
# forward x worldUp would have made it cos(pitch), exactly zero at 90.
|
||||
f, r, u = basis(0, 90)
|
||||
check('pitch 90 collapses the right axis (must be clamped)', round(math.sqrt(sum(v * v for v in r)), 9), 0.0)
|
||||
|
||||
f, r, u = basis(0, 89.9)
|
||||
approx('pitch 89.9 still gives a usable right axis', [round(v, 6) for v in r], (0.001745, 0.0, 0.0))
|
||||
check('right stays unit length at pitch 90', round(math.sqrt(sum(v * v for v in r)), 9), 1.0)
|
||||
check('pitch 90 puts the view along the world up axis', [round(v, 9) for v in f], [0.0, 0.0, -1.0])
|
||||
check('and the drawing plane is edge-on: up projects to nothing', round(u[2], 9), 0.0)
|
||||
|
||||
print()
|
||||
if FAILURES:
|
||||
@@ -283,7 +287,10 @@ print('all checks pass')
|
||||
def ortho_matrix(target, yaw, pitch, zoom, w, h, near, far):
|
||||
forward, right, up = basis(yaw, pitch)
|
||||
sx, sy = 2 * zoom / w, 2 * zoom / h
|
||||
dz = 1.0 / (far - near)
|
||||
# near and far are distances from the target, so the slab runs from -near (toward the viewer) to
|
||||
# +far. The depth row maps that slab to the full clip range [-1, 1]; mapping it to [0, 1] would
|
||||
# throw away half the precision for nothing.
|
||||
dz = 2.0 / (near + far)
|
||||
def dot(a, b):
|
||||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
|
||||
m = [0.0] * 16
|
||||
@@ -291,8 +298,8 @@ def ortho_matrix(target, yaw, pitch, zoom, w, h, near, far):
|
||||
for c in range(3):
|
||||
m[r * 4 + c] = axis[c] * scale
|
||||
m[r * 4 + 3] = -dot(axis, target) * scale
|
||||
m[2 * 4 + 3] += near * dz - 1.0
|
||||
m[15] = 1.0
|
||||
m[2 * 4 + 3] -= near * dz
|
||||
return m
|
||||
|
||||
|
||||
@@ -300,12 +307,24 @@ def apply_matrix(m, p):
|
||||
return tuple(m[r * 4 + 0] * p[0] + m[r * 4 + 1] * p[1] + m[r * 4 + 2] * p[2] + m[r * 4 + 3] for r in range(3))
|
||||
|
||||
|
||||
def window_depth(clip_z):
|
||||
return (clip_z + 1.0) / 2.0
|
||||
|
||||
|
||||
NEAR, FAR = 10.0, 30.0
|
||||
|
||||
for point, yaw, pitch in (((1, 0, 0), 0, 0), ((0, 0, 1), 0, 0), ((0, 1, 0), 90, 0), ((0.3, 0.4, 0.5), 37, 21)):
|
||||
direct, _ = project(point, (0, 0, 0), yaw, pitch, 1, 400, 400)
|
||||
m = ortho_matrix((0, 0, 0), yaw, pitch, 1, 400, 400, -10.0, 10.0)
|
||||
m = ortho_matrix((0, 0, 0), yaw, pitch, 1, 400, 400, NEAR, FAR)
|
||||
via_matrix = apply_matrix(m, point)
|
||||
depth_ok = abs(via_matrix[2] - (direct[2] + 10) / 20) < 1e-9
|
||||
depth_ok = abs(window_depth(via_matrix[2]) - (direct[2] + NEAR) / (NEAR + FAR)) < 1e-9
|
||||
xy_ok = all(abs(a - b) < 1e-9 for a, b in zip(via_matrix[:2], direct[:2]))
|
||||
check(f'matrix form agrees with the direct form at yaw {yaw} pitch {pitch}', (round(xy_ok), round(depth_ok)), (1, 1))
|
||||
|
||||
check('depth maps near to 0 and far to 1', [round(v, 6) for v in apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, -10.0, 10.0), (0, -10, 0))], [0.0, 0.0, 0.0])
|
||||
check('the near end of the slab maps to window depth 0', round(window_depth(apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, NEAR, FAR), (0, -NEAR, 0))[2]), 6), 0.0)
|
||||
check('the far end of the slab maps to window depth 1', round(window_depth(apply_matrix(ortho_matrix((0, 0, 0), 0, 0, 1, 400, 400, NEAR, FAR), (0, FAR, 0))[2]), 6), 1.0)
|
||||
|
||||
# Applying the columns instead of the rows is the transpose. It is invisible at zero angles, where the
|
||||
# matrix is diagonal, so the check has to run at a rotated camera.
|
||||
m = ortho_matrix((0, 0, 0), 37, 21, 1, 400, 400, NEAR, FAR)
|
||||
check('the matrix is applied by rows, not columns', round(m[1], 9) != round(m[4], 9), True)
|
||||
|
||||
Reference in New Issue
Block a user