Exemplo n.º 1
0
 public EditorCommandSet(int x, int y, int z, int dx, int dy, int dz, Block.BlockManager bm, short newType)
 {
     if (dx < 0)
     {
         x += dx + 1;
         dx = -dx;
     }
     if (dy < 0)
     {
         y += dy + 1;
         dy = -dy;
     }
     if (dz < 0)
     {
         z += dz + 1;
         dz = -dz;
     }
     this.dx  = dx; this.dy = dy; this.dz = dz;
     this.x   = x; this.y = y; this.z = z;
     oldTypes = new short[dx, dy, dz];
     for (int i = 0; i < dx; i++)
     {
         for (int j = 0; j < dy; j++)
         {
             for (int k = 0; k < dz; k++)
             {
                 oldTypes[i, j, k] = bm.getBlock(x + i, y + j, z + k);
             }
         }
     }
     this.newType = newType;
 }
Exemplo n.º 2
0
 public void Redo(Block.BlockManager bm)
 {
     if (commands.Count > cmdLength)
     {
         commands[cmdLength].Do(bm);
         cmdLength += 1;
     }
 }
Exemplo n.º 3
0
 public void Undo(Block.BlockManager bm)
 {
     if (cmdLength > 0)
     {
         commands[cmdLength - 1].Undo(bm);
         cmdLength -= 1;
     }
 }
Exemplo n.º 4
0
 public static void SaveModel(Block.BlockManager bm, VecInt3 pivot, string path)
 {
     //Block.BoundsInt bounds = new Block.BoundsInt();
     //bm.forEachChunk((Block.BlockChunk chunk, int cx, int cy, int cz) => {
     //    if (!chunk.isEmpty()) {
     //        for (int z = 0; z < Block.Const.ChunkSize; z++) {
     //            for (int x = 0; x < Block.Const.ChunkSize; x++) {
     //                for (int y = 0; y < Block.Const.ChunkSize; y++) {
     //                    int gx = cx * Block.Const.ChunkSize + x;
     //                    int gy = cy * Block.Const.ChunkSize + y;
     //                    int gz = cz * Block.Const.ChunkSize + z;
     //                    short blockType = bm.getBlock(gx, gy, gz);
     //                    if (bm.blockTypeFun.isOpacity(blockType)) {
     //                        bounds.Encapsulate(new Block.VecInt3(gx, gy, gz));
     //                    }
     //                }
     //            }
     //        }
     //    }
     //});
     //if (bounds.IsValid()) {
     //    BlockData.Model model = new BlockData.Model();
     //    model.version = 0;
     //    model.sizeX = bounds.Size.x;
     //    model.sizeY = bounds.Size.y;
     //    model.sizeZ = bounds.Size.z;
     //    model.pivotX = pivot.x - bounds.min.x;
     //    model.pivotY = pivot.y - bounds.min.y;
     //    model.pivotZ = pivot.z - bounds.min.z;
     //
     //    int num = 0;
     //    short lastType = bm.getBlock(bounds.min.x, bounds.min.y, bounds.min.z);
     //    for (int z = bounds.min.z; z <= bounds.max.z; z++) {
     //        for (int x = bounds.min.z; x <= bounds.max.x; x++) {
     //            for (int y = bounds.min.y; y <= bounds.max.y; y++) {
     //                short curType = bm.getBlock(x, y, z);
     //                if (lastType == curType) {
     //                    num++;
     //                }
     //                else {
     //                    BlockData.BlockPair block = new BlockData.BlockPair();
     //                    block.lenght = num;
     //                    block.type = lastType;
     //                    model.blocks.Add(block);
     //                    lastType = curType;
     //                    num = 1;
     //                }
     //            }
     //        }
     //    }
     //    BlockData.BlockPair blockLast = new BlockData.BlockPair();
     //    blockLast.lenght = num;
     //    blockLast.type = lastType;
     //    model.blocks.Add(blockLast);
     //    Write<BlockData.Model>(model, path);
     //}
 }
Exemplo n.º 5
0
 public void Do(EditorCommandBase cmd, Block.BlockManager bm)
 {
     cmd.Do(bm);
     for (int i = commands.Count - 1; i >= cmdLength; i--)
     {
         commands.RemoveAt(i);
     }
     commands.Add(cmd);
     cmdLength = commands.Count;
 }
