Setting alpha of model texture (SOLVED)

Is it possible to change the alpha of a model texture, like you can with tint.w on a sprite?

Check how the material is set up for the sprites, and do something similar for the model material.

2 Likes

It really was that easy. Works brilliantly, thanks!

The new material (edited from defmaterial’s “material-lit”): model_lit.zip (1.4 KB)

2 Likes

I wonder if this isn’t something we should do in the default material as well? @JCash what do you think?

It might be something we could add there yes.

Ah, I noticed that the default model material already has a tint constant, but it can’t be used to make a model semi-transparent. You can only use it to change (tint) RGB. That feels stupid to be honest. Why would you not want to also be able to adjust alpha?

model.fp from builtins:

varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
varying mediump vec4 var_light;

uniform lowp sampler2D tex0;
uniform lowp vec4 tint;

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    vec4 color = texture2D(tex0, var_texcoord0.xy) * tint_pm;

    // Diffuse light calculations
    vec3 ambient_light = vec3(0.2);
    vec3 diff_light = vec3(normalize(var_light.xyz - var_position.xyz));
    diff_light = max(dot(var_normal,diff_light), 0.0) + ambient_light;
    diff_light = clamp(diff_light, 0.0, 1.0);

    gl_FragColor = vec4(color.rgb*diff_light,1.0);
}

Change required to also adjust alpha of the model:

varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
varying mediump vec4 var_light;

uniform lowp sampler2D tex0;
uniform lowp vec4 tint;

void main()
{
    vec4 color = texture2D(tex0, var_texcoord0.xy);

    // Diffuse light calculations
    vec3 ambient_light = vec3(0.2);
    vec3 diff_light = vec3(normalize(var_light.xyz - var_position.xyz));
    diff_light = max(dot(var_normal,diff_light), 0.0) + ambient_light;
    diff_light = clamp(diff_light, 0.0, 1.0);

   gl_FragColor = vec4(color.rgb*diff_light*tint.xyz, tint.w);
}
1 Like