示例#1
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));
                }
            }
        }
 /// <summary>
 /// Init Virtual Texture
 /// </summary>
 /// <param name="perTextureSize">Virtual texture's basic size</param>
 /// <param name="maximumSize">Virtual texture's array size</param>
 /// <param name="indexSize">Index Texture's size</param>
 /// <param name="formats">Each VT's format</param>
 public VirtualTexture(int maximumSize, int indexSize, NativeArray <VirtualTextureFormat> formats)
 {
     allFormats = new NativeArray <VirtualTextureFormat>(formats.Length, Allocator.Persistent);
     UnsafeUtility.MemCpy(allFormats.GetUnsafePtr(), formats.GetUnsafePtr(), sizeof(VirtualTextureFormat) * formats.Length);
     shader            = Resources.Load <ComputeShader>("VirtualTexture");
     commandListBuffer = new ComputeBuffer(64, sizeof(SetIndexCommand));
     indexBuffers      = new Native2DArray <float4>(indexSize, Allocator.Persistent);
     pool     = new TexturePool(maximumSize);
     indexTex = new RenderTexture(new RenderTextureDescriptor
     {
         colorFormat       = RenderTextureFormat.ARGBHalf,
         depthBufferBits   = 0,
         dimension         = TextureDimension.Tex2D,
         enableRandomWrite = true,
         width             = indexSize,
         height            = indexSize,
         volumeDepth       = 1,
         msaaSamples       = 1
     });
     indexTex.filterMode = FilterMode.Point;
     indexTex.Create();
     textures = new RenderTexture[formats.Length];
     for (int i = 0; i < formats.Length; ++i)
     {
         VirtualTextureFormat format = formats[i];
         textures[i] = new RenderTexture(new RenderTextureDescriptor
         {
             colorFormat       = format.format,
             depthBufferBits   = 0,
             dimension         = TextureDimension.Tex2DArray,
             enableRandomWrite = true,
             width             = (int)format.perElementSize,
             height            = (int)format.perElementSize,
             volumeDepth       = maximumSize,
             msaaSamples       = 1
         });
         textures[i].Create();
     }
     loadNewTexFrameCount = -1;
     indexBuffers.SetAll(float4(0, 0, 0, -1));
 }
 public void Init(int2 perTextureSize, int maximumSize, int2 indexSize, NativeArray <RenderTextureFormat> formats)
 {
     commandListBuffer = new ComputeBuffer(64, sizeof(SetIndexCommand));
     indexBuffers      = new Native2DArray <float4>(indexSize, Allocator.Persistent);
     pool     = new TexturePool(maximumSize);
     indexTex = new RenderTexture(new RenderTextureDescriptor
     {
         colorFormat       = RenderTextureFormat.ARGBHalf,
         depthBufferBits   = 0,
         dimension         = TextureDimension.Tex2D,
         enableRandomWrite = true,
         width             = indexSize.x,
         height            = indexSize.y,
         volumeDepth       = 1,
         msaaSamples       = 1
     });
     indexTex.Create();
     textures = new RenderTexture[formats.Length];
     for (int i = 0; i < formats.Length; ++i)
     {
         textures[i] = new RenderTexture(new RenderTextureDescriptor
         {
             colorFormat       = formats[i],
             depthBufferBits   = 0,
             dimension         = TextureDimension.Tex2DArray,
             enableRandomWrite = true,
             width             = perTextureSize.x,
             height            = perTextureSize.y,
             volumeDepth       = maximumSize,
             msaaSamples       = 1
         });
         textures[i].Create();
     }
     loadNewTexFrameCount = -1;
     indexBuffers.SetAll(float4(0, 0, 0, -1));
 }