- Home /
Rendering 100000+ of simple objects whose colour I can control
I have created a large cubic lattice of thousands of cubes (or spheres), and I want to procedurally control their colour (so I can, for example, make it appear that a wave of colour moves through the lattice via controlling the cubes' colours). Here is the code:
void Start () {
//initialize cube field
cubes = new GameObject[cubeFieldSize [0], cubeFieldSize [1], cubeFieldSize [2]];
for (int i = 0; i < cubeFieldSize[0]; i++) {
for (int j = 0; j < cubeFieldSize[1]; j++) {
for (int k = 0; k < cubeFieldSize[2]; k++) {
//instantiate array of objects (cubes or spheres)
if(preferSpheres){
cubes[i,j,k] = (GameObject)Instantiate (sphere, transform1.position, Quaternion.identity);
}
else{
cubes[i,j,k] = (GameObject)Instantiate (cube, transform1.position, Quaternion.identity);
}
//make children of parent
cubes[i,j,k].transform.parent = transform;
//set up positions of objects
cubes[i,j,k].transform.position = new Vector3 ((i-cubeFieldSize[0]/2+0.5f)*cubeSpacing,
(j-cubeFieldSize[1]/2+0.5f)*cubeSpacing,
(k-cubeFieldSize[2]/2+0.5f)*cubeSpacing);
//set up scale of objects
cubes[i,j,k].transform.localScale = new Vector3 (cubeSize,cubeSize,cubeSize);
}
}
}
}
I am controlling the colour of the objects using
cubes[i,j,k].GetComponent<Renderer>().material.color = colours[m];
My problem is that I would like to render a lot of objects, but of course the larger the number the more it slows. Is there a more efficient way to render 100,000+ objects?
I thought maybe I could use a particle system, since I don't really need to draw entire meshes for my purpose, just circles or lights would do?
Or I saw one Unity Answer that talked about procedural meshes?
Any suggestion of how these might be implemented in my case, with the colours being controlled, and so I can draw many more objects than I can at the moment, would be much appreciated.
Yes, if the objects don't need to move, then a procedural mesh is probably a good idea. The Unity Scripting API for the mesh class gives you the required information, and there's a sample script on the User Wiki for creating planes on the fly. All you have left to do is change the mesh colors, and use a shader that supports vertex colors.
Answer by TiPE_intuity · Nov 05, 2015 at 04:42 PM
As you suggest a particle system appears to be the best solution. Particles are way simpler to calculate for unity because they are missing most of the attributes gameObjects have. Additionally particles can be controlled over their system in terms of position, movement, size etc.
You could use your structure to write the positions and sizes in an array which can be handed to the particle system in the next step, for all particles at once. (Same is true for color)
One disadvantage of particles is, that they usually have no depth. You can image a particle as a flat image, which always rotates to face the camera.
Your answer
Follow this Question
Related Questions
Text Update demands too much performance 0 Answers
Shadow artifects on mesh with Standard shader 1 Answer
Performance with a 250x250 grid 0 Answers
Trouble with Occlusion Culling 0 Answers
Particles not rendering correctly 1 Answer