Exemple #1
0
        private int SetData(RichCell c, int selfIdx, int neighbourIdx, int dataIdx, bool dataRecorded)
        {
            RichCell.HeightData data;
            if (dataRecorded)
            {
                data = _heightData[dataIdx++];
            }
            else
            {
                data = new RichCell.HeightData();
            }

            c.SetHeightData(selfIdx, data);

            var n = GetNeighbourFromIdx(c, selfIdx);

            if (n != null)
            {
                //Neighbour values are reversed
                var neighbourData = new RichCell.HeightData
                {
                    climbHeight = data.dropHeight,
                    dropHeight  = data.climbHeight,
                    slope       = data.slope
                };

                n.SetHeightData(neighbourIdx, neighbourData);
            }

            return(dataIdx);
        }
        private void StoreData(RichCell.HeightData[] source, int sourceIdx, int cellIdx, int entryMark)
        {
            var data = source[sourceIdx];
            if (data.climbHeight > 0f || data.dropHeight > 0f || data.slope > 0f)
            {
                if (_heightDataIndices[cellIdx] == 0)
                {
                    _heightDataIndices[cellIdx] = _recordedEntries << 4;
                }

                _heightData[_recordedEntries++] = data;
                _heightDataIndices[cellIdx] |= entryMark;
            }
        }
            private void UpdateCellHeightData(RichCell reference, RichCell neighbour, float ledgeThreshold, int dx, int dz)
            {
                GetPerpendicularOffsets(dx, dz);
                var granularity = _matrix.granularity;
                var steps       = _matrix.cellSize / granularity;

                var data = new RichCell.HeightData();

                for (int o = 0; o < 3; o++)
                {
                    var samplePos  = reference.position + _offsets[o];
                    var fromHeight = _heightSampler.SampleHeight(samplePos, _matrix);

                    var climbAccumulator = 0.0f;
                    var dropAccumulator  = 0.0f;

                    for (int i = 0; i < steps; i++)
                    {
                        samplePos.x += (dx * granularity);
                        samplePos.z += (dz * granularity);

                        var toHeight = _heightSampler.SampleHeight(samplePos, _matrix);

                        var heightDiff = toHeight - fromHeight;
                        var absDiff    = Mathf.Abs(heightDiff);

                        //Initially we just record the slope as the height diff
                        if (data.slope < absDiff)
                        {
                            data.slope = absDiff;
                        }

                        if (absDiff < ledgeThreshold)
                        {
                            if (data.dropHeight < dropAccumulator)
                            {
                                data.dropHeight = dropAccumulator;
                            }
                            else if (data.climbHeight < climbAccumulator)
                            {
                                data.climbHeight = climbAccumulator;
                            }

                            dropAccumulator  = 0f;
                            climbAccumulator = 0f;
                        }

                        if (heightDiff > 0f)
                        {
                            climbAccumulator += heightDiff;

                            if (data.dropHeight < dropAccumulator)
                            {
                                data.dropHeight = dropAccumulator;
                            }

                            dropAccumulator = 0f;
                        }
                        else if (heightDiff < 0f)
                        {
                            dropAccumulator += absDiff;

                            if (data.climbHeight < climbAccumulator)
                            {
                                data.climbHeight = climbAccumulator;
                            }

                            climbAccumulator = 0f;
                        }

                        fromHeight = toHeight;
                    }

                    //Make sure we get the last accumulation recorded
                    if (data.dropHeight < dropAccumulator)
                    {
                        data.dropHeight = dropAccumulator;
                    }
                    else if (data.climbHeight < climbAccumulator)
                    {
                        data.climbHeight = climbAccumulator;
                    }
                } /* end for each offset */

                //Set the slope to an angular value
                var mod = (dx != 0 && dz != 0) ? Consts.SquareRootTwo : 1f;

                data.slope = Mathf.Atan(data.slope / (granularity * mod)) * Mathf.Rad2Deg;

                //Set the data
                reference.SetHeightData(dx, dz, data);
                reference.CalculateWorst();

                //Create the neighbour data as the reverse of this, i.e. drop = climb and vice versa
                var neighbourData = new RichCell.HeightData(data.slope, data.dropHeight, data.climbHeight);

                neighbour.SetHeightData(-dx, -dz, neighbourData);
                neighbour.CalculateWorst();
            }
        private int SetData(RichCell c, int selfIdx, int neighbourIdx, int dataIdx, bool dataRecorded)
        {
            RichCell.HeightData data;
            if (dataRecorded)
            {
                data = _heightData[dataIdx++];
            }
            else
            {
                data = new RichCell.HeightData();
            }

            c.SetHeightData(selfIdx, data);

            var n = GetNeighbourFromIdx(c, selfIdx);
            if (n != null)
            {
                //Neighbour values are reversed
                var neighbourData = new RichCell.HeightData
                {
                    climbHeight = data.dropHeight,
                    dropHeight = data.climbHeight,
                    slope = data.slope
                };

                n.SetHeightData(neighbourIdx, neighbourData);
            }

            return dataIdx;
        }
        private void UpdateCellHeightData(RichCell reference, RichCell neighbour, float ledgeThreshold, int dx, int dz)
        {
            var heightSampler = GetHeightSampler();

            var offsets = GetPerpendicularOffsets(dx, dz);
            var granularity = _matrix.granularity;
            var steps = _matrix.cellSize / granularity;

            var data = new RichCell.HeightData();

            for (int o = 0; o < 3; o++)
            {
                var samplePos = reference.position + offsets[o];
                var fromHeight = heightSampler.SampleHeight(samplePos, _matrix);

                var climbAccumulator = 0.0f;
                var dropAccumulator = 0.0f;

                for (int i = 0; i < steps; i++)
                {
                    samplePos.x += (dx * granularity);
                    samplePos.z += (dz * granularity);

                    var toHeight = heightSampler.SampleHeight(samplePos, _matrix);

                    var heightDiff = toHeight - fromHeight;
                    var absDiff = Mathf.Abs(heightDiff);

                    //Initially we just record the slope as the height diff
                    if (data.slope < absDiff)
                    {
                        data.slope = absDiff;
                    }

                    if (absDiff < ledgeThreshold)
                    {
                        if (data.dropHeight < dropAccumulator)
                        {
                            data.dropHeight = dropAccumulator;
                        }
                        else if (data.climbHeight < climbAccumulator)
                        {
                            data.climbHeight = climbAccumulator;
                        }

                        dropAccumulator = 0f;
                        climbAccumulator = 0f;
                    }

                    if (heightDiff > 0f)
                    {
                        climbAccumulator += heightDiff;

                        if (data.dropHeight < dropAccumulator)
                        {
                            data.dropHeight = dropAccumulator;
                        }

                        dropAccumulator = 0f;
                    }
                    else if (heightDiff < 0f)
                    {
                        dropAccumulator += absDiff;

                        if (data.climbHeight < climbAccumulator)
                        {
                            data.climbHeight = climbAccumulator;
                        }

                        climbAccumulator = 0f;
                    }

                    fromHeight = toHeight;
                }

                //Make sure we get the last accumulation recorded
                if (data.dropHeight < dropAccumulator)
                {
                    data.dropHeight = dropAccumulator;
                }
                else if (data.climbHeight < climbAccumulator)
                {
                    data.climbHeight = climbAccumulator;
                }
            } /* end for each offset */

            //Set the slope to an angular value
            var mod = (dx != 0 && dz != 0) ? Consts.SquareRootTwo : 1f;
            data.slope = Mathf.Atan(data.slope / (granularity * mod)) * Mathf.Rad2Deg;

            //Set the data
            reference.SetHeightData(dx, dz, data);
            reference.CalculateWorst();

            //Create the neighbour data as the reverse of this, i.e. drop = climb and vice versa
            var neighbourData = new RichCell.HeightData(data.slope, data.dropHeight, data.climbHeight);
            neighbour.SetHeightData(-dx, -dz, neighbourData);
            neighbour.CalculateWorst();
        }