google / filament

Filament is a real-time physically based rendering engine for Android, iOS, Windows, Linux, macOS, and WebGL2
https://google.github.io/filament/
Apache License 2.0
17.77k stars 1.88k forks source link

Setting a chromakey in material #3343

Closed MarkMurillo closed 3 years ago

MarkMurillo commented 3 years ago

I have a simple filament mat definition but can't seem to get the pixel to be transparent above a certain threshold. It has similar logic to how I'm doing it in my open GL shader but i'm pretty sure I'm calling the wrong methods...or setting the wrong configurations...

Here is my material definition:

material {
    name : textured,
    shadingModel : unlit,
    blending : transparent,
    transparency : twoPassesTwoSides,
    parameters : [
        {
            type : sampler2d,
            name : texture
        },
        {
            type : float4,
            name : chromaKey
        }
    ],
    requires : [
        uv0
    ]
}
fragment {
    void material(inout MaterialInputs material) {
        prepareMaterial(material);
        material.baseColor = texture(materialParams_texture, getUV0());

        // Check for rgb thresholds.

        // Check for red
        if (materialParams.chromaKey.r >= -1.0f) {
            if (material.baseColor.r > materialParams.chromaKey.r
            && material.baseColor.g < materialParams.chromaKey.g
            && material.baseColor.b < materialParams.chromaKey.b) {
                // Discard
                discard;
            }
        }

        // Check for green
        if (materialParams.chromaKey.g >= -1.0f) {
            if (material.baseColor.r < materialParams.chromaKey.r
            && material.baseColor.g > materialParams.chromaKey.g
            && material.baseColor.b < materialParams.chromaKey.b) {
                // Discard
                discard;
            }
        }

        // Check for blue
        if (materialParams.chromaKey.b >= -1.0f) {
            if (material.baseColor.r < materialParams.chromaKey.r
            && material.baseColor.g < materialParams.chromaKey.g
            && material.baseColor.b > materialParams.chromaKey.b) {
                // Discard
                discard;
            }
        }

        // Check for alpha
        if (materialParams.chromaKey.a == 0.0f) {
            // Discard
            discard;
        }
    }
}
MarkMurillo commented 3 years ago

Figured it out...changed the blending mode to masked and updated the logic a bit.

material {
    name : textured,
    shadingModel : unlit,
    blending : masked,
    maskThreshold : 0.5,
    parameters : [
        {
            type : sampler2d,
            name : texture
        },
        {
            type : float3,
            name : chromaKey
        }
    ],
    requires : [
        uv0
    ]
}
fragment {
    void material(inout MaterialInputs material) {
        prepareMaterial(material);
        material.baseColor = texture(materialParams_texture, getUV0());

        // Check for rgb thresholds.

        // Check for red
        if (materialParams.chromaKey.x >= -1.0f) {
            if (material.baseColor.r > materialParams.chromaKey.x
            && material.baseColor.g < materialParams.chromaKey.x
            && material.baseColor.b < materialParams.chromaKey.x) {
                // Discard
                material.baseColor = vec4(0.0, 0.0, 0.0, 0.0);
            }
        }

        // Check for green
        if (materialParams.chromaKey.y >= -1.0f) {
            if (material.baseColor.r < materialParams.chromaKey.y
            && material.baseColor.g > materialParams.chromaKey.y
            && material.baseColor.b < materialParams.chromaKey.y) {
                // Discard
                material.baseColor = vec4(0.0, 0.0, 0.0, 0.0);
            }
        }

        // Check for blue
        if (materialParams.chromaKey.z >= -1.0f) {
            if (material.baseColor.r < materialParams.chromaKey.z
            && material.baseColor.g < materialParams.chromaKey.z
            && material.baseColor.b > materialParams.chromaKey.z) {
                // Discard
                material.baseColor = vec4(0.0, 0.0, 0.0, 0.0);
            }
        }
    }
}