Beispiel #1
0
    // Update is called once per frame
    public void EditorUpdate(Camera cam)
    {
        //Event e = Event.current;
        //if (e.type == EventType.MouseDown) {
        //    mouseDownPos = e.mousePosition;
        //    if (e.button == 0) {
        //        if (curOperator == Operator.AddBlock) {
        //            if (bShowPreview) {
        //                int gx = (int)previewBlockPos.x;
        //                int gy = (int)previewBlockPos.y;
        //                int gz = (int)previewBlockPos.z;
        //                blockManager.setBlock(gx, gy, gz, (short)BlockTypeEnum.BT_Sand);
        //                bDirty = true;
        //            }
        //        }
        //        if (curOperator == Operator.DeleteBlock) {
        //            if (bShowPreview) {
        //                int gx = (int)selectedBlockPos.x;
        //                int gy = (int)selectedBlockPos.y;
        //                int gz = (int)selectedBlockPos.z;
        //                blockManager.setBlock(gx, gy, gz, (short)BlockTypeEnum.BT_Air);
        //                bDirty = true;
        //            }
        //        }
        //    }
        //}
        //else if (e.type == EventType.mouseMove) {
        //    if (curOperator == Operator.AddBlock || curOperator == Operator.DeleteBlock) {
        //        Ray ray = cam.ScreenPointToRay(new Vector3(e.mousePosition.x, cam.pixelHeight - e.mousePosition.y, 0));
        //        Vector3 startPos = transform.InverseTransformPoint(ray.origin);
        //        Vector3 dir = transform.InverseTransformDirection(ray.direction);
        //        RayCastRestult rlt = rtm.rayCast(startPos, dir, 50);
        //        bShowPreview = rlt.bHit;
        //        if (rlt.bHit) {
        //            //Debug.Log(rlt.hitFaceIndex.ToString());
        //            if (curOperator == Operator.AddBlock) {
        //                Vector3 hitPos = startPos + dir * (rlt.hitLength + 0.001f);
        //                float gx = (int)(hitPos.x / Const.BlockSize) + 0.5f;
        //                float gy = (int)(hitPos.y / Const.BlockSize) + 0.5f;
        //                float gz = (int)(hitPos.z / Const.BlockSize) + 0.5f;
        //                selectedBlockPos = new Vector3(gx, gy, gz);
        //                previewBlockPos = new Vector3(gx, gy, gz) + Const.getFaceNormal(rlt.hitFaceIndex) * Const.BlockSize;
        //            }
        //            else if (curOperator == Operator.DeleteBlock) {
        //                Vector3 hitPos = startPos + dir * (rlt.hitLength + 0.001f);
        //                float gx = (int)(hitPos.x / Const.BlockSize) + 0.5f;
        //                float gy = (int)(hitPos.y / Const.BlockSize) + 0.5f;
        //                float gz = (int)(hitPos.z / Const.BlockSize) + 0.5f;
        //                selectedBlockPos = new Vector3(gx, gy, gz);
        //            }
        //        }
        //    }
        //}

        if (bDirty)
        {
            bDirty = false;
            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);

            m_meshes.Clear();
            DebugTool.Log("开始计算光照");
            TerrainTool.calcLight2(blockManager, 32);
            DebugTool.Log("开始更新网格");

            BlockTypeFun blockTypeFun = new BlockTypeFun();

            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);

                            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);
                            Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun);
                            if (mesh)
                            {
                                Vector3 pos = ((new Vector3(i, j, k))) * Const.ChunkSize;
                                m_meshes.Add(new MeshInfo(mesh, pos));
                            }
                        }
                    }
                }
            }
            DebugTool.Log("结束");
        }
    }
Beispiel #2
0
    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("结束");
        }
    }