private void InitializeBuffers()
    {
        kernel = computeShader.FindKernel("CSMain");

        // Argument buffer used by DrawMeshInstancedIndirect.
        uint[] args = new uint[5] {
            0, 0, 0, 0, 0
        };
        // Arguments for drawing mesh.
        // 0 == number of triangle indices, 1 == population, others are only relevant if drawing submeshes.
        args[0]    = (uint)mesh.GetIndexCount(0);
        args[1]    = (uint)numberOfHandAvatar;
        args[2]    = (uint)mesh.GetIndexStart(0);
        args[3]    = (uint)mesh.GetBaseVertex(0);
        argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
        argsBuffer.SetData(args);

        // Initialize buffer with the given population.
        properties = new MeshProperty[numberOfHandAvatar];

        // BigAgent
        for (int i = 0; i < numberOfHandAvatar; i++)
        {
            MeshProperty prop     = new MeshProperty();
            Vector3      position = new Vector3(Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f));
            Quaternion   rotation = Quaternion.Euler(Random.Range(-180, 180), Random.Range(-180, 180), Random.Range(-180, 180));
            Vector3      scale    = new Vector3(5.0f, 5.0f, 5.0f);

            prop.mat      = Matrix4x4.TRS(position, rotation, scale);
            prop.color    = new Color(0.0f, 0.0f, 0.0f);
            prop.velocity = new Vector3(0.0f, 0.0f, 0.0f);

            properties[i] = prop;
        }

        meshPropertiesBuffer = new ComputeBuffer(numberOfHandAvatar, MeshProperty.Size());
        meshPropertiesBuffer.SetData(properties);
        computeShader.SetBuffer(kernel, "_Properties", meshPropertiesBuffer);
        material.SetBuffer("_Properties", meshPropertiesBuffer);
    }
Esempio n. 2
0
    private void InitializeBuffers()
    {
        kernel = computeShader.FindKernel("CSMain");

        // Argument buffer used by DrawMeshInstancedIndirect.
        uint[] args = new uint[5] {
            0, 0, 0, 0, 0
        };
        // Arguments for drawing mesh.
        // 0 == number of triangle indices, 1 == population, others are only relevant if drawing submeshes.
        args[0]    = (uint)mesh.GetIndexCount(0);
        args[1]    = (uint)population;
        args[2]    = (uint)mesh.GetIndexStart(0);
        args[3]    = (uint)mesh.GetBaseVertex(0);
        argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
        argsBuffer.SetData(args);

        // Initialize buffer with the given population.
        properties = new MeshProperty[population];

        // BigAgent
        for (int i = 0; i < numberOfBigAgent; i++)
        {
            MeshProperty prop     = new MeshProperty();
            Vector3      position = new Vector3(Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f));
            Quaternion   rotation = Quaternion.Euler(Random.Range(-180, 180), Random.Range(-180, 180), Random.Range(-180, 180));
            Vector3      scale    = new Vector3(0.2f, 0.2f, 0.2f);

            prop.mat      = Matrix4x4.TRS(position, rotation, scale);
            prop.color    = Color.HSVToRGB(i * (1.0f / numberOfBigAgent), 1.0f, 1.0f);
            prop.velocity = new Vector3(Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f));

            properties[i] = prop;
        }

        // Agent
        for (int i = numberOfBigAgent; i < population - numberOfBigAgent; i++)
        {
            MeshProperty prop     = new MeshProperty();
            Vector3      position = new Vector3(Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f), Random.Range(-range / 2.0f, range / 2.0f));
            Quaternion   rotation = Quaternion.Euler(Random.Range(-180, 180), Random.Range(-180, 180), Random.Range(-180, 180));
            Vector3      scale    = new Vector3(0.1f, 0.1f, 0.1f);

            prop.mat   = Matrix4x4.TRS(position, rotation, scale);
            prop.color = Color.HSVToRGB(Random.Range(0.1f, 0.4f), Random.Range(0.4f, 0.6f), 1);
            // prop.color = new Color(0.0f, 0.5f, 0.0f, 0.5f);
            prop.velocity = new Vector3(Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f));

            properties[i] = prop;
        }

        meshPropertiesBuffer = new ComputeBuffer(population, MeshProperty.Size());
        meshPropertiesBuffer.SetData(properties);
        computeShader.SetBuffer(kernel, "_Properties", meshPropertiesBuffer);
        material.SetBuffer("_Properties", meshPropertiesBuffer);

        // For Debug.
        //currentVelocityArray = new Vector3[population];
        //currentVelocityBuffer = new ComputeBuffer(population, 12);
        //currentVelocityBuffer.SetData(currentVelocityArray);
        //computeShader.SetBuffer(kernel, "_currentVelocityBuffer", currentVelocityBuffer);

        //cohVelocityArray = new Vector3[population];
        //cohVelocityBuffer = new ComputeBuffer(population, 12);
        //cohVelocityBuffer.SetData(cohVelocityArray);
        //computeShader.SetBuffer(kernel, "_cohVelocityBuffer", cohVelocityBuffer);

        //alignVelocityArray = new Vector3[population];
        //alignVelocityBuffer = new ComputeBuffer(population, 12);
        //alignVelocityBuffer.SetData(alignVelocityArray);
        //computeShader.SetBuffer(kernel, "_alignVelocityBuffer", alignVelocityBuffer);

        //avoidVelocityArray = new Vector3[population];
        //avoidVelocityBuffer = new ComputeBuffer(population, 12);
        //avoidVelocityBuffer.SetData(avoidVelocityArray);
        //computeShader.SetBuffer(kernel, "_avoidVelocityBuffer", avoidVelocityBuffer);
    }