示例#1
0
    private void Start()
    {
        ballCount = 1024 * groupCount;

        // Calculate the buffer size.
        bufferSize = groupCount * threadCount;
        particles  = new Particle[bufferSize];

        // Create compute buffer.
        buffer = new ComputeBuffer(bufferSize, sizeof(float) * particleStructSize);

        // Obtain the handle to the kernel to run.
        kernelHandle = shader.FindKernel("CSMain");

        // Generate the specified number of balls game objects.
        IObjectGenerator bg = new BallGenerator();

        balls = bg.Generate(gameObject, ballCount);

        // Generate the particles, using the positions of the ball game objects.
        Particle[] initialBufferData = new Particle[ballCount];
        for (int i = 0; i < ballCount; ++i)
        {
            Particle particle = new Particle();
            particle.mass         = 2;
            particle.position     = balls[i].transform.position;
            particle.velocity     = new Vector3(0, 10, 0);
            particle.acceleration = new Vector3(0, -10f, 0);
            particle.damping      = 0;
            initialBufferData[i]  = particle;
        }

        // Set the data.
        buffer.SetData(initialBufferData);

        // Set the buffer on the compute shader.
        shader.SetBuffer(kernelHandle, "buffer", buffer);
    }
    // Use this for initialization
    void Start()
    {
        IObjectGenerator bg = new BallGenerator();

        bg.Generate(gameObject, ballCount);
    }