Exemplo n.º 6
0
    public static bool isOnBlock(Block.BlockManager bm, Vector3 pos)
    {
        VecInt3 bpos  = getBlockPos(pos - Vector3.up * 0.1f);
        short   block = bm.getBlock(bpos.x, bpos.y, bpos.z);

        if (bm.blockTypeFun.isCollider(block))
        {
            return(true);
        }
        return(false);
    }
Exemplo n.º 7
0
 public override void Undo(Block.BlockManager bm)
 {
     for (int i = 0; i < dx; i++)
     {
         for (int j = 0; j < dy; j++)
         {
             for (int k = 0; k < dz; k++)
             {
                 bm.setBlock(x + i, y + j, z + k, oldTypes[i, j, k]);
             }
         }
     }
 }
Exemplo n.º 8
0
 public override void Do(Block.BlockManager bm)
 {
     for (int i = 0; i < dx; i++)
     {
         for (int j = 0; j < dy; j++)
         {
             for (int k = 0; k < dz; k++)
             {
                 bm.setBlock(x + i, y + j, z + k, newType);
             }
         }
     }
 }
Exemplo n.º 9
0
Arquivo: main.cs Projeto: wachel/block
    IEnumerator InitWorld()
    {
        Block.BlockManager bm = new Block.BlockManager();

        for (int i = 0; i < (int)Game.BlockType.Num; i++)
        {
            for (int f = 0; f < 6; f++)
            {
                Game.BlockType block = (Game.BlockType)i;
                texturePacker.AddTexture(block, f, TextureNameConfig.GetTextureName(block, f));
            }
        }
        texturePacker.Pack();
        packedTexture = texturePacker.GetPackedTexture();

        BlockTypeFun blockTypeFun = new BlockTypeFun();

        blockTypeFun.texturePacker = texturePacker;
        //float startTime = Time.realtimeSinceStartup;
        int WorldSizeX = 8;
        int WorldSizeY = 2;
        int WorldSizeZ = 8;

        //申请内存
        bm.create(WorldSizeX * Const.ChunkSize, WorldSizeY * Const.ChunkSize, WorldSizeZ * Const.ChunkSize, blockTypeFun);

        DebugTool.Log("申请内存");
        yield return(null);

        //
        TerrainTool.createTerrain(bm, WorldSizeX, WorldSizeY, WorldSizeZ);
        DebugTool.Log("创建地形");
        yield return(null);

        TerrainTool.createTree(bm, WorldSizeX, WorldSizeY, WorldSizeZ);
        DebugTool.Log("创建树");
        yield return(null);

        List <HouseItem> houseItems = new List <HouseItem>();

        TerrainTool.createBuildings(bm, ref houseItems);
        DebugTool.Log("创建建筑完成");
        yield return(null);

        //光线追踪初始化
        RayCastManager rtm = new RayCastManager();

        rtm.create(bm.SizeX, bm.SizeY, bm.SizeZ);
        for (int x = 0; x < rtm.getSizeX(); x++)
        {
            for (int z = 0; z < rtm.getSizeZ(); z++)
            {
                for (int y = 0; y < rtm.getSizeY(); y++)
                {
                    short            block = bm.getBlock(x, y, z);
                    RayCastBlockType rlt   = 0;
                    rlt |= blockTypeFun.isCollider(block) ? RayCastBlockType.Collider : RayCastBlockType.Nothing;
                    rlt |= blockTypeFun.isOpacity(block) ? RayCastBlockType.Opacity : RayCastBlockType.Nothing;
                    rtm.setBlock(x, y, z, rlt);
                }
                rtm.updateInSun(x, z);
            }
        }

        //预处理光线
        Vector3[][] rays = new Vector3[6][];
        {
            for (int i = 0; i < 6; i++)
            {
                rays[i] = TerrainTool.getRandomRays(9, i);
            }
        }
        DebugTool.Log("预处理光线追踪完成");
        yield return(null);

        rtm.updateAllLight();
        DebugTool.Log("扩散光照计算完毕");
        yield return(null);

        //int[] lightCount = new int[Const.MaxLightIteration + 1];
        //for (int i = 0; i < WorldSizeX * Const.ChunkSize; i++) {
        //    for (int k = 0; k < WorldSizeZ * Const.ChunkSize; k++) {
        //        for (int j = 0; j < WorldSizeY * Const.ChunkSize; j++) {
        //            lightCount[rtm.getLight(i, j, k)]++;
        //        }
        //    }
        //}
        //for (int i = 0; i < lightCount.Length; i++) {
        //    Debug.Log("light (" + i + ") = " + lightCount[i]);
        //}

        Texture2D lightMap = new Texture2D(256, 256, TextureFormat.RGBAHalf, false);

        Color[] colors = new Color[256 * 256];
        for (int i = 0; i < 256; i++)
        {
            for (int j = 0; j < 256; j++)
            {
                colors[j * 256 + i] = new Color(i / 256.0f, i / 256.0f, i / 256.0f, 1) * 2;
            }
        }
        lightMap.SetPixels(colors);
        lightMap.Apply();


        LightmapData[] lightmaps = new LightmapData[1];
        lightmaps[0]               = new LightmapData();
        lightmaps[0].lightmapFar   = lightMap;
        lightmaps[0].lightmapNear  = lightMap;
        LightmapSettings.lightmaps = lightmaps;

        GameObject itemsRoot = new GameObject("items");

        for (int i = 0; i < houseItems.Count; i++)
        {
            HouseItem  item   = houseItems[i];
            string     path   = HouseItemGenerator.GetItemPrefabPath(item.item);
            GameObject prefab = GlobalResources.loadPrefab(path);
            if (prefab != null)
            {
                GameObject inst = GameObject.Instantiate <GameObject>(prefab);
                inst.name               = houseItems[i].item.ToString();
                inst.transform.parent   = itemsRoot.transform;
                inst.transform.position = houseItems[i].pos + new Vector3(0.5f, 0f, 0.5f);
                MeshRenderer renderer = inst.GetComponentInChildren <MeshRenderer>();
                renderer.lightmapIndex = 0;
                inst.isStatic          = true;
                float light = rtm.getLight(item.pos.x, item.pos.y, item.pos.z) / (float)Const.MaxLightIteration;
                light = Mathf.Lerp(0.0f, 0.6f, light);
                renderer.lightmapScaleOffset = new Vector4(0, 0, light, 0f);
                //MeshFilter meshFilter = inst.GetComponentInChildren<MeshFilter>();
                //Mesh mesh = meshFilter.mesh;
                //Color32[] colors = new Color32[mesh.colors.Length];
                //byte c = (byte)(rtm.getLight(item.pos.x, item.pos.y, item.pos.z) * 255);
                //ArrayTool.SetAll(colors, new Color32(c,c,c,1));
                //mesh.colors32 = colors;
            }
            else
            {
                int a = 0;
            }
        }

        //初始化AO计算
        rma = new RayMarchingAo();
        rma.Init(rtm);

        //ViewVoxel viewVoxel = GameObject.FindObjectOfType<ViewVoxel>();
        //if (viewVoxel) {
        //    Texture3D voxelTex3D = rma.GetVoxelTexture();
        //    //OpenGLLibrary.glEnable(OpenGL.GL_TEXTURE_3D);
        //    //OpenGLLibrary.glBindTexture(OpenGL.GL_TEXTURE_3D, voxelTex3D.GetNativeTexturePtr());
        //    //byte[] data = new byte[voxelTex3D.width * voxelTex3D.height * voxelTex3D.depth * 4];
        //    //for (int i = 0; i < data.Length; i++ ){
        //    //    data[i] = 0x56;
        //    //}
        //    //OpenGL.TexSubImage3D(OpenGL.GL_TEXTURE_3D, 0, 0, 0, 0, 32, 32, 32, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, ref data);
        //    //OpenGL.TextureSubImage3D(voxelTex3D, 0, 0, 0, 0, 1, 1, 1, OpenGL.GL_RED, OpenGL.GL_UNSIGNED_BYTE, ref data);
        //    viewVoxel.SetVexelTex(voxelTex3D);
        //}

        //计算光照
        Vector3 sunDir = new Vector3(1, -3, 1);

        sunDir.Normalize();
        {
            //TerrainTool.calcLight(bm, rtm, rays, sunDir);
            //TerrainTool.calcLight2(bm,32);
        }
        DebugTool.Log("计算光照完成");
        yield return(null);

        Material mat = GlobalResources.getBlockMaterial();

        mat.mainTexture = texturePacker.GetPackedTexture();
        GameObject root = GameObject.Find("Root");

        if (root == null)
        {
            root = new GameObject("Root");
        }

        Level level = GameObject.FindObjectOfType <Level>();

        if (level)
        {
            level.scene = rtm;
        }

        //创建网格
        for (int i = 0; i < WorldSizeX; i++)
        {
            for (int k = 0; k < WorldSizeZ; k++)
            {
                for (int j = 0; j < WorldSizeY; j++)
                {
                    int baseX = i * Const.ChunkSize;
                    int baseY = j * Const.ChunkSize;
                    int baseZ = k * Const.ChunkSize;

                    BlockChunk chunk = new BlockChunk(bm, baseX, baseY, baseZ);

                    Vector3 chunkPos = new Vector3(i * Const.ChunkSize * Const.BlockSize, j * Const.ChunkSize * Const.BlockSize, k * Const.ChunkSize * Const.BlockSize);

                    //bool[,,] visibleBlocks = new bool[Const.ChunkSize,Const.ChunkSize,Const.ChunkSize];// = MeshTool.GetVisibleBlocks(chunk, blockTypeFun);
                    //TerrainTool.calcChunkLight(chunk, i, j, k, rtm, rays, sunDir);
                    //Mesh mesh = MeshTool.createMesh(chunk, blockTypeFun, 0, 0, 0);
                    for (int f = 0; f < 6; f++)
                    {
                        List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f);
                        //for (int s = 0; s < surface.Count; s++) {
                        //    visibleBlocks[surface[s].pos.x, surface[s].pos.y, surface[s].pos.z] = true;
                        //}


                        Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f);

                        RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f);

                        //回读亮度数据
                        RenderTexture.active = targetAoResult;
                        Texture2D readback = new Texture2D(targetAoResult.width, targetAoResult.height);
                        readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0);
                        MeshTool.SetRaytraceAo(surface, readback);
                        MeshTool.SetExtendAo(surface, rtm, f, new VecInt3(i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize));

                        //将可行走区域标记黑色
                        //if (f == (int)BlockFaceIndex.BFI_y1) {
                        //    for (int s = 0; s < surface.Count; s++) {
                        //        int gx = baseX + surface[s].pos.x;
                        //        int gy = baseY + surface[s].pos.y;
                        //        int gz = baseZ + surface[s].pos.z;
                        //        if (rtm.testBlock(gx, gy, gz, (byte)RayTraceBlockType.Walkable)) {
                        //            for (int v = 0; v < 4; v++) {
                        //                surface[s].raytraceAo[v] *= 0.1f;
                        //            }
                        //        }
                        //    }
                        //}

                        Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun);

                        if (mesh != null)
                        {
                            GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter));
                            obj.isStatic = true;
                            obj.GetComponent <Renderer>().material = mat;
                            obj.GetComponent <MeshFilter>().mesh   = mesh;
                            obj.transform.SetParent(root.transform);
                            obj.transform.position = chunkPos;
                        }
                    }

                    //physics.AddChunk(visibleBlocks, chunkPos);
                    yield return(null);
                }
            }
        }
        StaticBatchingUtility.Combine(root);
        StaticBatchingUtility.Combine(itemsRoot);
        DebugTool.Log("生成网格");
        yield return(null);
    }
