示例#1
0
        private void FixedUpdate()
        {
            // Check here for any wall changes.
            // DOES NOT WORK. No clue why the compute buffers are not reading and writing correctly. Oh well.
            // Good education though. I now understand what it's doing.

            var timeStep = Time.deltaTime * 1;

            // Advect velocity against its self
            Advent(_velocity.Current, _velocity.Past, timeStep);
            // Advect temperature and density against velocity
            Advent(_densityTemp.Current, _densityTemp.Past, timeStep);

            _velocity.Swap();
            _densityTemp.Swap();

            Debug.Log("READ?");
            Debug.Break();

            // Apply forces here.
            ApplyBuoyancy(_velocity.Current, _densityTemp.Past, _velocity.Past, timeStep);

            _velocity.Swap();

            // Refresh the impulse of density and temperature
            ApplyImpulseToDensityTemp(new Vector2(0, 28), 4);

            _densityTemp.Swap();

            // Calculates how divergent the velocity is
            ComputeDivergence(_divergence, _velocity.Past);

            // Clearing pressure pressure for Jacobi loops.
            ClearBuffer(_pressure.Past);

            for (var i = 0; i < 50; ++i)
            {
                Jacobi(_pressure.Current, _pressure.Past, _divergence);
                _pressure.Swap();
            }

            SubtractGradient(_velocity.Current, _pressure.Past, _velocity.Past);
            _velocity.Swap();
        }
示例#2
0
        private void FixedUpdate()
        {
            //Obstacles only need to be added once unless changed.
            AddObstacles();

            //Set the density field and obstacle color.
            GUIMat.SetColor("_FluidColor", FluidColor);
            GUIMat.SetColor("_ObstacleColor", ObstacleColor);

            var dt = Time.fixedDeltaTime * 10;

            //Advect velocity against its self
            Advect(_velocityTex.Read, _velocityTex.Read, _velocityTex.Write, VelocityDissipation, dt);
            //Advect temperature against velocity
            Advect(_velocityTex.Read, _temperatureTex.Read, _temperatureTex.Write, TemperatureDissipation, dt);
            //Advect density against velocity
            Advect(_velocityTex.Read, _densityTex.Read, _densityTex.Write, DensityDissipation, dt);

            _velocityTex.Swap();
            _temperatureTex.Swap();
            _densityTex.Swap();

            //Determine how the flow of the fluid changes the velocity
            ApplyBuoyancy(_velocityTex.Read, _temperatureTex.Read, _densityTex.Read, _velocityTex.Write, dt);

            _velocityTex.Swap();

            //Refresh the impulse of density and temperature
            ApplyImpulse(_temperatureTex.Read, _temperatureTex.Write, _implusePos, ImpulseRadius, ImpulseTemperature);
            ApplyImpulse(_densityTex.Read, _densityTex.Write, _implusePos, ImpulseRadius, ImpulseDensity);

            _temperatureTex.Swap();
            _densityTex.Swap();

            //If left click down add impulse, if right click down remove impulse from mouse pos.
            // if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
            // {
            //     Vector2 pos = Input.mousePosition;
            //
            //     pos.x -= _rect.xMin;
            //     pos.y -= _rect.yMin;
            //
            //     pos.x /= _rect.width;
            //     pos.y /= _rect.height;
            //
            //     var sign = Input.GetMouseButton(0) ? 1.0f : -1.0f;
            //
            //     ApplyImpulse(_temperatureTex.Read, _temperatureTex.Write, pos, MouseImpulseRadius, ImpulseTemperature);
            //     ApplyImpulse(_densityTex.Read, _densityTex.Write, pos, MouseImpulseRadius, ImpulseDensity * sign);
            //
            //     _temperatureTex.Swap();
            //     _densityTex.Swap();
            // }

            //Calculates how divergent the velocity is
            ComputeDivergence(_velocityTex.Read, _divergenceTex);

            ClearSurface(_pressureTex.Read);

            for (var i = 0; i < NumJacobiIterations; ++i)
            {
                Jacobi(_pressureTex.Read, _divergenceTex, _pressureTex.Write);
                _pressureTex.Swap();
            }

            //Use the pressure tex that was last rendered into. This computes divergence free velocity
            SubtractGradient(_velocityTex.Read, _pressureTex.Read, _velocityTex.Write);

            _velocityTex.Swap();

            //Render the tex you want to see into gui tex. Will only use the red channel
            GUIMat.SetTexture("_Obstacles", _obstaclesTex);
            Graphics.Blit(_densityTex.Read, _guiTex, GUIMat);
        }