/// <summary>
        /// Updates the Engine with a change in time.
        /// This call will block all access to the engine while it is running.
        /// A complete call to this method is also known as a timestep.
        /// </summary>
        /// <param name="dt">The change in time since the last call to this method. (In Seconds)</param>
        public void Update(Scalar dt)
        {
            if (dt < 0)
            {
                throw new ArgumentOutOfRangeException("dt");
            }
            CheckState();
            rwLock.EnterWrite();
            TimeStep step = new TimeStep(dt, updateCount++);

            inUpdate = true;
            try
            {
                RemoveExpired();
                AddPending();
                if (logicsNeedSorting)
                {
                    logicsNeedSorting = false;
                    logics.Sort(logicComparer);
                }
                UpdateTime(step);
                solver.Solve(step);
                OnPositionChanged();
            }
            finally
            {
                inUpdate = false;
                rwLock.ExitWrite();
            }
        }
Exemple #2
0
    public void FixedUpdate()
    {
        // Solve collisions only for obbs (not particles)
        collisionSolver.Solve();

        // Update particle system
        particleSystem.Update(Constants.TimeStepSeconds);

        // Solve collisions only for particles
        collisionSolver.Solve(ref particleSystem.Particles);

        // Do simulation
        fluidSim.Calculate(ref particleSystem.Particles, lGravity, Constants.TimeStepSeconds);

        if (fillTypeIndex == 1)
        {
            renderer.GetComponent <MeshRenderer>().enabled = true;
            Mesh mesh = generator.GenerateMesh(fluidSim.m_grid.getFluidMapCount(), Constants.CellSpace / 3);
            filter.mesh = mesh;
        }
        else if (fillTypeIndex == 0)
        {
            renderer.GetComponent <MeshRenderer>().enabled = false;
            // align Unity Particles with simulated Particles ...
            int d = particleSystem.Particles.Count - ps.particleCount;
            if (d > 0)
            {
                ps.Emit(d);
            }

            ps.GetParticles(particles);
            mParticle p;
            for (int i = 0; i < particleSystem.Particles.Count; i++)
            {
                p = particleSystem.Particles[i];
                particles[i].position          = new Vector3(p.Position.x, p.Position.y, 0);
                particles[i].remainingLifetime = 1f;
            }
            ps.SetParticles(particles, particleSystem.MaxParticles);
        }
        else
        {
            //update particle buffer
            smallParticle[] particleArray = new smallParticle[particleSystem.Particles.Count];
            for (int i = 0; i < particleSystem.Particles.Count; i++)
            {
                particleArray[i] = particleSystem.Particles[i].toE();
            }
            particleBuffer.SetData(particleArray);
            // Bind the ComputeBuffer to the shader and the compute shader
            computeShader.SetBuffer(mComputeShaderKernelID, "particleBuffer", particleBuffer);
            computeShader.SetInt("particle_length", particleSystem.Particles.Count);
            int mWarpCount = Mathf.CeilToInt((float)fluidSim.m_grid.GPUVoxel / Constants.WARP_SIZE);
            computeShader.Dispatch(mComputeShaderKernelID, mWarpCount, 1, 1);
            grid[] gridArray = new grid[fluidSim.m_grid.GPUVoxel];
            gridBuffer.GetData(gridArray);
            int[] field = new int[gridArray.GetLength(0)];
            for (int i = 0; i < gridArray.GetLength(0); i++)
            {
                if (gridArray[i].value > 0)
                {
                    field[i] = 1;
                }
            }

            int[,] f = Helper.Make2DArray <int>(field, fluidSim.m_grid.Width * Constants.resolution + 1, fluidSim.m_grid.Height * Constants.resolution + 1);
            renderer.GetComponent <MeshRenderer>().enabled = true;
            Mesh mesh = generator.GenerateMesh(f, Constants.CellSpace / Constants.resolution);
            mesh        = mattatz.MeshSmoothingSystem.MeshSmoothing.LaplacianFilter(mesh);
            filter.mesh = mesh;
        }
    }