Ejemplo n.º 1
0
        public static void Main(TerrainDataGen obj)
        {
            game_obj = obj;
            {
                CSharpFunction cSharpF = new CSharpFunction();

                // 创建中间件
                bool sucess = CFunction.Create(CFunction.DLL_DIR);
                if (!sucess)
                {
                    CFunction.CoreTrace("Create failed");
                    return;
                }
                CFunction.CoreTrace("Create sucess");

                // 设置地形路径
                CFunction.SetTerrainPath(@"./map/ter/111/");

                // 初始化中间件
                sucess = CFunction.Init(CFunction.FILE_NAME);
                if (!sucess)
                {
                    CFunction.CoreTrace("Init failed");
                    return;
                }
                CFunction.CoreTrace("Init sucess");

                cSharpF.SetTraceDetail();
                cSharpF.SetTraceSphere();
                cSharpF.SetTraceHitted();
                cSharpF.SetGetGroundHeight();
                cSharpF.SetGetWaterExists();
                cSharpF.SetGetWaterBaseHeight();

                int terrain_width  = (int)Terrain.activeTerrain.terrainData.size.x;
                int terrain_height = (int)Terrain.activeTerrain.terrainData.size.z;

                uint zone_scale = CFunction.GetZoneScale();
                int  zone_rows  = (int)(terrain_height / zone_scale);
                int  zone_cols  = (int)(terrain_width / zone_scale);

                // 高度数据
                uint    rows = zone_scale + 1;
                uint    cols = zone_scale + 1;
                float[] hv   = new float[rows * cols];

                for (int r = 0; r < zone_rows; ++r)
                {
                    for (int c = 0; c < zone_cols; ++c)
                    {
                        int zone_index = r * zone_cols + c;
                        // 区域索引
                        System.String zone_name = "";

                        IntPtr intPtr = CFunction.GetZoneName(zone_index);
                        if (intPtr == (IntPtr)0)
                        {
                            CFunction.CoreTrace("GetZoneName failed");
                            continue;
                        }

                        zone_name = Marshal.PtrToStringAnsi(intPtr);


                        sucess = (zone_name != "");
                        if (!sucess)
                        {
                            CFunction.CoreTrace("zone_name error");
                            continue;
                        }
                        CFunction.CoreTrace("GetZoneName sucess");

                        float min_y           = 1e8f;
                        float max_y           = -1e8f;
                        int   zone_row_offset = (int)(r * zone_scale);
                        int   zone_col_offset = (int)(c * zone_scale);

                        float zone_left = 0.0f;
                        float zone_top  = 0.0f;
                        CFunction.GetZoneLeft(zone_index, ref zone_left);
                        CFunction.GetZoneTop(zone_index, ref zone_top);
                        for (uint i = 0; i < rows; ++i)
                        {
                            for (uint j = 0; j < cols; ++j)
                            {
                                hv[i * cols + j] = game_obj.GetGroundHeight(zone_left + j, zone_top + i);
                                if (hv[i * cols + j] < min_y)
                                {
                                    min_y = hv[i * cols + j];
                                }

                                if (hv[i * cols + j] > max_y)
                                {
                                    max_y = hv[i * cols + j];
                                }
                            }
                        }
                        IntPtr heighs = Marshal.AllocHGlobal((int)(sizeof(float) * hv.Length));
                        Marshal.Copy(hv, 0, heighs, hv.Length);
                        IntPtr normals = new IntPtr();

                        sucess = CFunction.BuildZone(zone_index, rows, cols, min_y, max_y, heighs, normals);
                        if (!sucess)
                        {
                            CFunction.CoreTrace("BuildZone failed");
                            continue;
                        }

                        CFunction.CoreTrace("BuildZone sucess");

                        sucess = CFunction.GenWalkBegin(zone_index, 1, 1.8f, 0.2f, 0.5f, 100.0f, 0.0f, 0.7f, 0.34f, 15.0f);
                        if (!sucess)
                        {
                            CFunction.CoreTrace("GenWalkBegin failed");
                            continue;
                        }
                        CFunction.CoreTrace("GenWalkBegin sucess");

                        //sucess = CFunction.GenWalkAddPermit(zone_index, 0, 10.0f, 0);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkAddPermit failed");
                        //    continue;
                        //}
                        //CFunction.CoreTrace("GenWalkAddPermit sucess");

                        //sucess = CFunction.GenWalkAddForbid(zone_index, 0, 0, 0, 1);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkAddForbid failed");
                        //    continue;
                        //}
                        //CFunction.CoreTrace("GenWalkAddForbid sucess");

                        sucess = CFunction.GenWalkGround(zone_index);
                        if (!sucess)
                        {
                            CFunction.CoreTrace("GenWalkGround failed");
                            continue;
                        }
                        CFunction.CoreTrace("GenWalkGround sucess");

                        int row = 0;

                        while (sucess)
                        {
                            sucess = CFunction.GenWalkBalk(zone_index, row);

                            row = row + 1;
                        }

                        float left = 0;
                        float top  = 0;
                        CFunction.GetZoneLeft(zone_index, ref left);
                        CFunction.GetZoneTop(zone_index, ref top);

                        float zone_center_x = zone_scale * (r + 0.5f);
                        float zone_center_z = zone_scale * (c + 0.5f);
                        float zone_center_y = game_obj.GetGroundHeight(zone_center_x, zone_center_z);

                        sucess = CFunction.GenWalkable(zone_index, zone_center_x + left, zone_center_y, zone_center_z + top);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkable failed");
                        //    continue;
                        //}
                        CFunction.CoreTrace("GenWalkable sucess");

                        sucess = CFunction.GenWalkFloor(zone_index);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkFloor failed");
                        //    continue;
                        //}
                        CFunction.CoreTrace("GenWalkFloor sucess");

                        sucess = CFunction.GenWalkWall(zone_index);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkWall failed");
                        //    continue;
                        //}
                        CFunction.CoreTrace("GenWalkWall sucess");

                        sucess = CFunction.GenWalkWater(zone_index);
                        //if (!sucess)
                        //{
                        //    CFunction.CoreTrace("GenWalkWater failed");
                        //   continue;
                        //}
                        CFunction.CoreTrace("GenWalkWater sucess");

                        sucess = CFunction.GenWalkEnd(zone_index);
                        if (!sucess)
                        {
                            CFunction.CoreTrace("GenWalkEnd failed");
                            continue;
                        }
                        CFunction.CoreTrace("GenWalkEnd sucess");

                        sucess = CFunction.SaveWalk(zone_index, CFunction.FILE_NAME + "walk/" + zone_name + ".walk");
                        if (!sucess)
                        {
                            CFunction.CoreTrace("SaveWalk failed");
                            continue;
                        }
                        CFunction.CoreTrace("SaveWalk sucess");

                        sucess = CFunction.SaveHeight(zone_index, CFunction.FILE_NAME + zone_name + ".height");
                        if (!sucess)
                        {
                            CFunction.CoreTrace("SaveHeight failed");
                            continue;
                        }
                        CFunction.CoreTrace("SaveHeight sucess");

                        sucess = CFunction.SaveHeightCompress(zone_index, CFunction.FILE_NAME + zone_name + ".ground");
                        if (!sucess)
                        {
                            CFunction.CoreTrace("SaveHeightCompress failed");
                            continue;
                        }
                        CFunction.CoreTrace("SaveHeightCompress sucess");
                    }
                }

                // 销毁中间件
                CFunction.Destroy();
            }
        }
Ejemplo n.º 2
0
 // 获得精确的地面高度实现
 public float GetGroundHeight(float x, float z)
 {
     return(game_obj.GetGroundHeight(x, z));
 }