Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

write shader in GLSL for cloud particle with normal map

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-sonnet-20241022’s answer.
  1. User

    write shader in GLSL for cloud particle with normal map

  2. Assistant

    Here’s a pair of GLSL shaders (vertex + fragment) that render a single cloud‐particle as a camera‐facing billboard, lit by a normal map. The particle always faces the camera, and per-pixel lighting is done in tangent space using the normal map.

    Feel free to adapt the uniforms or attribute binding locations to your engine/GL setup.

    //----------------------------------------------------------------------------
    // Vertex Shader
    //----------------------------------------------------------------------------
    #version 330 core
    
    // Attributes for each quad vertex:
    //   aOffset.xy : local quad coords in [-0.5,+0.5] range
    //   aUV     .xy : texture coordinates
    layout(location = 0) in vec2 aOffset;
    layout(location = 1) in vec2 aUV;
    
    // Uniforms
    uniform mat4 uView;         // view matrix
    uniform mat4 uProj;         // projection matrix
    uniform vec3 uCamPos;       // world‐space camera position
    uniform vec3 uCamRight;     // world‐space "right" vector of camera
    uniform vec3 uCamUp;        // world‐space "up" vector of camera
    
    uniform vec3 uParticlePos;  // world‐space center of this billboard
    uniform vec2 uScale;        // scale in x (width) and y (height) directions
    
    // Light (world‐space)
    uniform vec3 uLightPos;
    
    // Outputs to fragment shader
    out vec2 vUV;
    out vec3 vTangentLightDir;  // light direction in tangent space
    out vec3 vTangentViewDir;   // view direction in tangent space
    
    void main() {
        // 1) Compute world‐space position of this quad vertex
        vec3 worldPos = uParticlePos
                      + uCamRight * (aOffset.x * uScale.x)
                      + uCamUp    * (aOffset.y * uScale.y);
    
        // 2) Billboard normal (faces camera)
        //    Since the billboard is camera-facing, its normal = cross(camRight, camUp)
        vec3 N = normalize(cross(uCamRight, uCamUp));
        vec3 T = normalize(uCamRight);
        vec3 B = normalize(uCamUp);
    
        // 3) Build the TBN matrix (columns = T, B, N)
        mat3 TBN = mat3(T, B, N);
    
        // 4) Compute light & view vectors in world space
        vec3 L = normalize(uLightPos - worldPos);
        vec3 V = normalize(uCamPos   - worldPos);
    
        // 5) Transform into tangent space (TBN^T = inverse since TBN is orthonormal)
        vTangentLightDir = TBN * L;
        vTangentViewDir  = TBN * V;
    
        // 6) Pass UV
        vUV = aUV;
    
        // 7) Final gl_Position
        gl_Position = uProj * uView * vec4(worldPos, 1.0);
    }
    
    //----------------------------------------------------------------------------
    // Fragment Shader
    //----------------------------------------------------------------------------
    #version 330 core
    
    in vec2 vUV;
    in vec3 vTangentLightDir;
    in vec3 vTangentViewDir;
    
    out vec4 fragColor;
    
    // Samplers
    uniform sampler2D uDiffuseMap;  // RGB cloud texture + alpha
    uniform sampler2D uNormalMap;   // tangent-space normal map (RGBA or RGB)
    
    // Material
    uniform vec3 uAmbientColor;     // low ambient light
    uniform vec3 uSpecularColor;    // specular highlight color
    uniform float uShininess;       // specular exponent
    uniform float uAlphaCutoff;     // alpha test threshold
    
    void main() {
        // 1) Sample base color and alpha
        vec4 baseCol = texture(uDiffuseMap, vUV);
        if (baseCol.a < uAlphaCutoff)
            discard;
    
        // 2) Sample and unpack normal map (in [0,1]) → tangent‐space normal
        vec3 N = texture(uNormalMap, vUV).rgb * 2.0 - 1.0;
        N = normalize(N);
    
        // 3) Normalize interpolated light & view directions
        vec3 L = normalize(vTangentLightDir);
        vec3 V = normalize(vTangentViewDir);
    
        // 4) Diffuse term
        float diff = max(dot(N, L), 0.0);
    
        // 5) Specular term (Blinn‐Phong)
        vec3 H = normalize(L + V);
        float spec = pow(max(dot(N, H), 0.0), uShininess);
    
        // 6) Combine
        vec3 color = uAmbientColor * baseCol.rgb
                   + diff * baseCol.rgb
                   + spec * uSpecularColor;
    
        fragColor = vec4(color, baseCol.a);
    }
    

    Usage notes:

    • You’ll need to supply per‐particle uniforms (uParticlePos, uScale) each draw call (or via a UBO/SSBO).
    • The quad’s vertex buffer holds only 4 vertices with aOffset = {(-0.5, -0.5), (0.5,-0.5), (0.5,0.5), (-0.5,0.5)} and matching UVs.
    • Make sure your normal map is in tangent space (RGB channels).
    • Tweak uAmbientColor, uSpecularColor and uShininess to taste.
    • If you need multiple lights, either loop in the shader or accumulate contributions in a deferred pass.

    That’s it! You now have a camera‐facing cloud particle shaded per-pixel with a normal map for fat, fluffy lighting detail. 🌥️🖌️

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