Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yurykuz65 · Mar 16, 2021 at 01:48 PM · shadermaterialshadersmaterialsmaterial color

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 alt text

After alt text

     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);
     }

изображение-2021-03-16-164434.png (102.8 kB)
fsggssgsdgssgd.png (136.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image undevable · Mar 16, 2021 at 02:22 PM 0
Share

@yurykuz65 Are you using URP or HDRP?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
    }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Material alpha is 1 but still see through 0 Answers

Unity 5.6: Shader Broken and Showing Up Black 0 Answers

Can't find unlit shader in material shader selection!, 1 Answer

How can i make a shader to get this effect? 0 Answers

Shader: get back scene pixel color? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges