Exemplo n.º 1
0
    void Update()
    {
        if (m_particlesBuffer == null)
        {
            m_particlesBuffer = new ComputeBuffer[2];
            for (int i = 0; i < 2; ++i)
            {
                m_particlesBuffer[i] = new ComputeBuffer(m_nWidth * m_nHeight, 4, ComputeBufferType.Default);
            }
        }

        m_particleAreaShader.SetFloat("_Dissipation", m_densityDissipation);
        m_particleAreaShader.SetFloat("_ElapsedTime", Time.deltaTime);
        m_particleAreaShader.SetFloat("_Speed", m_simulation.GetSimulationSpeed());

        // ADVECT (Particles)
        m_particleAreaShader.SetBuffer(m_advectKernel, "_Obstacles", m_simulation.ObstaclesBuffer);
        m_particleAreaShader.SetBuffer(m_advectKernel, "_Velocity", m_simulation.VelocityBuffer);
        m_particleAreaShader.SetBuffer(m_advectKernel, "_ParticlesIn", m_particlesBuffer[READ]);
        m_particleAreaShader.SetBuffer(m_advectKernel, "_ParticlesOut", m_particlesBuffer[WRITE]);
        m_particleAreaShader.Dispatch(m_advectKernel, m_nNumGroupsX, m_nNumGroupsY, 1);
        FlipBuffers();

        if (m_colourRamp == null)
        {
            m_colourRamp = new ComputeBuffer(m_nColourRampSize, 16, ComputeBufferType.Default);
            UpdateGradient();
        }

        if (m_updateGradient)
        {
            if (gradient1 == true)
            {
                UpdateGradient();
            }
            if (gradient1 == false)
            {
                UpdateGradient2();
            }
        }

        //RENDER
        GetComponent <Renderer>().material.SetBuffer("_Particles", m_particlesBuffer[READ]);
        GetComponent <Renderer>().material.SetBuffer("_ColourRamp", m_colourRamp);
        GetComponent <Renderer>().material.SetVector("_Size", new Vector2(m_nWidth, m_nHeight));

        // Cache particles
        if (m_CacheParticles)
        {
            m_particlesBuffer[READ].GetData(m_currentParticles);
        }
    }