Beispiel #1
0
        public void PrepareSplatmapGeneration(bool clearLockedTextures)
        {
            LoadHeightData();


            int width           = _terrain.terrainData.alphamapWidth;
            int height          = _terrain.terrainData.alphamapHeight;
            int layers          = _terrain.terrainData.alphamapLayers;
            int heightMapLength = width * height;

            if (_heightMapSamples.IsCreated)
            {
                _heightMapSamples.Dispose();
            }

            _heightMapSamples = new NativeArray <HeightMapSample>(heightMapLength, Allocator.TempJob);
            SampleHeightMapJob sampleHeightMapJob = new SampleHeightMapJob
            {
                HeightMapSamples = _heightMapSamples,
                InputHeights     = Heights,
                HeightMapScale   = _heightmapScale,
                HeightmapHeight  = _heightmapHeight,
                HeightmapWidth   = _heightmapWidth,
                Scale            = _scale,
                Size             = _size,
                Width            = width,
                Height           = height
            };

            _splatMapHandle = sampleHeightMapJob.Schedule(heightMapLength, 32);


            if (_currentSplatmapArray.IsCreated)
            {
                _currentSplatmapArray.Dispose();
            }

            _currentSplatmapArray = new NativeArray <float>(width * height * layers, Allocator.TempJob);

            if (!clearLockedTextures)
            {
                float[,,] spltmapArray = _terrain.terrainData.GetAlphamaps(0, 0, width, height);
                _currentSplatmapArray.CopyFromFast(spltmapArray);
            }

            //Read original splatdata to a original splat map array
        }
Beispiel #2
0
        public void AddPolygon(List <Vector3> pointList)
        {
            _points2D = new Vector2[pointList.Count];
            _points3D = new Vector3[pointList.Count];
            for (int i = 0; i <= pointList.Count - 1; i++)
            {
                _points2D[i] = new Vector2(pointList[i].x, pointList[i].z);
                _points3D[i] = pointList[i];
            }

            MaskBounds = GetMaskBounds();
            if (PolygonArray.IsCreated)
            {
                PolygonArray.Dispose();
            }
            PolygonArray = new NativeArray <Vector2>(_points2D.Length, Allocator.Persistent);
            PolygonArray.CopyFromFast(_points2D);
            CreateSegments();
        }
