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
[177599-fsggssgsdgssgd.png*|177599]
After
[177600-изображение-2021-03-16-164434.png*|177600]
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 *= 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.GameManager = gameManager;
// The problem is here
MeshRenderer meshRenderer = voxelGO.GetComponent();
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);
}
*
*