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
1
Question by leonalchemist · May 28, 2014 at 10:30 PM · randomdraw callschange color

Random color on object [Solved]

Been asking this for a bit now and not getting any luck so to ask as simply as possible.

How can i have a cube that gets a random color on start without increasing draw calls?

I've tried shaders, and script but getting mesh leaked as the changes are executed in the editor, tried bunch of other ideas and no luck. I know how to get a random color but doing it without increasing draw calls or getting mesh leak is the problem i've been having for weeks now.

Comment
Add comment · Show 8
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 CHPedersen · May 28, 2014 at 10:39 PM 0
Share

I'm not sure I totally understand why this is complex. You say you know how to generate a random color. What exactly stops you from creating a prefab out of the default cube, then slapping a script onto it with a Start method, nothing more, and then in the start method, you generate a random color and set the renderer.material.color?

Then that prefab is going to spawn a cube and immediately assign a random color on Start. That's all I see to it?

avatar image leonalchemist · May 28, 2014 at 10:55 PM 0
Share

You kinda missed half there question there, thats would 'work' except it would increase draw for each gameObject making it extremely inefficient.

avatar image itsa · May 28, 2014 at 11:05 PM 0
Share

Have you tried to make a shader like the 2d sprites have and change the vertex color on Start?

avatar image leonalchemist · May 28, 2014 at 11:12 PM 0
Share

dunno how to code shaders at all, but did find a shader that lets you change vertex color in script, does work but this lead to having mesh leaks.

I am doing this with ExecuteInEdit$$anonymous$$ode i the OnGUI() function which leads to mesh leaks; and tried to use the script on game start but says:

Not allowed to access vertices/colors on mesh 'Combined $$anonymous$$esh (root: scene) Instance'

avatar image Kiwasi · May 29, 2014 at 12:38 AM 0
Share

Can you simply create your prefab with the renderer disabled, then in start set the colour on the renderer and enable the renderer?

Show more comments

4 Replies

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

Answer by leonalchemist · Jun 03, 2014 at 02:11 PM

This works (thanks zharik86) [Quick edit in code, had one line too much]

You can choose te color u wish with the color picker tool and set a random value for how far apart each brick color will be on the dark/light scale

 private var newColor : Color;
 public var colorPicker : Color;
 public var randomizer : float = 0.1;
 
 function Awake () {
     var randomColor : float = Random.Range(-randomizer, randomizer);
     newColor.r = colorPicker.r + randomColor;
     newColor.g = colorPicker.g + randomColor;
     newColor.b = colorPicker.b + randomColor;
     
     var mesh : Mesh = GetComponent(MeshFilter).mesh;
     var vertices : Vector3[] = mesh.vertices;
     var colors : Color[] = new Color[vertices.Length];
     var i : int = 0;
     while (i < vertices.Length) {
         colors[i] = newColor;
         i++;
     }
     mesh.colors = colors;
 }

alt text

1 Draw call for the whole room :)


1.jpg (130.7 kB)
Comment
Add comment · Show 1 · 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 zharik86 · Jun 03, 2014 at 06:58 PM 0
Share

I am glad that everything works for you. Cool picture:) . Always at your service.

avatar image
2

Answer by zharik86 · May 29, 2014 at 08:03 AM

When you create new color, thereby you create a new material. Therefore draw call increase. Dynamic batching doesn't work because of different materials. And so, if to try to use a standard shader (for example, Diffuse). But for each vertices in advance to set its color. For example I write code(write on CSharp):

  void Start() {
   //Create random color
   Color col1 = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);
   //Find mesh from game objects
   Mesh mesh1 = GetComponent<MeshFilter>().mesh;
   //Change colors of meshes
   Vector3[] vertices = mesh1.vertices;
   Color[] colors = new Color[vertices.Length];
   for(int i = 0; i < vertices.Length; i++) {
    colors[i] = col1;
   }
   mesh1.colors = colors; //Set new colors of vertices
  }

And using for your objects simple shader:

  Shader "Custom/SimpleColor" {
   Properties {
    _MainTex { "Base (RGB)", 2D) = "white" {}
   }
   Category {
    Tags { "Queue" = "Geometry" }
    Lighting off

    Bindchanel {
     Bind "Color", color
     Bind "Vertex", vertex
     Bind "Texcoord", Texcoord
    }

    SubShader {
     Pass {
      SetTexture [_MainTex] {
       Combine texture * primary
      }
     }
    }
   }
  }

I hope it to you will help. Itself I tested, at me all cubes are drawn for one draw call.

P.S.: If you need use shader with light, simple write to me.

