Esempio n. 1
0
 static void setBlock(BlockManager blockManger, int posX, int posY, int posZ, short block)
 {
     //int cx = posX / Const.ChunkSize;
     //int bx = posX % Const.ChunkSize;
     //int cy = posY / Const.ChunkSize;
     //int by = posY % Const.ChunkSize;
     //int cz = posZ / Const.ChunkSize;
     //int bz = posZ % Const.ChunkSize;
     blockManger.setBlock(posX, posY, posZ, block);
 }
Esempio n. 2
0
        public static void createTerrain(BlockManager blockManager, int WorldSizeX, int WorldSizeY, int WorldSizeZ)
        {
            NodeBase nodeHeight = null;
            //PerlinNoise perlin = new PerlinNoise(0);
            NodeManager nodeManager = NodeManager.load("default");

            nodeManager.forEachNodes((n) =>
            {
                if (n.node.value.getNodeType() == NodeType.HeightOutput)
                {
                    nodeHeight = n.node.value;
                }
            });
            if (nodeHeight != null)
            {
                //创建地形

                //float[,] values = nodeHeight.update(0, blockManager.baseX, blockManager.baseZ, blockManager.SizeX, blockManager.SizeZ);
                for (int x = 0; x < blockManager.SizeX; x++)
                {
                    for (int z = 0; z < blockManager.SizeZ; z++)
                    {
                        float density2d = 0.3f;// values[x, z];
                        for (int y = 0; y < blockManager.SizeY; y++)
                        {
                            //float s = 0.05f;
                            double density3d = 0;// perlin.Noise(rx * 1.23 * s, ry * 1.23 * s, rz * 1.23 * s) * 0.8;
                            if (y < WorldSizeY * Const.ChunkSize * (density2d + density3d - 0.2f) * 0.5f)
                            {
                                blockManager.setBlock(x, y, z, (short)BlockTypeEnum.Sand);
                            }
                            else
                            {
                                blockManager.setBlock(x, y, z, (short)BlockTypeEnum.Air);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
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]);
             }
         }
     }
 }
Esempio n. 4
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);
             }
         }
     }
 }
Esempio n. 5
0
    public void DoGenerate()
    {
        float startTime = Time.realtimeSinceStartup;

        bm.create(numX * Const.ChunkSize, numY * Const.ChunkSize, numZ * Const.ChunkSize, new BlockTypeFun());

        for (int x = 0; x < bm.SizeX; x++)
        {
            for (int y = 0; y < bm.SizeY; y++)
            {
                for (int z = 0; z < bm.SizeZ; z++)
                {
                    //bm.setBlock(x, y, z, (short)(z < y ? Block.BlockTypeEnum.Air : Block.BlockTypeEnum.Sand));
                    bm.setBlock(x, y, z, (short)(y < 10 || (x == 20 && z > 10 && z <= 15) ? Block.BlockTypeEnum.Sand : Block.BlockTypeEnum.Air));
                }
            }
        }

        rtm = new Block.RayCastManager();
        rtm.create(numX, numY, numZ);
        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);
                }
            }
        }
        rma = new Block.RayMarchingAo();
        rma.Init(rtm);

        viewVoxel.SetVexelTex(rma.GetVoxelTexture());

        Debug.Log("生成完体素数据," + (Time.realtimeSinceStartup - startTime));
        StartCoroutine(GenerateMesh());
    }
Esempio n. 6
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);
                        }
                    }
                }
            }
        }
    }