コード例 #1
0
    public void ReplaceObjectType(Game.BlockType newValue)
    {
        var index     = GameComponentsLookup.ObjectType;
        var component = (ObjectTypeComponent)CreateComponent(index, typeof(ObjectTypeComponent));

        component.value = newValue;
        ReplaceComponent(index, component);
    }
コード例 #2
0
ファイル: TexturePacker.cs プロジェクト: wachel/block
    public static Color GetColor(Game.BlockType block, int faceIndex)
    {
        switch (block)
        {
        case Game.BlockType.Leaves: return(Color.green);

        default: return(Color.white);
        }
    }
コード例 #3
0
ファイル: main.cs プロジェクト: wachel/block
    public override bool isOpacity(short block)
    {
        Game.BlockType blockType = (Game.BlockType)block;
        switch (blockType)
        {
        case Game.BlockType.Air: return(false);

        case Game.BlockType.StairLeft: return(true);

        default: return(true);
        }
    }
コード例 #4
0
ファイル: TexturePacker.cs プロジェクト: wachel/block
    public static string GetTextureName(Game.BlockType block, int faceIndex)
    {
        switch (block)
        {
        case Game.BlockType.Sand: return("sand");

        case Game.BlockType.Stone: return("stone");

        case Game.BlockType.WhiteStone: return("white_stone");

        case Game.BlockType.Log: return(faceIndex == 2 || faceIndex == 3 ? "log_acacia_top":"log_acacia");

        case Game.BlockType.StoneBrickWhiteWall0: return(faceIndex == 0 ? "white_stone" : "stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StoneBrickWhiteWall1: return(faceIndex == 1 ? "white_stone" : "stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StoneBrickWhiteWall4: return(faceIndex == 4 ? "white_stone" : "stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StoneBrickWhiteWall5: return(faceIndex == 5 ? "white_stone" : "stonebrick_mossy/stonebrick_3");   //red_brick

        case Game.BlockType.Leaves: return("leaves_acacia");

        case Game.BlockType.WoodFloor: return("wood_floor");

        case Game.BlockType.RedSand: return("red_sand");

        case Game.BlockType.WoolColoredBlack: return("wool_colored_black");

        case Game.BlockType.StoneSlab: return(faceIndex == 2 || faceIndex == 3 ? "stone_slab_top" : "stone_slab_side");

        case Game.BlockType.HardenedClayStainedBlue: return("hardened_clay_stained_blue");

        case Game.BlockType.StairLeft: return("stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StairRight: return("stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StairBack: return("stonebrick_mossy/stonebrick_3");

        case Game.BlockType.StairFront: return("stonebrick_mossy/stonebrick_3");

        default: return("sand");
        }
    }
コード例 #5
0
ファイル: TexturePacker.cs プロジェクト: wachel/block
 public void AddTexture(Game.BlockType block, int faceIndex, string texName)
 {
     names[(int)block * 6 + faceIndex] = texName;
 }
コード例 #6
0
ファイル: TexturePacker.cs プロジェクト: wachel/block
    public Rect GetRect(Game.BlockType block, int faceIndex)
    {
        string name = names[(int)block * 6 + faceIndex];

        return(packResult[name]);
    }
コード例 #7
0
ファイル: main.cs プロジェクト: 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);
    }
コード例 #8
0
    public static Game.BlockType[, ,] CreateBuilding(FloorType[,] layout, bool addRoof)
    {
        int w          = layout.GetLength(0);
        int h          = 7;
        int roofHeight = 8;
        int l          = layout.GetLength(1);

        Game.BlockType[, ,] rlt = new Game.BlockType[w, h + roofHeight, l];

        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < l; j++)
            {
                FloorType floor = layout[i, j];

                FloorType left  = i == 0 ? FloorType.Out : layout[i - 1, j];
                FloorType right = i == w - 1 ? FloorType.Out : layout[i + 1, j];
                FloorType back  = j == 0 ? FloorType.Out : layout[i, j - 1];
                FloorType front = j == l - 1 ? FloorType.Out : layout[i, j + 1];

                if (floor == FloorType.Out)
                {
                    if (left == FloorType.DoorLeft)
                    {
                        rlt[i, 0, j] = Game.BlockType.StairRight;
                    }
                    else if (right == FloorType.DoorRight)
                    {
                        rlt[i, 0, j] = Game.BlockType.StairLeft;
                    }
                    else if (back == FloorType.DoorDown)
                    {
                        rlt[i, 0, j] = Game.BlockType.StairFront;
                    }
                    else if (front == FloorType.DoorUp)
                    {
                        rlt[i, 0, j] = Game.BlockType.StairBack;
                    }
                    //|| right == FloorType.DoorRight || up == FloorType.DoorUp || down == FloorType.DoorDown) {
                    //    rlt[i, 0, j] = Game.BlockType.WoolColoredBlack;
                    //}
                }
                else if (floor == FloorType.Wall || floor == FloorType.Window || HouseLayout.IsDoor(floor))
                {
                    for (int k = 0; k < h; k++)
                    {
                        if (left == FloorType.Out)
                        {
                            rlt[i, k, j] = Game.BlockType.StoneBrickWhiteWall1;
                        }
                        else if (right == FloorType.Out)
                        {
                            rlt[i, k, j] = Game.BlockType.StoneBrickWhiteWall0;
                        }
                        else if (front == FloorType.Out)
                        {
                            rlt[i, k, j] = Game.BlockType.StoneBrickWhiteWall4;
                        }
                        else if (back == FloorType.Out)
                        {
                            rlt[i, k, j] = Game.BlockType.StoneBrickWhiteWall5;
                        }
                        else
                        {
                            rlt[i, k, j] = Game.BlockType.WhiteStone;
                        }
                    }
                    if (floor == FloorType.Window)
                    {
                        for (int k = 3; k < 5; k++)
                        {
                            rlt[i, k, j] = Game.BlockType.Air;
                        }
                    }
                    else if (HouseLayout.IsDoor(floor))
                    {
                        rlt[i, 0, j] = Game.BlockType.WoolColoredBlack;
                        for (int k = 1; k < 5; k++)
                        {
                            rlt[i, k, j] = Game.BlockType.Air;
                        }
                    }
                }
                else
                {
                    if (floor == FloorType.Kitchen || floor == FloorType.Bathroom)
                    {
                        rlt[i, 0, j] = Game.BlockType.StoneSlab;
                    }
                    else
                    {
                        rlt[i, 0, j] = Game.BlockType.WoodFloor;
                    }
                }
            }
        }
        //生成屋顶
        if (addRoof)
        {
            bool[,] blocks    = new bool[w, l];
            bool[,] blocksOld = new bool[w, l];
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < l; j++)
                {
                    blocks[i, j]    = layout[i, j] != FloorType.Out;
                    blocksOld[i, j] = blocks[i, j];
                }
            }
            for (int i = 1; i < w - 1; i++)
            {
                for (int j = 1; j < l - 1; j++)
                {
                    blocks[i, j] = blocksOld[i, j] || blocksOld[i - 1, j] || blocksOld[i, j - 1] || blocksOld[i + 1, j] || blocksOld[i, j + 1] ||
                                   blocksOld[i - 1, j - 1] || blocksOld[i - 1, j + 1] || blocksOld[i + 1, j - 1] || blocksOld[i + 1, j + 1];
                }
            }

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < l; j++)
                {
                    blocksOld[i, j] = blocks[i, j];
                }
            }

            Game.BlockType[] roofTypes = { Game.BlockType.HardenedClayStainedBlue, Game.BlockType.RedSand };
            Game.BlockType   roofType  = roofTypes[FakeRandom.Range(0, roofTypes.Length)];

            for (int k = 0; k < roofHeight; k++)
            {
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < l; j++)
                    {
                        if (blocks[i, j])
                        {
                            if (k == 0)
                            {
                                rlt[i, k + h, j] = layout[i, j] == FloorType.Out ? roofType : Game.BlockType.WhiteStone;
                            }
                            else
                            {
                                rlt[i, k + h, j] = roofType;
                            }
                        }
                    }
                }
                bool[,] temp = blocksOld;
                blocksOld    = blocks;
                blocks       = temp;
                for (int i = 1; i < w - 1; i++)
                {
                    for (int j = 1; j < l - 1; j++)
                    {
                        blocks[i, j] = blocksOld[i, j] && blocksOld[i - 1, j] && blocksOld[i, j - 1] && blocksOld[i + 1, j] && blocksOld[i, j + 1];
                    }
                }
            }
        }
        return(rlt);
    }
コード例 #9
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);
                        }
                    }
                }
            }
        }
    }