public void CreateData(FluidComponentManager manager, WorldApi worldApi)
        {
            _helperIndicesList = new NativeList <VectorI3>(Unity.Collections.Allocator.Persistent);

            _worldApiPointer = GCHandle.Alloc(worldApi, GCHandleType.Pinned);
            _managerPointer  = GCHandle.Alloc(manager, GCHandleType.Pinned);
        }
        public void Execute()
        {
            Block block = (Block)BlockPointer.Target;

            // unsettle chunks
            for (int i = 0; i < ChunksToUnsettle.Length; i++)
            {
                block.Chunks[ChunksToUnsettle[i]].Unsettle();
            }

            ChunksToUnsettle.Clear();

            // add just settled voxels to components
            FluidComponentManager componentManager = (FluidComponentManager)ComponentManagerPointer.Target;

            lock (componentManager.hashSetLock)
            {
                for (int i = 0; i < VoxelsToProcess.Length; i++)
                {
                    componentManager.VoxelsToProcess.Add(VoxelsToProcess[i]);
                }
            }

            VoxelsToProcess.Clear();

            // prepare for the next iteration
            FluidBlockSimData simData = block.SimData;

            simData.SwapArrays();
        }
        public void Execute()
        {
            WorldApi worldApi             = (WorldApi)_worldApiPointer.Target;
            FluidComponentManager manager = (FluidComponentManager)_managerPointer.Target;

            TryCreateComponents(worldApi, manager);
            TryRemoveComponents(worldApi, manager.Components, manager.RebuildEnabled);
        }
Exemplo n.º 4
0
        public void CreateData(WorldApi worldApi, FluidComponentManager manager, FluidComponent component)
        {
            IsRunning = true;
            ToRemove  = false;

            _helperIndicesList  = new NativeList <VectorI3>(Unity.Collections.Allocator.Persistent);
            _helperDictKeysList = new NativeList <Vector2>(Unity.Collections.Allocator.Persistent);

            _componentPointer = GCHandle.Alloc(component, GCHandleType.Pinned);
            _worldApiPointer  = GCHandle.Alloc(worldApi, GCHandleType.Pinned);
            _managerPointer   = GCHandle.Alloc(manager, GCHandleType.Pinned);
        }
Exemplo n.º 5
0
        public void Create(FluidComponentManager componentManager)
        {
            UnityEngine.Profiling.Profiler.BeginSample("CreateSimData");

            _writeIsRead             = false;
            _writeVoxels             = new NativeArray <Voxel>(ReadVoxels, Unity.Collections.Allocator.Persistent);
            _chunksToUnsettle        = new NativeList <int>(Unity.Collections.Allocator.Persistent);
            _voxelsToProcess         = new NativeList <VectorI3>(Unity.Collections.Allocator.Persistent);
            _emptyHelperArray        = new NativeArray <Voxel>(0, Unity.Collections.Allocator.Persistent);
            _blockPointer            = GCHandle.Alloc(_block, GCHandleType.Pinned);
            _componentManagerPointer = GCHandle.Alloc(componentManager, GCHandleType.Pinned);

            UnityEngine.Profiling.Profiler.EndSample();
        }
        public void TryCreateComponents(WorldApi worldApi, FluidComponentManager manager)
        {
            UnityEngine.Profiling.Profiler.BeginSample("TryCreateComponents");

            // first try to add voxel to a nearby existing component because creating a new one is expensive
            int processed = 0;

            foreach (VectorI3 indices in manager.VoxelsToProcess)
            {
                if (TryAddToExistingComponent(worldApi, manager, in indices))
                {
                    _helperIndicesList.Add(indices);
                }

                if (processed++ > FluidComponentManager.kMaxVoxelsProcessedPerIteration)
                {
                    break;
                }
            }

            manager.VoxelsToProcess.RemoveRange(_helperIndicesList);
            _helperIndicesList.Clear();

            // no nearby existing component found
            if (manager.VoxelsToProcess.Count > FluidComponentManager.kMinComponentSize)
            {
                // try to create a single new component
                foreach (VectorI3 indices in manager.VoxelsToProcess)
                {
                    if (TryCreateNewComponent(worldApi, manager, in indices))
                    {
                        manager.VoxelsToProcess.Remove(indices);
                    }

                    break;
                }
            }

            UnityEngine.Profiling.Profiler.EndSample();
        }
Exemplo n.º 7
0
        // TODO speedup
        /// <summary>
        /// Validates the existing segments and updates / equalizes the outlets of this component.
        /// </summary>
        public void Execute()
        {
            UnityEngine.Profiling.Profiler.BeginSample("Update");

            FluidComponent        component = (FluidComponent)_componentPointer.Target;
            WorldApi              worldApi  = (WorldApi)_worldApiPointer.Target;
            FluidComponentManager manager   = (FluidComponentManager)_managerPointer.Target;

            int diff = component.Count;

            if (component.Viscosity > FluidComponentManager.kMaxViscosityNotEqualize && component.Outlets == null)
            {
                component.Outlets = new HashSet <VectorI3>();
            }

            UpdateSegments(worldApi, component.AllSegments, component.Outlets, ref component.Count, ref component.WaterLevel);

            diff -= component.Count;

            if (diff != 0)
            {
                component.Unsettle(diff * component.Viscosity);
            }
            else
            {
                component.DecreaseSettle();
            }

            if (component.Viscosity > FluidComponentManager.kMaxViscosityNotEqualize && component.Outlets.Count > 0)
            {
                UpdateOutlets(worldApi, component.Outlets, component.Rebuilding, component.Lifetime, ref component.WaterLevel);

                EqualizeOutlets(true, worldApi, component.Outlets, component.Viscosity);
                EqualizeOutlets(false, worldApi, component.Outlets, component.Viscosity);
            }

            UnityEngine.Profiling.Profiler.EndSample();
        }
 /// <summary>
 /// Try to create a new component out of given voxel indices.
 /// </summary>
 private bool TryCreateNewComponent(WorldApi worldApi, FluidComponentManager manager, in VectorI3 indices)