Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-medium-2505· Mistral AIHugging Face LMArena Human Preference 140K

How do I mirror geometry in Godot via a vertex spatial shader, as in flip it…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-preview-02-24’s answer.
  1. User

    How do I mirror geometry in Godot via a vertex spatial shader, as in flip it horizontally? The normal/binormal/tangent needs to reflect the mirroring to not break lighting.

  2. Mistral Assistant

    Mirroring geometry in Godot via a vertex shader requires modifying the vertex positions and adjusting the normal, binormal, and tangent vectors to ensure correct lighting. Here's how you can achieve a horizontal flip (mirroring along the X-axis) in a SpatialMaterial with a vertex shader:

    Steps:

    1. Create a SpatialMaterial and assign a shader to it.
    2. Modify the vertex positions by flipping the X-coordinate.
    3. Adjust the normal, binormal, and tangent to match the mirrored geometry.

    Shader Code:

    shader_type spatial;
    render_mode unshaded; // Optional: Remove if you want lighting
    
    uniform float mirror_axis = 1.0; // 1.0 for X-axis, -1.0 for Y or Z if needed
    
    void vertex() {
        // Mirror the vertex position (flip X-axis)
        VERTEX = VERTEX * vec3(mirror_axis, 1.0, 1.0);
    
        // Adjust the normal, binormal, and tangent to match the mirroring
        NORMAL = NORMAL * vec3(mirror_axis, 1.0, 1.0);
        BINORMAL = BINORMAL * vec3(mirror_axis, 1.0, 1.0);
        TANGENT = TANGENT * vec3(mirror_axis, 1.0, 1.0);
    
        // Ensure the normal is renormalized (optional but recommended)
        NORMAL = normalize(NORMAL);
    }
    

    Explanation:

    • VERTEX: The vertex position is flipped along the X-axis by multiplying by vec3(-1.0, 1.0, 1.0) (or mirror_axis if you want to control it dynamically).
    • NORMAL, BINORMAL, TANGENT: These vectors must also be mirrored to ensure lighting calculations remain correct. If you don't adjust them, lighting will appear incorrect on the mirrored side.
    • normalize(NORMAL): Ensures the normal vector remains unit-length after transformation.

    Applying the Shader:

    1. Create a SpatialMaterial in Godot.
    2. Assign a new Shader to it and paste the code above.
    3. Apply the material to your mesh.

    Alternative: Using a Uniform for Dynamic Control

    If you want to toggle mirroring dynamically, you can expose a uniform:

    uniform bool should_mirror = false;
    
    void vertex() {
        float mirror_factor = should_mirror ? -1.0 : 1.0;
        VERTEX.x *= mirror_factor;
        NORMAL.x *= mirror_factor;
        BINORMAL.x *= mirror_factor;
        TANGENT.x *= mirror_factor;
        NORMAL = normalize(NORMAL);
    }
    

    Then, you can control should_mirror from a script.

    Notes:

    • This works for static meshes. For skeletal meshes, you may need additional adjustments in the shader.
    • If you're using UVs, you might also need to flip them (UV.x = 1.0 - UV.x) if the texture appears mirrored incorrectly.

    This approach ensures that the geometry is mirrored while preserving correct lighting.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.