예제 #1
0
        public TerrainQuadTree(TerrainQuadTree *parent, int parentLodLevel, ref TerrainQuadTreeSettings setting, LocalPos sonPos, int2 parentPos, int2 rootPosition)
        {
            this.setting      = setting.Ptr();
            this.rootPosition = rootPosition;
            lodLevel          = parentLodLevel + 1;
            this.parent       = parent;
            leftDown          = null;
            leftUp            = null;
            rightDown         = null;
            rightUp           = null;
            localPosition     = parentPos * 2;
            switch (sonPos)
            {
            case LocalPos.LeftUp:
                localPosition += int2(0, 1);
                break;

            case LocalPos.RightDown:
                localPosition += int2(1, 0);
                break;

            case LocalPos.RightUp:
                localPosition += 1;
                break;
            }
        }
예제 #2
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            current = this;
            int indexMapSize = 1;

            for (int i = 1; i < lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            indexMapSize *= chunkCount;
            NativeArray <VirtualTextureFormat> allFormats = new NativeArray <VirtualTextureFormat>(4, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            allFormats[0]  = new VirtualTextureFormat(VirtualTextureSize.x512, RenderTextureFormat.RHalf, "_VirtualHeightMap");
            allFormats[1]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualAlbedoMap");
            allFormats[2]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualNormalMap");
            allFormats[3]  = new VirtualTextureFormat(VirtualTextureSize.x1024, RenderTextureFormat.ARGB32, "_VirtualSMOMap");
            virtualTexture = new VirtualTexture(129, indexMapSize, allFormats);
            allFormats.Dispose();
            InitializeMesh();
            dispatchDrawBuffer = new ComputeBuffer(5, sizeof(int), ComputeBufferType.IndirectArguments);
            const int INIT_LENGTH = 500;

            culledResultsBuffer = new ComputeBuffer(INIT_LENGTH, sizeof(int));
            loadedBuffer        = new ComputeBuffer(INIT_LENGTH, sizeof(TerrainChunkBuffer));
            loadedBufferList    = new NativeList <TerrainChunkBuffer>(INIT_LENGTH, Allocator.Persistent);
            shader = Resources.Load <ComputeShader>("TerrainCompute");
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = (uint)meshBuffer.count;
            dispatchDrawBuffer.SetData(dispatchDraw);
            allTrees = new Native2DArray <TerrainQuadTree>(chunkCount, Allocator.Persistent, true);
            setting  = new TerrainQuadTreeSettings
            {
                allLodLevles     = new NativeList_Float(lodDistances.Length + 1, Allocator.Persistent),
                largestChunkSize = chunkSize,
                screenOffset     = chunkOffset
            };
            for (int i = 0; i < lodDistances.Length; ++i)
            {
                setting.allLodLevles.Add(min(lodDistances[max(0, i - 1)], lodDistances[i]));
                setting.allLodLevles[i] *= setting.allLodLevles[i];
            }
            setting.allLodLevles[lodDistances.Length] = 0;
            int2 len = allTrees.Length;

            for (int x = 0; x < len.x; ++x)
            {
                for (int y = 0; y < len.y; ++y)
                {
                    allTrees[int2(x, y)] = new TerrainQuadTree(-1, setting.Ptr(), TerrainQuadTree.LocalPos.LeftDown, 0, int2(x, y));
                }
            }
        }
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            current = this;
            //virtualTexture = new VirtualTexture()
            basicChunkSize = (float)(chunkSize / pow(2.0, max(1, lodDistances.Length)));
            InitializeMesh();
            dispatchDrawBuffer = new ComputeBuffer(5, sizeof(int), ComputeBufferType.IndirectArguments);
            removeIndexBuffer  = new ComputeBuffer(10, sizeof(int2));
            const int INIT_LENGTH = 200;

            culledResultsBuffer = new ComputeBuffer(INIT_LENGTH, sizeof(int));
            loadedBuffer        = new ComputeBuffer(INIT_LENGTH, sizeof(TerrainChunkBuffer));
            loadedBufferList    = new NativeList <TerrainChunkBuffer>(INIT_LENGTH, Allocator.Persistent);
            shader = Resources.Load <ComputeShader>("TerrainCompute");
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = (uint)meshBuffer.count;
            dispatchDrawBuffer.SetData(dispatchDraw);
            allTrees = new Native2DArray <TerrainQuadTree>(chunkCount, Allocator.Persistent, true);
            setting  = new TerrainQuadTreeSettings
            {
                allLodLevles     = new NativeList_Float(lodDistances.Length + 1, Allocator.Persistent),
                largestChunkSize = chunkSize,
                screenOffset     = chunkOffset
            };
            for (int i = 0; i < lodDistances.Length; ++i)
            {
                setting.allLodLevles.Add(min(lodDistances[max(0, i - 1)], lodDistances[i]));
                setting.allLodLevles[i] *= setting.allLodLevles[i];
            }
            setting.allLodLevles[lodDistances.Length] = 0;
            int2 len = allTrees.Length;

            for (int x = 0; x < len.x; ++x)
            {
                for (int y = 0; y < len.y; ++y)
                {
                    allTrees[int2(x, y)] = new TerrainQuadTree(-1, setting.Ptr(), TerrainQuadTree.LocalPos.LeftDown, 0, int2(x, y));
                }
            }
        }