Exemplo n.º 10
0
 public abstract void Undo(Block.BlockManager bm);
Exemplo n.º 11
0
 public override void Undo(Block.BlockManager bm)
 {
     funUndo(bm);
 }
Exemplo n.º 12
0
 public override void Do(Block.BlockManager bm)
 {
     funDo(bm);
 }
Exemplo n.º 13
0
 public BlockChunk(BlockManager bm, int baseX, int baseY, int baseZ)
 {
     this.blockManager = bm;
     this.baseX        = baseX; this.baseY = baseY; this.baseZ = baseZ;
 }
Exemplo n.º 14
0
 public static void calcLight2(BlockManager blockManager, int maxStep)
 {
     ////DebugTool.Log("开始光照计算");
     //
     //int Step = maxStep;
     //
     //List<VecInt3> surface = new List<VecInt3>();
     //const int StartLight = 0;
     //List<VecInt3>[] light = new List<VecInt3>[Step * 2 + StartLight];
     //for (int i = 0; i < light.Length; i++) {
     //    light[i] = new List<VecInt3>();
     //}
     //SByte[, ,] brightness = new SByte[blockManager.getSizeX() * Const.ChunkSize, blockManager.getSizeY() * Const.ChunkSize, blockManager.getSizeZ() * Const.ChunkSize];
     //int mbx = brightness.GetLength(0);
     //int mby = brightness.GetLength(1);
     //int mbz = brightness.GetLength(2);
     //
     //
     //for (int i = 0; i < blockManager.getSizeX(); i++) {
     //    for (int j = 0; j < blockManager.getSizeY(); j++) {
     //        for (int k = 0; k < blockManager.getSizeZ(); k++) {
     //            BlockChunk chunk = blockManager.getChunk(i, j, k);
     //            for (int x = 0; x < Const.ChunkSize; x++) {
     //                for (int y = 0; y < Const.ChunkSize; y++) {
     //                    for (int z = 0; z < Const.ChunkSize; z++) {
     //                        int gx = i * Const.ChunkSize + x;
     //                        int gy = j * Const.ChunkSize + y;
     //                        int gz = k * Const.ChunkSize + z;
     //                        short blockType = chunk.getBlock(x, y, z);
     //                        if (blockType != (short)BlockTypeEnum.Air) {
     //                            brightness[gx, gy, gz] = (SByte)Flag.Inner;
     //                        }
     //                        else {
     //                            brightness[gx, gy, gz] = (SByte)Flag.Air;
     //                        }
     //                    }
     //                }
     //            }
     //        }
     //    }
     //}
     //
     //for (int gx = 0; gx < mbx; gx++) {
     //    for (int gy = 0; gy < mby; gy++) {
     //        for (int gz = 0; gz < mbz; gz++) {
     //            if (brightness[gx, gy, gz] == (SByte)Flag.Inner) {
     //                bool bSurface = false;
     //                for (int f = 0; f < 6; f += 1) {
     //                    int adjx = gx + Const.AdjacencyOffset[f, 0];
     //                    int adjy = gy + Const.AdjacencyOffset[f, 1];
     //                    int adjz = gz + Const.AdjacencyOffset[f, 2];
     //
     //                    if (adjx >= 0 && adjx < mbx && adjy >= 0 && adjy < mby && adjz >= 0 && adjz < mbz) {
     //                        if (brightness[adjx, adjy, adjz] == (SByte)Flag.Air) {
     //                            brightness[adjx, adjy, adjz] = StartLight;
     //                            light[StartLight].Add(new VecInt3(adjx, adjy, adjz));
     //                        }
     //                        if (brightness[adjx, adjy, adjz] == StartLight) {
     //                            bSurface = true;
     //                        }
     //                    }
     //                }
     //                if (bSurface) {
     //                    brightness[gx, gy, gz] = (SByte)Flag.Surface;
     //                    surface.Add(new VecInt3(gx, gy, gz));
     //                }
     //            }
     //        }
     //    }
     //}
     //
     ////DebugTool.Log("第0步");
     ////收集亮度
     //for (int t = 0; t < Step - 1; t++) {
     //    for (int i = 0; i < light[t * 2].Count; i++) {
     //        VecInt3 v = light[t * 2][i];
     //        for (int f = 0; f < 6; f += 1) {
     //            int adjx = v.x + Const.AdjacencyOffset[f, 0];
     //            int adjy = v.y + Const.AdjacencyOffset[f, 1];
     //            int adjz = v.z + Const.AdjacencyOffset[f, 2];
     //            if (adjx >= 0 && adjx < mbx && adjy >= 0 && adjy < mby && adjz >= 0 && adjz < mbz) {
     //                if (brightness[adjx, adjy, adjz] == (short)Flag.Air) {
     //                    brightness[adjx, adjy, adjz] = (SByte)(t * 2 + 2);
     //                    light[t * 2 + 2].Add(new VecInt3(adjx, adjy, adjz));
     //                }
     //            }
     //        }
     //    }
     //}
     //
     ////DebugTool.Log("第1步");
     //
     ////亮度扩散
     //for (int t = Step * 2 - 1; t > 0; t--) {
     //    for (int i = 0; i < light[t].Count; i++) {
     //        VecInt3 v = light[t][i];
     //        SByte targetBrightness = (SByte)(t - 1);
     //        for (int f = 0; f < 6; f += 1) {
     //            int adjx = v.x + Const.AdjacencyOffset[f, 0];
     //            int adjy = v.y + Const.AdjacencyOffset[f, 1];
     //            int adjz = v.z + Const.AdjacencyOffset[f, 2];
     //            if (adjx >= 0 && adjx < mbx && adjy >= 0 && adjy < mby && adjz >= 0 && adjz < mbz) {
     //                if (brightness[adjx, adjy, adjz] < targetBrightness && brightness[adjx, adjy, adjz] >= 0) {
     //                    brightness[adjx, adjy, adjz] = targetBrightness;
     //                    light[targetBrightness].Add(new VecInt3(adjx, adjy, adjz));
     //                }
     //            }
     //        }
     //    }
     //}
     //
     //
     ////DebugTool.Log("第2步");
     //
     ////for (int i = 0; i < brightness.GetLength(0); i++) {
     ////    for (int j = 0; j < brightness.GetLength(1); j++) {
     ////        for (int k = 0; k < brightness.GetLength(2); k++) {
     ////            if (brightness[i, j, k] == Step * 2 - 6) {
     ////                debugTool.addLine(new Vector3(i, j, k), new Vector3(i, j, k) + Vector3.one * 0.5f, Color.red);;
     ////            }
     ////        }
     ////    }
     ////}
     //
     ////应用亮度
     //for (int a = 0; a < surface.Count; a++) {
     //    VecInt3 v = surface[a];
     //    int i = v.x / Const.ChunkSize;
     //    int j = v.y / Const.ChunkSize;
     //    int k = v.z / Const.ChunkSize;
     //    int x = v.x % Const.ChunkSize;
     //    int y = v.y % Const.ChunkSize;
     //    int z = v.z % Const.ChunkSize;
     //    BlockChunk chunk = blockManager.getChunk(i, j, k);
     //    SByte curValue = brightness[v.x, v.y, v.z];
     //    for (int f = 0; f < 6; f++) {
     //        int adjx = v.x + Const.AdjacencyOffset[f, 0];
     //        int adjy = v.y + Const.AdjacencyOffset[f, 1];
     //        int adjz = v.z + Const.AdjacencyOffset[f, 2];
     //        int ladjx = x + Const.AdjacencyOffset[f, 0];
     //        int ladjy = y + Const.AdjacencyOffset[f, 1];
     //        int ladjz = z + Const.AdjacencyOffset[f, 2];
     //
     //        if (adjx >= 0 && adjx < mbx && adjy >= 0 && adjy < mby && adjz >= 0 && adjz < mbz) {
     //            SByte adjValue = brightness[adjx, adjy, adjz];
     //            if (adjValue >= 0) {
     //                if (adjValue == 0) {
     //                    int ccc = 0;
     //                }
     //                float l = Mathf.Clamp((float)adjValue / (Step), 0, 1);
     //                //l = Mathf.Pow(l, 0.8f);
     //                l = Mathf.Lerp(0.0f, 1, l);
     //                Vector3 faceNormal = Const.getFaceNormal(f);
     //                float faceLightFactor = Vector3.Dot(faceNormal, Vector3.up);
     //                float directLight = faceLightFactor;
     //                l = l * 0.9f + directLight * 0.1f;
     //                l = Mathf.Clamp(l,0,0.999f);
     //                byte bright = (byte)(Const.MaxBrightness * l);
     //                chunk.setBrightness(x, y, z, f, (byte)bright);
     //            }
     //        }
     //    }
     //}
     //
     ////DebugTool.Log("计算完成");
     //
 }
