private void Start() { // A copy paste of https://github.com/Scrawk/GPU-GEMS-2D-Fluid-Simulation // Genius who ever wrote that. Some changes to adapt for my use. _width = 500; _height = 500; var size = new Vector2(_width, _height) / 5; var pos = new Vector2(Screen.width / 2, Screen.height / 2) - size * 0.5f; _rect = new Rect(pos, size); _inverseSize = new Vector2(1.0f / _width, 1.0f / _height); _velocityTex = new RenderTextureRotating(_width, _height, RenderTextureFormat.RGFloat, FilterMode.Bilinear); _densityTex = new RenderTextureRotating(_width, _height, RenderTextureFormat.RFloat, FilterMode.Bilinear); _temperatureTex = new RenderTextureRotating(_width, _height, RenderTextureFormat.RFloat, FilterMode.Bilinear); _pressureTex = new RenderTextureRotating(_width, _height, RenderTextureFormat.RFloat, FilterMode.Point); _guiTex = new RenderTexture(_width, _height, 0, RenderTextureFormat.ARGB32) { filterMode = FilterMode.Bilinear, wrapMode = TextureWrapMode.Clamp }; _guiTex.Create(); _divergenceTex = new RenderTexture(_width, _height, 0, RenderTextureFormat.RFloat) { filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp }; _divergenceTex.Create(); _obstaclesTex = new RenderTexture(_width, _height, 0, RenderTextureFormat.RFloat) { filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp }; _obstaclesTex.Create(); }
private void Start() { _adventMain = AdventShader.FindKernel("Advent"); _buoyancyMain = BuoyancyShader.FindKernel("Buoyancy"); _impulseMain = ImpulseShader.FindKernel("Impulse"); _divergenceMain = DivergenceShader.FindKernel("Divergence"); _jacobiMain = JacobiShader.FindKernel("Jacobi"); _subtractMain = SubtractGradientShader.FindKernel("Subtract"); _clearMain = ClearShader.FindKernel("ClearBuffer"); _tilemap = TileMapObject.GetComponent <Tilemap>(); _mapBounds = _tilemap.cellBounds; _mapSize = new Vector2Int(_mapBounds.size.x, _mapBounds.size.y); // Calculate obstacle locations var walls = new float4[_mapSize.x, _mapSize.y]; foreach (var position in _mapBounds.allPositionsWithin) { walls[position.x - _mapBounds.xMin, position.y - _mapBounds.yMin] = new float4(100); if (position.x == _mapBounds.xMin || position.x == _mapBounds.xMax || position.y == _mapBounds.yMin || position.y == _mapBounds.yMax) { continue; // Even if border tile is a simulate atmo tile, it will be defined as a wall. } if (SimulateAtmoTiles.Contains(_tilemap.GetTile(position))) { walls[position.x - _mapBounds.xMin, position.y - _mapBounds.yMin] = new float4(-100); } } _linearMapSize = _mapSize.x * _mapSize.y; _velocity = new RenderTextureRotating(_linearMapSize, ComputeBufferMode.Dynamic); _densityTemp = new RenderTextureRotating(_linearMapSize); _pressure = new RenderTextureRotating(_linearMapSize); _walls = new ComputeBuffer(_linearMapSize, 4 * sizeof(float), ComputeBufferType.Structured, ComputeBufferMode.SubUpdates); _walls.SetData(walls); _divergence = new ComputeBuffer(_linearMapSize, 4 * sizeof(float), ComputeBufferType.Structured, ComputeBufferMode.SubUpdates); }