Optimization, replace cubes on a surface with a texture.

I have 100k cubes, I want to replace them on a 2D surfaces with a texture. The problem is that these surfaces should always be directed towards the camera. Please tell me how to do it.

My code that generates.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class World : MonoBehaviour
{
    GameObject[] matrix;

    int xSize =  100;
    int ySize =  10;
    int zSize =  100;

    void Start()
    {
        matrix = new GameObject[xSize * ySize * zSize];
     
        for (var x = 0f; x < xSize; ++x)
        {
            for (var y = 0f; y < ySize; ++y)
            {
                for (var z = 0f; z < zSize; ++z)
                {
                    var i = (int)(x + y + z);

                    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

                    cube.transform.position = new Vector3(x * 4, y * 4, z * 4);
                 
                    matrix[i] = cube;
                }
            }
        }
    }

    void Update()
    {
    }
}

There is also a problem with a large generation time. For 1kk cubes it is about a minute.

Well you can make the cubes look at the camera using transform.LookAt(Camera.main.transform.position).

If you’re using 100,000 of anything, I strongly recommend learning to use Unity DOTS, which is designed explicitly for huge numbers of simple objects. It’s a little complicated to learn but I don’t think you’ll ever get anywhere close to reasonable performance on this project without using DOTS.

Thank you very good advice. But I still need to get away from the cubes, to surfaces with textures that are always aimed at the camera. When the camera moves, it is necessary to orient each surface, so that the angle of incidence of the beam(line of sight) is 90 degrees.

I make it

    void Update()
    {
        var pos = Camera.main.transform.position;

        foreach (var itm in matrix)
        {
            itm.transform.LookAt(pos);
        }
    }

But i have:

Only one row change direction! Here I have two questions:

  1. Why is this not working?
  2. Is it possible to do this with a shader?
  1. You must have only added the first row to your List somehow.
  2. I don’t think a shader specifically, but let me ask you this: Do these objects execute any logic? Like, do they move/etc or are they just sort of there?
    I would definitely consider making these into a particle system, which will handle the “facing the camera” thing for you, and would be much faster than having separate GameObjects.
1 Like

Yes. I want to do wave(longitudinal and transverse) modeling. There will be animation and texture changes of individual particles.

OK that sounds even more like you want to use a particle system, it supports texture animation.

I already learned how DOTS works :). And I liked it. Can a particle system limit me if I have to use ordinary geometry in the future? Could you give me a small example of how to build a 3D grid with particles?