Exemplo n.º 1
0
        protected override void OnEnableFunc()
        {
            if (current && current != this)
            {
                enabled = false;
                Debug.LogError("Only One Terrain allowed!");
                return;
            }
            if (!terrainData)
            {
                enabled = false;
                Debug.LogError("No Data!");
                return;
            }
            if (!cam || !decalCamera)
            {
                enabled = false;
                Debug.LogError("No Decal Camera!");
                return;
            }
            initializing      = true;
            lodOffset         = terrainData.GetLodOffset();
            largestChunkCount = (int)(0.1 + pow(2.0, lodOffset));
            msb = new MStringBuilder(32);
            oneVTPixelWorldLength = terrainData.VTTexelLength();
            textureShader         = Resources.Load <ComputeShader>("ProceduralTexture");
            shader  = Resources.Load <ComputeShader>("TerrainCompute");
            current = this;
            int indexMapSize = 1;

            for (int i = 1; i < terrainData.lodDistances.Length; ++i)
            {
                indexMapSize *= 2;
            }
            vtContainer  = new NativeArray <int>(4, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            maskLoadList = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            ComputeShader editShader = Resources.Load <ComputeShader>("TerrainEdit");

            loadingThread = new MTerrainLoadingThread(10);
            maskLoader    = new VirtualTextureLoader(terrainData.maskmapPath, editShader, largestChunkCount, MASK_RESOLUTION, false, loadingThread);
            heightLoader  = new VirtualTextureLoader(terrainData.heightmapPath, editShader, largestChunkCount, MASK_RESOLUTION, true, loadingThread);

            loadDataList       = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            initializeLoadList = new NativeQueue <TerrainLoadData>(100, Allocator.Persistent);
            NativeArray <uint> dispatchDraw = new NativeArray <uint>(5, Allocator.Temp, NativeArrayOptions.ClearMemory);

            dispatchDraw[0] = 6;
            VirtualTextureFormat *formats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8B8A8_UNorm, "_VirtualMainTex", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R16G16_SNorm, "_VirtualBumpMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)COLOR_RESOLUTION, GraphicsFormat.R8G8_UNorm, "_VirtualSMMap", 2),
                new VirtualTextureFormat((VirtualTextureSize)HEIGHT_RESOLUTION, HEIGHT_FORMAT, "_VirtualDisplacement", 0)
            };

            mipIDs          = new NativeArray <int>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            mipIDs[0]       = Shader.PropertyToID("_Mip0");
            mipIDs[1]       = Shader.PropertyToID("_Mip1");
            chunkCount      = (int)(pow(2.0, terrainData.lodDistances.Length - 1) + 0.1);
            vt              = new VirtualTexture(terrainData.virtualTexCapacity, min(2048, chunkCount), formats, 4, "_TerrainVTIndexTex");
            textureCapacity = terrainData.virtualTexCapacity;
            VirtualTextureFormat *maskFormats = stackalloc VirtualTextureFormat[]
            {
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R8_UNorm, "_VirtualMaskmap"),
                new VirtualTextureFormat((VirtualTextureSize)MASK_RESOLUTION, GraphicsFormat.R16_UNorm, "_VirtualHeightmap"),
            };

            maskVT = new VirtualTexture(terrainData.heightmapTexCapacity, largestChunkCount, maskFormats, 2, "_MaskIndexMap");
            maskVT.GetTexture(0).filterMode = FilterMode.Point;
            maskVT.GetTexture(1).filterMode = FilterMode.Point;
            vt.GetTexture(0).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(1).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(2).filterMode     = FilterMode.Trilinear;
            vt.GetTexture(3).filterMode     = FilterMode.Bilinear;
            allLodLevles = new NativeList_Float(terrainData.lodDistances.Length, Allocator.Persistent);
            for (int i = 0; i < terrainData.lodDistances.Length; ++i)
            {
                allLodLevles.Add((float)min(terrainData.lodDistances[max(0, i - 1)], terrainData.lodDistances[i]));
            }
            allLodLevles[terrainData.lodDistances.Length] = 0;
            meshResolution = terrainData.GetMeshResolution();
            cullingFlags   = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8_UNorm,
                dimension         = TextureDimension.Tex2D,
                width             = meshResolution,
                height            = meshResolution,
                volumeDepth       = 1,
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = false
            });
            cullingFlags.filterMode = FilterMode.Point;
            cullingFlags.Create();
            albedoTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R8G8B8A8_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                autoGenerateMips  = false,
                useMipMap         = true,
                mipCount          = 6,
            });
            albedoTex.Create();
            normalTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_SNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                autoGenerateMips  = false,
                useMipMap         = true,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                mipCount          = 6,
            });
            normalTex.Create();
            smTex = new RenderTexture(new RenderTextureDescriptor
            {
                graphicsFormat    = GraphicsFormat.R16G16_UNorm,
                dimension         = TextureDimension.Tex2DArray,
                width             = COLOR_RESOLUTION,
                height            = COLOR_RESOLUTION,
                volumeDepth       = Mathf.Max(1, terrainData.textures.Length),
                enableRandomWrite = true,
                msaaSamples       = 1,
                useMipMap         = true,
                autoGenerateMips  = false,
                mipCount          = 6,
                depthBufferBits   = 0,
                useDynamicScale   = false
            });
            smTex.Create();

            /*   heightTex = new RenderTexture(new RenderTextureDescriptor
             * {
             *     graphicsFormat = GraphicsFormat.R8_UNorm,
             *     dimension = TextureDimension.Tex2DArray,
             *     width = COLOR_RESOLUTION,
             *     height = COLOR_RESOLUTION,
             *     volumeDepth = Mathf.Max(1, terrainData.textures.Length),
             *     enableRandomWrite = true,
             *     msaaSamples = 1,
             *     useMipMap = true,
             *     autoGenerateMips = false,
             *     mipCount = 6,
             *     depthBufferBits = 0,
             *     useDynamicScale = false
             * });*/
            heightloadingCacheRT = new RenderTexture(new RenderTextureDescriptor
            {
                width             = COLOR_RESOLUTION + 2,
                height            = COLOR_RESOLUTION + 2,
                volumeDepth       = 1,
                enableRandomWrite = true,
                dimension         = TextureDimension.Tex2D,
                graphicsFormat    = GraphicsFormat.R16_UNorm,
                msaaSamples       = 1,
                useMipMap         = false,
                autoGenerateMips  = false,
                mipCount          = 0,
                depthBufferBits   = 0,
                useDynamicScale   = false,
            });
            randomTileRT = new RenderTexture(256, 256, 0, GraphicsFormat.R16G16B16A16_SNorm, 0);
            randomTileRT.enableRandomWrite = true;
            randomTileRT.wrapMode          = TextureWrapMode.Repeat;
            randomTileRT.filterMode        = FilterMode.Point;
            randomTileRT.Create();

            heightloadingCacheRT.Create();
            //    heightTex.Create();
            smTex.wrapMode     = TextureWrapMode.Repeat;
            normalTex.wrapMode = TextureWrapMode.Repeat;
            albedoTex.wrapMode = TextureWrapMode.Repeat;
            //   heightTex.wrapMode = TextureWrapMode.Repeat;
            boundBoxLoadList   = new NativeQueue <MaskLoadCommand>(10, Allocator.Persistent);
            boundingLoadStream = new FileStream(terrainData.boundPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            boundingDict       = new NativeDictionary <int2, MTerrainBoundingTree, Int2Equal>(20, Allocator.Persistent, new Int2Equal());
            InitializeMeshData();
            allDrawCommand = new NativeList <TerrainDrawCommand>(20, Allocator.Persistent);
            materialBuffer = new ComputeBuffer(max(1, terrainData.allMaterials.Length), sizeof(MTerrainData.HeightBlendMaterial));
            materialBuffer.SetData(terrainData.allMaterials);
            decalLayerOffset = max(0, terrainData.lodDistances.Length - terrainData.allDecalLayers.Length);
            tree             = new TerrainQuadTree(-1, TerrainQuadTree.LocalPos.LeftDown, 0, 0, terrainData.largestChunkSize, double3(1, 0, 0), 0);

            StartCoroutine(AsyncLoader());
        }