Exemplo n.º 15
0
    void Start()
    {
        SimpleModelFactory.CreateAll();
        texturePacker = new TexturePacker();
        Block.BlockManager bm = new Block.BlockManager();

        for (int i = 0; i < (int)Game.BlockType.Num; i++)
        {
            for (int f = 0; f < 6; f++)
            {
                Game.BlockType block = (Game.BlockType)i;
                texturePacker.AddTexture(block, f, TextureNameConfig.GetTextureName(block, f));
            }
        }


        texturePacker.Pack();
        packedTexture = texturePacker.GetPackedTexture();

        BlockTypeFun blockTypeFun = new BlockTypeFun();

        blockTypeFun.texturePacker = texturePacker;

        //float startTime = Time.realtimeSinceStartup;
        int WorldSizeX = 1;
        int WorldSizeY = 1;
        int WorldSizeZ = 1;

        //申请内存
        bm.create(WorldSizeX * Const.ChunkSize, WorldSizeY * Const.ChunkSize, WorldSizeZ * Const.ChunkSize, blockTypeFun);
        bm.setBlock(3, 4, 4, (int)Game.BlockType.StairLeft);
        bm.setBlock(5, 4, 4, (int)Game.BlockType.StairRight);
        bm.setBlock(4, 4, 3, (int)Game.BlockType.StairBack);
        bm.setBlock(4, 4, 5, (int)Game.BlockType.StairFront);

        //光线追踪初始化
        Block.RayCastManager rtm = new Block.RayCastManager();
        rtm.create(bm.SizeX, bm.SizeY, bm.SizeZ);
        for (int x = 0; x < bm.SizeX; x++)
        {
            for (int y = 0; y < bm.SizeY; y++)
            {
                for (int z = 0; z < bm.SizeZ; z++)
                {
                    rtm.setBlock(x, y, z, bm.getBlock(x, y, z) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing);
                }
            }
        }


        //初始化AO计算
        rma = new Block.RayMarchingAo();
        rma.Init(rtm);
        ViewVoxel viewVoxel = GameObject.FindObjectOfType <ViewVoxel>();

        if (viewVoxel)
        {
            Texture3D voxelTex3D = rma.GetVoxelTexture();
            viewVoxel.SetVexelTex(voxelTex3D);
        }

        Material mat = GlobalResources.getBlockMaterial();

        mat.mainTexture = texturePacker.GetPackedTexture();
        GameObject root = this.gameObject;

        if (root == null)
        {
            root = new GameObject("Root");
        }

        //创建网格
        for (int i = 0; i < WorldSizeX; i++)
        {
            for (int k = 0; k < WorldSizeZ; k++)
            {
                for (int j = 0; j < WorldSizeY; j++)
                {
                    Block.BlockChunk chunk = new BlockChunk(bm, i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize);
                    //TerrainTool.calcChunkLight(chunk, i, j, k, rtm, rays, sunDir);
                    //Mesh mesh = MeshTool.createMesh(chunk, blockTypeFun, 0, 0, 0);
                    for (int f = 0; f < 6; f++)
                    {
                        List <Block.MeshTool.BlockSurface> surface = Block.MeshTool.getChunkSurface(chunk, blockTypeFun, f);
                        Texture2D texSurface = Block.MeshTool.SurfacePointsToTexture(surface, f);

                        RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Block.Const.ChunkSize, f);

                        //回读亮度数据
                        RenderTexture.active = targetAoResult;
                        Texture2D readback = new Texture2D(targetAoResult.width, targetAoResult.height);
                        readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0);
                        Block.MeshTool.SetRaytraceAo(surface, readback);
                        Mesh mesh = Block.MeshTool.createMesh2(surface, f, blockTypeFun);

                        if (mesh != null)
                        {
                            GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter));
                            obj.isStatic = true;
                            obj.GetComponent <Renderer>().material = mat;
                            obj.GetComponent <MeshFilter>().mesh   = mesh;
                            obj.transform.SetParent(root.transform);
                            obj.transform.position = new Vector3(i * Block.Const.ChunkSize * Block.Const.BlockSize, j * Block.Const.ChunkSize * Block.Const.BlockSize, k * Block.Const.ChunkSize * Block.Const.BlockSize);
                        }
                    }
                }
            }
        }
    }