Beispiel #3
0
        void CreateSegments()
        {
            _segments = new LineSegment2D[_points2D.Length];
            for (int i = 0; i <= _points2D.Length - 2; i++)
            {
                LineSegment2D lineSegment2D = new LineSegment2D(_points2D[i], _points2D[i + 1]);
                _segments[i] = lineSegment2D;
            }

            if (_points2D.Length > 0)
            {
                LineSegment2D lineSegment2D = new LineSegment2D(_points2D[0], _points2D[_points2D.Length - 1]);
                _segments[_points2D.Length - 1] = lineSegment2D;
            }

            if (SegmentArray.IsCreated)
            {
                SegmentArray.Dispose();
            }
            SegmentArray = new NativeArray <LineSegment2D>(_segments.Length, Allocator.Persistent);
            SegmentArray.CopyFromFast(_segments);
        }
        //public NativeArray<T> BackJobArray()
        //{
        //    return nativeArray1;
        //}

        public void SwapBuffer()
        {
            var back = nativeArray1;

            nativeArray1 = nativeArray0;

            // サイズを合わせる
            if (back.IsCreated == false || back.Length != nativeArray0.Length)
            {
                if (back.IsCreated)
                {
                    back.Dispose();
                }
                //back = new NativeArray<T>(nativeArray0.Length, Allocator.Persistent, NativeArrayOptions.ClearMemory);
                back = new NativeArray <T>(nativeArray0.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                back.CopyFromFast(nativeArray0);

                //Debug.Log("★サイズ変更!");
            }

            nativeArray0 = back;
        }
Beispiel #5
0
        //=========================================================================================
        /// <summary>
        /// データ追加
        /// 追加したインデックスを返す
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public int Add(T element)
        {
            int index = 0;

            if (emptyStack.Count > 0)
            {
                // 再利用
                index = emptyStack.Dequeue();
                nativeArray0[index] = element;
            }
            else
            {
                // 新規
                if (nativeArray0.Length <= useLength)
                {
                    // 拡張
                    int len = nativeArray0.Length;
                    while (len <= useLength)
                    {
                        len += len;
                    }
                    var nativeArray2 = new NativeArray <T>(len, Allocator.Persistent);
                    nativeArray2.CopyFromFast(nativeArray0);
                    nativeArray0.Dispose();

                    nativeArray0 = nativeArray2;
                }

                index = useLength;
                nativeArray0[index] = element;
                nativeLength        = nativeArray0.Length;

                useLength++;
            }
            useIndexSet.Add(index);

            return(index);
        }
Beispiel #6
0
        //private ManagedNativeFloatArray _nativeManagedFloatArray;

        void LoadHeightData()
        {
            _heightmapScale  = _terrain.terrainData.heightmapScale;
            _heightmapHeight = _terrain.terrainData.heightmapHeight;
            _heightmapWidth  = _terrain.terrainData.heightmapWidth;

            //Vector3 floatingOriginOffset = Vector3.zero;
            //if (ApplyFloatingOriginOffset)
            //{
            //    floatingOriginOffset = VegetationStudioManager.GetFloatingOriginOffset();
            //}
            //_terrainPosition = _terrain.transform.position - floatingOriginOffset;
            //_terrainPosition = TerrainPosition;

            _size    = _terrain.terrainData.size;
            _scale.x = _size.x / (_heightmapWidth - 1);
            _scale.y = _size.y;
            _scale.z = _size.z / (_heightmapHeight - 1);

            Vector2 terrainCenter = new Vector2(TerrainPosition.x, TerrainPosition.z);
            Vector2 terrainSize   = new Vector2(_size.x, _size.z);

            _terrainRect = new Rect(terrainCenter, terrainSize);

            float[,] hs = _terrain.terrainData.GetHeights(0, 0, _heightmapWidth, _heightmapHeight);

            //_nativeManagedFloatArray = new ManagedNativeFloatArray(hs);
            //Heights = (NativeArray<float>)_nativeManagedFloatArray.NativeArray;

            if (Heights.IsCreated)
            {
                Heights.Dispose();
            }

            Heights = new NativeArray <float>(_heightmapWidth * _heightmapHeight, Allocator.Persistent);
            Heights.CopyFromFast(hs);
        }
        //=========================================================================================
        /// <summary>
        /// データチャンクの追加
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public ChunkData AddChunk(int length)
        {
            // 再利用チェック
            for (int i = 0; i < emptyChunkList.Count; i++)
            {
                var cdata = emptyChunkList[i];
                if (cdata.dataLength >= length)
                {
                    // このチャンクを再利用する
                    int remainder = cdata.dataLength - length;
                    if (remainder > 0)
                    {
                        // 分割
                        var rchunk = new ChunkData()
                        {
                            chunkNo    = ++chunkSeed,
                            startIndex = cdata.startIndex + length,
                            dataLength = remainder,
                        };
                        emptyChunkList[i] = rchunk;
                    }
                    else
                    {
                        emptyChunkList.RemoveAt(i);
                    }
                    cdata.dataLength = length;

                    // 使用リストに追加
                    useChunkList.Add(cdata);

                    return(cdata);
                }
            }

            // 新規追加
            var data = new ChunkData()
            {
                chunkNo    = ++chunkSeed,
                startIndex = useLength,
                dataLength = length,
                useLength  = 0,
            };

            useChunkList.Add(data);
            useLength += length;

            if (nativeArray.Length < useLength)
            {
                // 拡張
                int len = nativeArray.Length;
                while (len < useLength)
                {
                    len += Mathf.Min(len, 4096);
                }
                var nativeArray2 = new NativeArray <T>(len, Allocator.Persistent);
                nativeArray2.CopyFromFast(nativeArray);
                nativeArray.Dispose();

                nativeArray  = nativeArray2;
                nativeLength = nativeArray.Length;
            }

            return(data);
        }
 public void CopyToNativeArray()
 {
     NativeVegetationItemArray = new NativeArray <PersistentVegetationItem>(VegetationItemList.Count, Allocator.Persistent);
     NativeVegetationItemArray.CopyFromFast(VegetationItemList);
 }
Beispiel #9
0
 public override void DoCopy(int[] source, NativeArray <int> destination)
 {
     destination.CopyFromFast(source);
 }