public void Update() { if (bDirty) { bDirty = false; bNeedReselectPreview = true; DebugTool.Log("开始更新射线模块"); rtm.clearAll(); blockManager.forEachBlock((short block, int gx, int gy, int gz) => { if (block != (short)BlockTypeEnum.Air) { rtm.setBlock(gx, gy, gz, RayCastBlockType.All); } }); //初始化AO计算 RayMarchingAo rma = new RayMarchingAo(); rma.Init(rtm); foreach (var obj in objects) { GameObject.Destroy(obj); } objects.Clear(); //m_meshes.Clear(); DebugTool.Log("开始计算光照"); { //计算光照 Vector3 sunDir = new Vector3(1, -3, 1); sunDir.Normalize(); TerrainTool.calcLight(blockManager, rtm, rays, sunDir); } BlockTypeFun blockTypeFun = new BlockTypeFun(); //TerrainTool.calcLight2(blockManager, 16); DebugTool.Log("开始更新网格"); for (int i = 0; i < SpaceX; i++) { for (int k = 0; k < SpaceZ; k++) { for (int j = 0; j < SpaceY; j++) { BlockChunk chunk = new BlockChunk(blockManager, i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize); for (int f = 0; f < 6; f++) { List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f); Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f); Texture2D readback = null; if (surface.Count > 0) { RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f); //回读亮度数据 RenderTexture.active = targetAoResult; readback = new Texture2D(targetAoResult.width, targetAoResult.height); readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0); MeshTool.SetRaytraceAo(surface, readback); } Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun); if (mesh) { Vector3 pos = ((new Vector3(i, j, k))) * Const.ChunkSize - halfSpaceSize; if (mesh != null) { GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter)); obj.isStatic = true; obj.GetComponent <Renderer>().material = GlobalResources.getBlockMaterial(); obj.GetComponent <MeshFilter>().mesh = mesh; obj.transform.position = pos; objects.Add(obj); } } } } } } DebugTool.Log("结束"); } }