Build with XuiView Markdown

3D coordinates and cameras

A camera transform answers three questions: where is the model, where is the viewer, and how does distance affect the picture? Keep those answers in separate matrices so you can change one without rewriting the others.

Xui's shader Float4x4 multiplies column vectors. Compose the transform as projection * view * model, then multiply it by the position. Reversing the order changes the meaning of the scene.

Build the transform

using System;
using Xui.GPU.Shaders.Types;
using NFloat = System.Runtime.InteropServices.NFloat;

public static class CameraTransform
{
    public static Float4x4 Create(int width, int height, NFloat pitch, NFloat yaw)
    {
        if (width <= 0 || height <= 0)
            throw new ArgumentOutOfRangeException(nameof(width));
        var model = Float4x4.CreateRotationX((float)pitch) * Float4x4.CreateRotationY((float)yaw);
        var view = Float4x4.CreateLookAt(new Float3(0, 0, 5), new Float3(0, 0, 0), new Float3(0, 1, 0));
        var projection = Float4x4.CreatePerspective(MathF.PI / 4, (float)width / height, 0.1f, 100);
        return projection * view * model;
    }
}

Call CameraTransform.Create(target.Width, target.Height, pitch, yaw) from the GPU callback and upload its result as the matrix binding. Width and height here are physical target pixels, not the view's logical coordinates. Their ratio keeps the image proportionate at different display scales.

The model rotates around X and Y. The view looks from (0, 0, 5) toward the origin with Y pointing up. The projection uses a 45-degree vertical field of view, a near plane at 0.1, and a far plane at 100. Geometry outside that depth interval is clipped.

Resize without stretching

Recompute projection when the target's aspect ratio changes. A portrait window should reveal a different horizontal extent rather than squash a sphere into an oval. Guard against zero-sized surfaces before dividing; the helper rejects them explicitly.

CreatePerspective uses a zero-to-one depth range. Keep backend coordinate adjustments in the shader/backend pipeline; do not scatter compensating flips through app input and rendering code.

UI positions and drag distances use NFloat. Shader buffers use the fixed-width F32 and vector types required by the GPU layout. Convert at that boundary rather than changing the whole UI to single precision.

Try a camera change

Move the eye farther from the origin to make the model smaller. Change the field of view to compare a narrow lens with a wide one. Keep the near plane positive and smaller than the far plane. If geometry disappears, first check matrix order, clipping planes, and whether the model lies in front of the camera; only then investigate shader compilation.