Comment
Add comment · Show 9 · 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 leonalchemist · Jun 01, 2014 at 11:44 AM 0
Share

Tells me: "Not allowed to access vertices/colors on mesh 'Combined $$anonymous$$esh (root: scene) Instance' UnityEngine.$$anonymous$$esh:get_vertices()"

and objects go invisible in game??

avatar image zharik86 · Jun 01, 2014 at 12:02 PM 0
Share

With color of vertices you can look at an operation example at Unity documentation. I always corresponded to it. Also I checked for standard cubes. I didn't use Combine$$anonymous$$esh for cubes as for them dynamic batching normally works. Probably you should change a line where $$anonymous$$esh is defined on:

  $$anonymous$$esh mesh1 = GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh;

avatar image leonalchemist · Jun 02, 2014 at 01:40 AM 0
Share

hang on, had to change a few things so it would work but realize this is a simple version of what i already have T_T

i already have a simple shader that changes vertex colors and already have a script that randomized colors on a cube. The problem i have is i want every cube to have a different random color which is why i cant use shared$$anonymous$$esh and why im having this problem on the first place

avatar image zharik86 · Jun 02, 2014 at 06:46 AM 0
Share

@leonalchemist In a shader it isn't necessary to change colors of vertices. They should be changed programmatically as I wrote. Then draw call won't increase. I tested that wrote. I created 4 cubes on a scene. I attached to them a script and a material on the basis of a shader. After I started my scene. Everything works perfectly. (I use Unity 4.1.2). So it isn't clear why for you doesn't work. Or try to change you mesh in Editor?

avatar image zharik86 · Jun 03, 2014 at 07:37 AM 1
Share

@leonalchemist Well. I sent the project to your email. I made there 6 cubes which are drawn for one draw call (perfectly works dynamic batching). On a scene (TestScene) there is a light source. Also I made two shaders: without light and with light. In a material I decided to use a shader with light (so more visually). Also I made GUI Button in the upper left corner. When clicking the button there is a change of color of cubes. Launch a scene (Play) and everything will work. All scene is drawn for 3 draw call: 2 draw call - button, 1 draw call - 6 cubes.

I hope, you write me your comments on this my project.

Show more comments
avatar image
0

Answer by Aladine · May 29, 2014 at 12:37 AM

Can this help ? (apply the script to a cube with a material that allow color changing, diffuse for example)

  using UnityEngine;
     using System.Collections;
     
     public class RandomColor : MonoBehaviour
     {
             //set X & Y values between 0.0 and 1.0 to have more control 
             //(complete random color are ugly)
             public Vector2 redRange, greenRange, blueRange, alphaRange;
             
             //the new color 
             Color color = new Color (0, 0, 0, 0);
             
             void Start ()
             {
                     //randomize
                     color.r = Random.Range (redRange.x, redRange.y);
                     color.g = Random.Range (greenRange.x, greenRange.y);
                     color.b = Random.Range (blueRange.x, blueRange.y);
                     color.a = Random.Range (alphaRange.x, alphaRange.y);
                     //apply
                     renderer.material.color = color;
             }
         
             
     }
     
Comment
Add comment · Show 5 · 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 itsa · May 29, 2014 at 12:51 AM 0
Share

i was just about to post the solution, but you were faster :) this should be 2 draw calls

avatar image itsa · May 29, 2014 at 01:00 AM 0
Share

increases draw calls for every instance...

avatar image itsa · May 29, 2014 at 01:12 AM 0
Share

he needs something like the sprite renderer for 3d... just tried with sprite-default shader and it doesnt increase draw calls

avatar image leonalchemist · May 29, 2014 at 01:14 AM 0
Share

sry but nope, same problem as mentioned above in bold; already tried a script similar to this; color changes but i dont want draw call to increase for each object this script is on

@itsa im making a 3D game so i dont think using sprite shader would work right?

avatar image itsa · May 29, 2014 at 08:10 AM 0
Share

it wouldn't. but at least you know where to dig. sprite shader just uses vertex colors, but the sprite renderer uses the same material for all instances of the same shader, with variable color. maybe some more experienced user knows how its done.

avatar image
0

Answer by Split3 · Jan 13, 2018 at 08:54 PM

There is a code Random.ColorHSV for shorter line. You can read about it in unity documentary

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

27 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

Related Questions

¿how to take all values from an array without order? 2 Answers

Using arrays to randomly select an object 0 Answers

Random Range Seems... Unrandom 1 Answer

Random object placement. 1 Answer

Randomly Place Cubes In Viewport Without Overlap 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