- Home /
Problem with material color change.
I have a method that imports a voxel model to Unity as a set of separate voxels. It creates and colors each voxel individually. I make copy of a material, change its color and assign it to a voxel. The result is very strange. Voxels have wrong color (in this example, blue is turned into pink, in other case, grey was turned into white), however, RGB values in inspector are corrent. Color becomes correct when I slightly move an RGB slider (or when I manually edit RGB values in the color window). I use the default shader. I couldn't find any working solution for this.
Edit: Also I get this warning, despite I don't call PropertiesGUI() :
PropertiesGUI() is being called recursively. If you want to render the default gui for shader properties
then call PropertiesDefaultGUI() instead
UnityEditor.EditorApplication:Internal_CallGlobalEventHandler()
Before I move an RGB slider
After
public static void Import(string path, string name, float voxelSize, bool isDeadly)
{
GameObject voxelPrefab = Resources.Load<GameObject>(VoxelPrefabPath);
GameManager gameManager = Object.FindObjectOfType<GameManager>();
var data = File.ReadAllBytes(path);
var chunks = Reader.GetChunks(data);
var voxelChunk = chunks.FirstOrDefault(c => c.Id == nameof(ChunkType.XYZI)) as VoxelChunk;
var paletteChunk = chunks.FirstOrDefault(c => c.Id == nameof(ChunkType.RGBA)) as PaletteChunk;
UnityEngine.Color[] colors = new UnityEngine.Color[paletteChunk.Colors.Length + 1];
for (int i = 1; i < colors.Length; i++)
{
colors[i] = ConvertVoxReaderColorToUnityColor(paletteChunk.Colors[i - 1]);
}
GameObject root = new GameObject(name);
foreach (Voxel voxel in voxelChunk.Voxels)
{
GameObject voxelGO = GameObject.Instantiate(voxelPrefab, root.transform);
voxelGO.transform.localScale = Vector3.one * voxelSize;
voxelGO.transform.position = new Vector3(voxel.X, voxel.Z, voxel.Y) * voxelSize;
VoxelBehaviour voxelBehaviour = voxelGO.GetComponent<VoxelBehaviour>();
voxelBehaviour.GameManager = gameManager;
// The problem is here
MeshRenderer meshRenderer = voxelGO.GetComponent<MeshRenderer>();
var clonedMaterial = new Material(meshRenderer.sharedMaterial);
clonedMaterial.color = colors[voxel.ColorIndex];
meshRenderer.sharedMaterial = clonedMaterial;
}
EditorSceneManager.MarkAllScenesDirty();
}
private static UnityEngine.Color ConvertVoxReaderColorToUnityColor(VoxReader.Color vrColor)
{
return new UnityEngine.Color(vrColor.R, vrColor.G, vrColor.B, vrColor.A);
}
Answer by yurykuz65 · Mar 17, 2021 at 10:25 AM
I fixed it. The problem was that is was calling Color constructor with [0; 255] values, but I needed to call it with [0; 1] values. i just needed to fix the color convertation method.
private static UnityEngine.Color ConvertVoxReaderColorToUnityColor(VoxReader.Color vrColor)
{
return new UnityEngine.Color((float)vrColor.R / 255, (float)vrColor.G / 255,
(float)vrColor.B / 255, (float)vrColor.A / 255);
}
Answer by HoweToGaming · Mar 16, 2021 at 03:33 PM
void Start()
{
//Create a new cube primitive to set the color on
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//Get the Renderer component from the new cube
var cubeRenderer = cube.GetComponent<Renderer>();
//Call SetColor using the shader property name "_Color" and setting the color to red
cubeRenderer.material.SetColor("_Color", Color.red);
}