示例#1
0
        // Create a mesh for the terrain of the current scene
        public static BInstance CreateTerrainMesh(
            Scene scene,
            PrimToMesh assetMesher, IAssetFetcher assetFetcher)
        {
            ITerrainChannel terrainDef = scene.Heightmap;
            int             XSize      = terrainDef.Width;
            int             YSize      = terrainDef.Height;

            float[,] heightMap = new float[XSize, YSize];
            if (ConvOAR.Globals.parms.P <bool>("HalfRezTerrain"))
            {
                ConvOAR.Globals.log.DebugFormat("{0}: CreateTerrainMesh. creating half sized terrain sized <{1},{2}>", LogHeader, XSize / 2, YSize / 2);
                // Half resolution mesh that approximates the heightmap
                heightMap = new float[XSize / 2, YSize / 2];
                for (int xx = 0; xx < XSize; xx += 2)
                {
                    for (int yy = 0; yy < YSize; yy += 2)
                    {
                        float here = terrainDef.GetHeightAtXYZ(xx + 0, yy + 0, 26);
                        float ln   = terrainDef.GetHeightAtXYZ(xx + 1, yy + 0, 26);
                        float ll   = terrainDef.GetHeightAtXYZ(xx + 0, yy + 1, 26);
                        float lr   = terrainDef.GetHeightAtXYZ(xx + 1, yy + 1, 26);
                        heightMap[xx / 2, yy / 2] = (here + ln + ll + lr) / 4;
                    }
                }
            }
            else
            {
                ConvOAR.Globals.log.DebugFormat("{0}: CreateTerrainMesh. creating terrain sized <{1},{2}>", LogHeader, XSize / 2, YSize / 2);
                for (int xx = 0; xx < XSize; xx++)
                {
                    for (int yy = 0; yy < YSize; yy++)
                    {
                        heightMap[xx, yy] = terrainDef.GetHeightAtXYZ(xx, yy, 26);
                    }
                }
            }

            // Number found in RegionSettings.cs as DEFAULT_TERRAIN_TEXTURE_3
            OMV.UUID convoarID = new OMV.UUID(ConvOAR.Globals.parms.P <string>("ConvoarID"));

            OMV.UUID defaultTextureID = new OMV.UUID("179cdabd-398a-9b6b-1391-4dc333ba321f");
            OMV.Primitive.TextureEntryFace terrainFace = new OMV.Primitive.TextureEntryFace(null);
            terrainFace.TextureID = defaultTextureID;

            EntityHandleUUID terrainTextureHandle = new EntityHandleUUID();
            MaterialInfo     terrainMaterialInfo  = new MaterialInfo(terrainFace);

            if (ConvOAR.Globals.parms.P <bool>("CreateTerrainSplat"))
            {
                // Use the OpenSim maptile generator to create a texture for the terrain
                var terrainRenderer = new TexturedMapTileRenderer();
                Nini.Config.IConfigSource config = new Nini.Config.IniConfigSource();
                terrainRenderer.Initialise(scene, config);

                var mapbmp = new Bitmap(terrainDef.Width, terrainDef.Height,
                                        System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                terrainRenderer.TerrainToBitmap(mapbmp);

                // Place the newly created image into the Displayable caches
                ImageInfo terrainImageInfo = new ImageInfo();
                terrainImageInfo.handle    = terrainTextureHandle;
                terrainImageInfo.image     = mapbmp;
                terrainImageInfo.resizable = false; // terrain image resolution is not reduced
                assetFetcher.Images.Add(new BHashULong(terrainTextureHandle.GetHashCode()), terrainTextureHandle, terrainImageInfo);
                // Store the new image into the asset system so it can be read later.
                assetFetcher.StoreTextureImage(terrainTextureHandle, scene.Name + " Terrain", convoarID, mapbmp);
                // Link this image to the material
                terrainFace.TextureID = terrainTextureHandle.GetUUID();
            }
            else
            {
                // Use the default texture code for terrain
                terrainTextureHandle = new EntityHandleUUID(defaultTextureID);
                BHash terrainHash = new BHashULong(defaultTextureID.GetHashCode());
                assetFetcher.GetImageInfo(terrainHash, () => {
                    ImageInfo terrainImageInfo = new ImageInfo();
                    terrainImageInfo.handle    = terrainTextureHandle;
                    assetFetcher.FetchTextureAsImage(terrainTextureHandle)
                    .Then(img => {
                        terrainImageInfo.image = img;
                    });
                    return(terrainImageInfo);
                });
            }

            // The above has created a MaterialInfo for the terrain texture

            ConvOAR.Globals.log.DebugFormat("{0}: CreateTerrainMesh. calling MeshFromHeightMap", LogHeader);
            DisplayableRenderable terrainDisplayable = assetMesher.MeshFromHeightMap(heightMap,
                                                                                     terrainDef.Width, terrainDef.Height, assetFetcher, terrainFace);

            BInstance   terrainInstance = new BInstance();
            Displayable terrainDisp     = new Displayable(terrainDisplayable);

            terrainDisp.name               = "Terrain";
            terrainDisp.baseUUID           = OMV.UUID.Random();
            terrainInstance.Representation = terrainDisp;

            return(terrainInstance);
        }