// Height jobs for the next frame
        public static void UpdateHeights()
        {
            if (_processing)
            {
                return;
            }

            _processing = true;

            // Buoyant Object Job
            var waterHeight = new HeightJob()
            {
                WaveData     = _waveData,
                Position     = _positions,
                OffsetLength = new int2(0, _positions.Length),
                Time         = Time.time,
                OutPosition  = _wavePos,
                OutNormal    = _waveNormal
            };

            _waterHeightHandle = waterHeight.Schedule(_positionCount, 32);

            JobHandle.ScheduleBatchedJobs();

            _firstFrame = false;
        }
Exemple #2
0
        /// <summary>
        /// Run the jobs
        /// </summary>
        /// <returns>True if jobs kicked off, false if jobs already running.</returns>
        public static bool ScheduleJobs()
        {
            if (s_jobsRunning)
            {
                return(false);
            }

            if (s_lastQueryIndexHeights == 0)
            {
                // Nothing to do
                return(true);
            }

            s_jobsRunning = true;

            var heightJob = new HeightJob()
            {
                _waveNumbers    = s_waveNumbers,
                _amps           = s_amps,
                _windDirX       = s_windDirX,
                _windDirZ       = s_windDirZ,
                _phases         = s_phases,
                _chopAmps       = s_chopAmps,
                _numWaveVecs    = _waveVecCount,
                _queryPositions = s_queryPositionsHeights,
                _computeSegment = new int2(0, s_queryPositionsHeights.Length),
                _time           = OceanRenderer.Instance.CurrentTime,
                _outHeights     = s_resultHeights,
                _seaLevel       = OceanRenderer.Instance.SeaLevel,
            };

            s_handleHeights = heightJob.Schedule(s_lastQueryIndexHeights, 32);

            JobHandle.ScheduleBatchedJobs();

            s_firstFrame = false;

            return(true);
        }
    // calculate global Heights
    public HeightData[] CalculateHeights()
    {
        // output data
        var heights = new HeightData[_totalBlockNumberX * _totalBlockNumberZ];

        var heightJob = new HeightJob()
        {
            // input
            TotalBlockNumberX = _totalBlockNumberX,

            // output
            Result = new NativeArray <HeightData>(heights, Allocator.TempJob)
        };

        var heightJobHandle = heightJob.Schedule(_totalBlockNumberX * _totalBlockNumberZ, 8);

        heightJobHandle.Complete();
        heightJob.Result.CopyTo(heights);

        // cleanup
        heightJob.Result.Dispose();

        return(heights);
    }
Exemple #4
0
        /// <summary>
        /// Calculates global heights. This method uses Unity Job System.
        /// Each column described by its x and z values has three heights.
        /// One for Bedrock, Stone and Dirt. Heights determines up to where certain types appear.
        /// x is Bedrock, y is Stone and z is Dirt.
        /// </summary>
        internal static ReadonlyVector3Int[] CalculateHeightsJobSystem()
        {
            // output data
            var heights = new ReadonlyVector3Int[TotalBlockNumberX * TotalBlockNumberZ];

            var heightJob = new HeightJob()
            {
                // input
                TotalBlockNumberX = TotalBlockNumberX,

                // output
                Result = new NativeArray <ReadonlyVector3Int>(heights, Allocator.TempJob)
            };

            var heightJobHandle = heightJob.Schedule(TotalBlockNumberX * TotalBlockNumberZ, 8);

            heightJobHandle.Complete();
            heightJob.Result.CopyTo(heights);

            // cleanup
            heightJob.Result.Dispose();

            return(heights);
        }
Exemple #5
0
        /// <summary>
        /// Run the jobs
        /// </summary>
        /// <returns>True if jobs kicked off, false if jobs already running.</returns>
        public static bool ScheduleJobs()
        {
            if (s_jobsRunning)
            {
                return(false);
            }

            if (s_lastQueryIndexHeights == 0)
            {
                // Nothing to do
                return(true);
            }

            s_jobsRunning = true;

            if (s_segments.IsCreated)
            {
                s_segments.Dispose();
            }
            if (s_matrixes.IsCreated)
            {
                s_matrixes.Dispose();
            }

            // Create a list of guid matrixes (do this every time a schedule happens since the matrixes are always updating)
            // Does it this way to not generate garbage
            NativeArray <int> guids = new NativeArray <int>(s_segmentRegistry.Count, Allocator.Temp);

            int index = 0;

            foreach (var guid in s_segmentRegistry.Keys)
            {
                guids[index++] = guid;
            }

            s_segments = new NativeArray <int2>(guids.Length, Allocator.TempJob);
            s_matrixes = new NativeArray <Matrix4x4>(guids.Length, Allocator.TempJob);

            for (int i = 0, l = guids.Length; i < l; i++)
            {
                int2 segment;
                s_segmentRegistry.TryGetValue(guids[i], out segment);                 // this should NEVER be false

                s_segments[i] = segment;

                Transform trans;
                if (s_transformsRegistry.TryGetValue(guids[i], out trans))
                {
                    s_matrixes[i] = trans.localToWorldMatrix;
                }

                // else a new matrix is empty which SHOULD transform that just based on world
            }

            guids.Dispose();

            var matrixJob = new MatrixTransformJob()
            {
                _querySegments = s_segments,
                _guidMatrixes  = s_matrixes,

                _segmentRegistry = s_segmentRegistryArray,
                _matrix          = s_matrixArray,
                _resolution      = s_resolutionArray,
                _size            = s_sizeArray,

                _depthCaches = s_depthCachesArray,

                _localPositions      = s_localQueryPositions,
                _worldQueryPositions = s_worldQueryPositions,
                _depthAtHeight       = s_heightQueryPositions,
            };

            var heightJob = new HeightJob()
            {
                _attenuationInShallows = 0.95f,                 // TODO - hook this up with the info from the simulation settings
                _waveNumbers           = s_waveNumbers,
                _amps           = s_amps,
                _windDirX       = s_windDirX,
                _windDirZ       = s_windDirZ,
                _phases         = s_phases,
                _chopAmps       = s_chopAmps,
                _numWaveVecs    = _waveVecCount,
                _queryPositions = s_worldQueryPositions,
                _depths         = s_heightQueryPositions,
                _computeSegment = new int2(0, s_localQueryPositions.Length),
                _time           = OceanRenderer.Instance.CurrentTime,
                _outHeights     = s_resultHeights,
                _seaLevel       = OceanRenderer.Instance.SeaLevel,
                _dontUseDepth   = false,
            };

            JobHandle handler = matrixJob.Schedule();

            s_handleHeightsHandler = heightJob.Schedule(s_lastQueryIndexHeights, 32, handler);

            JobHandle.ScheduleBatchedJobs();

            s_firstFrame = false;

            return(true);
        }