Add / Alpha for model (SOLVED)

Hi there,

In the render script I have these lines:

function init(self)

self.tile_pred = render.predicate({“tile”})

end

function update(self)

render.enable_state(render.STATE_BLEND)
render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)
render.draw(self.tile_pred)

end

The predicate “tile” is used everywhere in the game for sprites and I can set Add / Alpha using the editor switch. However if I use a model component, there is no switch in the editor and it seems to me that the engine always use Add…

How could I use Alpha with model?

Probably I am missing something. Any help is highly appreciated.

Ciao!

Try resetting the blend function before you draw your model predicate. Copy the line:

render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)

I’m not 100% sure why this would be necessary, but that solved it for me in the past.

1 Like

And indeed this worked also for me!

Thank you so much @ross.grams.

1 Like

I notice this blending like this:

render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)

Results in white turning grey (the grey cylinder has a pure white texture at 63% alpha):

I’ve been playing around with other modes listed here with no luck:

Any ideas on how to get the white to display as white?

I created a new predicate with this blender, which appears to be additive, but works in my case:

render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_DST_ALPHA)

I’m back at this and this time the trick above won’t work.

What it should look like:

What it looks like in game:

Minimal project: TransparentTexture.zip (353.4 KB)

I am using the unlit material from DefMaterial and Rendercam. Is there a way to make semi-transparency work on a model?

Some notes from @mats.gisselson:

  • Render transparent objects after all the opaque objects have been rendered.
  • Enable depth test, but disable depth write.
  • Use blend func render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA if not premultiplied, replace first arg with render.BLEND_ONE if premultiplied.
  • Render transparent objects in back-to front order from the camera.
  • Oh, and you probably want to enable back-face culling.

I haven’t tested any of this. I’m thinking that maybe @Dragosha, @d954mas or @Pkeod have some ideas as well.

2 Likes

looks like premultiplied alpha with black background?
How you turn on ADD blend for models? I noticed that if model/mesh renders after sprite with ADD blend the model/mesh inherits blend mode :confused:

Looks like Mats pulled it out of the bag! I followed the advice, but couldn’t find the “disable depth write”, and two predicates doesn’t seem necessary. The result:

Expected:

In game:

Spot the difference! This looks like it solves the issue.

The relevant render_script code:

render.set_blend_func(render.BLEND_ONE, render.BLEND_ONE_MINUS_SRC_ALPHA)

Adjusted project: TransparentTexture.zip (370.7 KB)

In a new project, using the same render script as above, I’ve noticed that when the Rendercam game object is tilted downwards (using a negative x angle) the transparency fails - but only on textures surrounded by transparency. When not tilted it works.

Tilted -20 degrees:

Not tilted:

Any idea why this happens?

Updated project: TransparentTexture.zip (291.9 KB)

1 Like

On my PC I see black borders in both cases, tilted or not.
It’s happened because for textures with alpha channel you need to make 2 pass in render. First for all models without alpha, second for models with alpha. (just add second predicate )

1 Like

Thanks! How would I modify the above project for it to work? I have tried a few things, but I don’t fully understand how draw calls and rendering work. So far I’ve tried these things:

  • Add a new “self.model_alpha_pred” predicate with a “model_alpha” tag.
  • Create a new material with the model_alpha tag.
  • Add code to render the model_alpha_pred in the .render_script.

What am I missing?

@totebo,

How did you solve this issue?

Thanks

Hey, I can’t remember if and how I solved it actually. Are you having a similar issue?

I had exactly the same issue (with the rotation of the sprite / tilted - not tilted), but I was able to solve it in a couple of hours of studying the topic :slight_smile:

The algorithm is very simple:

  1. Create a new material (copied from sprite) with new name and new tag
  2. Added a predicate for this tag to the render script
  3. Set rendering (draw call) immediately after rendering the tile predicate
  4. (!!!) Of course depth test included before draw call:
render.set_depth_mask(true)
render.enable_state(render.STATE_DEPTH_TEST)
render.draw(YOUR_NEW_PREDICATE)

It is all :wink:

Thanks

3 Likes