Changing Terrain tint color via script
Asked Answered
B

2

0

Hello, I have a Terrain with a few TerrainLayers and I would like to change the tint color of the TerrainLayers via script.

The tint color property is only visible through the Terrain inspector and not through the TerrainLayer inspector and likewise, I don’t see a colorTint property in the Visual Studio suggestions under terrainLayer. Interestingly though, changing the color tint changes the color for all terrains that use that TerrainLayer.

Does anybody know if it is possible to change the TerrainLayer color tint via script?

If not, the only alternative I see would be to create new textures with the color already baked in and apply those via script using the terrainLayer.diffuseTexture property.

Thanks.

Boanerges answered 6/5, 2024 at 7:27 Comment(1)

Um... I don't see any tint color in the terrain, only Grass Tint which corresponds to TerrainData.wavingGrassTint.

Columbium
G
0

The documentation and the variables names are not clear at all here, but I found the solution to change the color tint of a terrain layer diffuse:

terrain.terrainData.terrainLayers[layerSlot].diffuseRemapMax = color;
  • terrain is the Terrain to edit
  • layerSlot is the number of your layer in the paint texture list on the terrain, it is an int
  • color is a Color, it should be the tint color you want.

Hope this helps :slight_smile:

Greasepaint answered 6/5, 2024 at 7:25 Comment(1)

thank you!!!!

Buffer
A
0

How to change via code is answered above, but here’s how to change via the inspector:

Select TerrainLayer - Inspector > Debug mode in top-right. Adjust Diffuse Remap Max (no color picker).

Here’s a script to change it via the inspector.

using System.Collections.Generic;
using UnityEngine;

// https://gist.github.com/st4rdog/40fc14a8a256c2376ba121bca33d7571
// Gets TerrainLayers assets used on selected terrain and applies tint/color.
// TerrainLayer color can be changed in Debug inspector view.
public class TerrainColorTint : MonoBehaviour
{
	public List<Terrain> Terrains = new();
	public List<Color> TargetColors = new();

	public TerrainLayer TerrainLayer;

	void OnValidate()
	{
		if (Terrains == null || Terrains.Count == 0) return;

		var layersCount = Terrains[0].terrainData.terrainLayers.Length;

		// Init with current colours
		{
			if (TargetColors.Count != layersCount)
			{
				TargetColors.Clear();

				for (int i = 0; i < layersCount; i++)
				{
					TargetColors.Add(Terrains[0].terrainData.terrainLayers[i].diffuseRemapMax);
				}
			}
		}

		// Apply
		{
			for (int i = 0; i < Terrains.Count; i++)
			{
				var terrain = Terrains[i];
				if (terrain == null) return;

				for (int j = 0; j < layersCount; j++)
				{
					terrain.terrainData.terrainLayers[j].diffuseRemapMax = TargetColors[j];
				}
			}
		}
	}
}

Alveolate answered 6/5, 2024 at 3:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.