Colouring Mesh via Code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Catprog
:warning: Old Version Published before Godot 3 was released.

I am trying to make a mesh that starts off as a colour and fades to black as damage is applied.

Is their any capability to do this?

:bust_in_silhouette: Reply From: Zylann

You should be able to do that by modifying its material’s diffuse color. If your mesh is not too complicated, you can create a new material override (in MeshInstance) and modify the diffuse color of this one only, so it won’t affect other instances of the same mesh.

This is what I have so far

var mesh = meshNode.get_mesh();	
var mat = mesh.surface_get_material(0);		
mat.diffuse = 0;	
mesh.surface_set_material(0, mat);

But mat is returning null.

This is what I have for the editor.

Catprog | 2016-12-24 02:50

You can apply a material override in the inspector, or using the following code:

# Create a new FixedMaterial (for example)
var new_material = FixedMaterial.new()

# Assign it on the mesh as an override
mesh_instance.set_material_override(new_material)

# Modify the material (it will be applied on all meshes you use it on, here only one)
# Note: if you need a texture or other setup, you may want to set more params
new_material.set_parameter(Material.PARAM_DIFFUSE, Color(0,1,0))

Zylann | 2016-12-25 00:20