How to apply/mix multiple materials by vertex color mask? (Godot 3)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DimitriyPS

How to apply/mix multiple materials by vertex color mask?

To make it clearer what I want to do… was here is such Shader-material for Year from 2.X:

enter link description here

I am very interested in an analogue for materials from Godot 3. But I do not know how to solve this task

:bust_in_silhouette: Reply From: Zylann

You cannot mix two materials at the same time. A pixel can only be calculated using one shader (hence one material).

However if you are talking about textures, that’s possible with a shader that uses multiple “weights” to blend different visuals rather than only one set of textures.

A classical approach we can do in Godot is to have one of the vertex attribute being 4 “weights”, for example Color with R, G , B and A. Then, in a shader, you sample 4 textures, and blend the colors together using the 4 weights so you can choose how much of each gets rendererd in the final pixel.

This approach can expand for normal maps, roughness maps etc, however it is more expensive than regular materials because there are a lot more textures to fetch.

I am working on a terrain plugin that currently uses the 4-weights approach, but I’m using textures instead of vertices, with many more tweaks: https://github.com/Zylann/godot_heightmap_native_plugin/blob/master/addons/zylann.hterrain/shaders/simple4.shader

Texture arrays could allow more textures to be used but they aren’t supported yet in the API…