public MTerrainHandler(MAstroBody body) : base(EType.Other, "TerrainHandler") { CurrentBody = body; Tiles = new Dictionary <string, MTerrainTile>(); Container = new MObject(EType.Other, "container"); }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { MAstroBody b = MPlanetHandler.CurrentNear; if (b == null) { return; } MTerrainTile tile = b.CurrentTile; if (tile == null) { return; } /* * if ((tile.PhysicsIsComplete == true) && (tile.GrassPlanter.IsComplete == false)) * { * tile.GrassPlanter.PrepareGrass(tile); * } */ lock (matlocker) { PlantGrass(b, tile); } }
// coordinates are entered lat lon, so will be flipped bool DecodeLonLat(string sText, out Vector3d v) { Vector3d vt = Vector3d.Zero; string[] parts = sText.Split(','); if (parts.Length == 2) { MAstroBody ab = MPlanetHandler.CurrentNear; double radius = 0; if (ab != null) { radius = ab.Radius.X; } if (GetNumbers(parts, out vt) == true) { //y = lon, x = lat Vector3d vr = MGISTools.LonLatMercatorToPosition(vt.Y, vt.X, radius + 1000) + ab.Position; v = vr; return(true); } } v = vt; return(false); }
//F = G*(m1*m2/d^2) public static Vector3d GetGravityVector(Vector3d AP) { double G = 6.67191e-11; if (Globals.Avatar.Target == null) { //return Vector3d.UnitY * -9.8; // AP = Globals.UserAccount.CurrentPosition; } Vector3d g = Vector3d.Zero; //Vector3d g = GetGravityAtXYZ(Globals.Avatar.GetPosition()); //bool EnableFog = false; double FogHeight = 0; MAstroBody Closest = null; double closestcandidate = double.MaxValue; //Vector3d AP = Globals.Avatar.GetPosition(); foreach (MAstroBody b in MPlanetHandler.Bodies) { //+1000 because our mesh is not perfectly spherical, so choose the lowest point b.DistanceToAvatar = Vector3d.Distance(AP, b.Position); b.AvatarDistanceToSurface = b.DistanceToAvatar - b.Radius.X; if (b.DistanceToAvatar < closestcandidate) { Closest = b; closestcandidate = b.DistanceToAvatar; } if (b.Name == "Earth") { FogHeight = Math.Pow(Math.Abs((b.AtmosphereStart) / (b.DistanceToAvatar)), 64); //FogHeight *= 0.01; //Console.WriteLine(b.DistanceToAvatar + ",FH " + FogHeight + ",AS " + b.AtmosphereStart); FogHeight = Extensions.Clamp(FogHeight, 0, 1.0); ///MPlanetHandler.CurrentNear = b; } Vector3d delta = (b.Position - AP).Normalized(); double bbmass = b.Mass; double force = G * (bbmass / (b.DistanceToAvatar * b.DistanceToAvatar)); g += delta * force; } //Console.WriteLine("G:" + g); //g = -(AP - Closest.Position).Normalized() * 9.8; MScene.Fog.FogMultiplier = (float)FogHeight; //Console.WriteLine(MScene.Fog.FogMultiplier); //we always need to calculate some useful up vector if (g.Length < 0.0001) { g = new Vector3d(0, -0.8, 0); } MPlanetHandler.CurrentNear = Closest; return(g); }
public void Setup() { AstroBodies.DataSource = MPlanetHandler.Bodies; AstroBodies.DisplayMember = "Name"; CurrentBody = MPlanetHandler.CurrentNear; if (CurrentBody == null) { CurrentBody = MPlanetHandler.Bodies.Find(x => "Earth" == x.Name); } AstroBodies.SelectedIndex = CurrentBody.ListIndex; }
public static Vector3d PositionFromMap(MAstroBody b, Vector2d p, Vector2d MapSize) { Vector3d r = Vector3d.Zero; //get percentage Vector2d per = new Vector2d(p.X / MapSize.X, p.Y / MapSize.Y); r.X = per.X; r.Y = per.Y; return(r); }
//TODO: pull the nearest blades from the Grassmats and add to mats //OR: Rolling buffer, taking from behind and adding edges of direction of travel public void PlantGrass3(MAstroBody planet, MTerrainTile tile) { Tile = tile; if (tile.material == null) { return; } if (Tile.GrassPlanter.IsComplete == false) { return; } Vector3d AP = Globals.Avatar.GetPosition(); Barycentric bary = new Barycentric(Tile.Boundary.TL, Tile.Boundary.BR, Tile.Boundary.TR, AP); Vector3d Centroid = new Vector3d((1 - bary.u) * MGrassPlanter.LOD0Size, 0, bary.v * MGrassPlanter.LOD0Size); int cx = (int)(bary.u * MGrassPlanter.LOD0Size); int cz = (int)(bary.v * MGrassPlanter.LOD0Size); float numxinterps = MGrassPlanter.LOD0Size / Tile.x_res; float numzinterps = MGrassPlanter.LOD0Size / Tile.z_res; TotalInstances = 0; for (int x = 0; x < 100; x++) { for (int z = 0; z < 100; z++) { if (TotalInstances >= Settings.MaxGrassPerTerrain) { continue; } if (cx + x > MGrassPlanter.LOD0Size) { continue; } if (cz + z > MGrassPlanter.LOD0Size) { continue; } mats[TotalInstances] = Tile.GrassPlanter.Grassmats[cx + x, cz + z]; TotalInstances++; } } Planted = true; }
void CalcPlanes() { MAstroBody planet = MPlanetHandler.CurrentNear; if (planet != null) { if (CurrentBody == planet) { return; } CurrentBody = planet; Vector3d pt = planet.GetNearestPointOnSurface(Globals.Avatar.GetPosition() + Globals.Avatar.Forward() * 10); //Forest.SetStart(GetQuantizedPoint(pt, planet.Position, 0, 0), planet); // Gnomon.SetPosition(pt); // Console.WriteLine(Vector3d.Distance(pt, Globals.Avatar.GetPosition())); } }
public void Setup(MAstroBody b) { CurrentBody = b; // TerrainShader = Helper.CreateShader(MShader.TERRAIN_SHADER); TerrainShader = new MShader(MShader.TERRAIN_SHADER); TerrainShader.Load("default_v.glsl", "Terrain\\terrain_f.glsl", "", "" ); TerrainShader.Bind(); TerrainShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE); TerrainShader.SetInt("material.specular", MShader.LOCATION_SPECULAR); TerrainShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX); TerrainShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP); TerrainShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP); base.Setup(); }
private void AstroBodies_SelectedIndexChanged(object sender, EventArgs e) { CurrentBody = (MAstroBody)AstroBodies.SelectedItem; //string sPath = string sPath = CurrentBody.TextureName; if (MassiveTools.IsURL(sPath)) { string cache = MassiveTools.GetCachePath(sPath); if (!File.Exists(cache)) { return; } bmp = Image.FromFile(cache); MapBox.BackgroundImage = bmp; } else { bmp = Image.FromFile(Path.Combine(MFileSystem.AssetsPath, CurrentBody.TextureName)); MapBox.BackgroundImage = bmp; } }
void AddPOI(string sName, string sclass, double lon, double lat, MAstroBody _body) { MPOI poi = new MPOI(); poi.sClass = sclass; poi.Position = MGISTools.LonLatMercatorToPosition(lon, lat, _body.Radius.X) + _body.Position; poi.LonLat = new Vector3d(lon, lat, 0); poi.Name = sName; if (sclass.Equals("suburb")) { Suburbs.Add(poi); } if (sclass.Equals("minor")) { PointsOfInterest.Add(poi); } if (sclass.Equals("tertiary")) { PointsOfInterest.Add(poi); } if (sclass.Equals("major")) { PointsOfInterest.Add(poi); } if (sclass.Equals("primary")) { PointsOfInterest.Add(poi); } if (sclass.Equals("highway")) { PointsOfInterest.Add(poi); } if (sclass.Equals("stream")) { PointsOfInterest.Add(poi); } }
public void Setup(MAstroBody body) { string sText = string.Format("Earth\\{0}\\biome\\{1}_{2}.png", ZoomLevel, TileX, TileY); sText = Path.Combine(Settings.TileDataPath, sText); if (!File.Exists(sText)) { return; } CurrentBody = body; if (_backgroundWorker != null) { _backgroundWorker.CancelAsync(); } ; Console.WriteLine("Memory:" + GC.GetTotalMemory(false)); MMessageBus.LoadingStatus(this, "Loading:" + TileX + "," + TileY); _backgroundWorker = new BackgroundWorker(); _backgroundWorker.DoWork += Bw_DoWork; _backgroundWorker.RunWorkerCompleted += Bw_RunWorkerCompleted; _backgroundWorker.WorkerSupportsCancellation = true; _backgroundWorker.RunWorkerAsync(body); }
public void InitializePlanets() { //GravityIndicator = Helper.CreateCube(MScene.AstroRoot, "GravitySphere", Vector3d.Zero); //GravityIndicator.transform.Scale = new Vector3d(0.05, 0.1, 0.1); //GravityIndicator.OwnerID = "MasterAstronomer"; //MScene.AstroRoot.Add(GravityIndicator); //GravityIndicator.SetMaterial((MMaterial)MScene.MaterialRoot.FindModuleByName(MMaterial.DEFAULT_MATERIAL)); CreateShaders(); for (int i = 0; i < Bodies.Count; i++) { MAstroBody m = Bodies[i]; //m.Radius = m.Radius * 0.5; if (m.IsTemplate == true) { continue; } m.ListIndex = i; Vector3d pos = m.Position + new Vector3d(-m.Radius.X, 0, 0) * (m.HasRings == true ? 3.0 : 1.1); MServerZone zone = new MServerZone("MASTER_ASTRONOMER", m.Name, "Astronomical", MassiveTools.ToVector3_Server(pos)); zone.Rotation = MassiveTools.ArrayFromQuaterniond(Quaterniond.Identity); zone.Description = m.Description + " \nRadius:" + m.Radius; MMessageBus.AddZone(this, zone); //Extensions.LookAt(m.Position + m.Radius * 2, m.Position), m.Name)); MSceneObject mo; //planet files contain uvs for mercator if (m.HasAtmosphere) { CurrentNear = m; mo = Helper.CreateModel(MScene.AstroRoot, m.Name, @"Models\planets\earth.3ds", Vector3d.Zero); //mo = Helper.CreateSphere(MScene.AstroRoot, 3, "Planet"); //mo.transform.Scale = m.Radius * 1.00055; mo.transform.Scale = m.Radius; } else { mo = Helper.CreateModel(MScene.AstroRoot, m.Name, @"Models\planets\planet_sphere2.3ds", Vector3d.Zero); mo.transform.Scale = m.Radius; } if (m.HasRings) { MModel ring = Helper.CreateModel(MScene.Priority2, m.Name + "_rings", @"Models\planets\planet_rings.3ds", Vector3d.Zero); ring.transform.Position = m.Position; ring.transform.Rotation = Quaterniond.FromEulerAngles(0, 0, 5 * Math.PI / 180); ring.transform.Scale = m.Radius; ring.InstanceID = m.Name; ring.TemplateID = m.Name; ring.DistanceThreshold = m.Radius.X * 12; ring.OwnerID = "MasterAstronomer"; MMaterial ringmat = new MMaterial(m.Name + "_mat"); ringmat.AddShader((MShader)MScene.MaterialRoot.FindModuleByName(MShader.DEFAULT_SHADER)); MTexture ringtex = Globals.TexturePool.GetTexture(@"Textures\Planets\saturn_rings.png"); ringmat.SetDiffuseTexture(ringtex); ring.SetMaterial(ringmat); MPhysicsObject ringpo = new MPhysicsObject(ring, "Physics", 0, MPhysicsObject.EShape.ConcaveMesh, false, m.Radius); ringpo.SetLinearFactor(0, 0, 0); ringpo.SetRestitution(0.5); } mo.transform.Position = m.Position; //mo.transform.Scale = m.Radius * 1.9999; mo.InstanceID = m.Name; mo.TemplateID = m.Name; mo.OwnerID = "MasterAstronomer"; mo.DistanceThreshold = m.Radius.X * 110002; //distance at which it becomes visible //MModel mod = (MModel)mo.FindModuleByType(EType.Model); //mod.DistanceThreshold = mo.DistanceThreshold; //MMesh met = (MMesh)mod.FindModuleByType(EType.Mesh); //if ( met != null ) { //met.DistanceThreshold = mo.DistanceThreshold; //} mo.Tag = m; m.Tag = mo; //now that we have a 3d model, apply some progressive textures (will auto-switch with smooth transition in shader) MMaterial mat = new MMaterial(m.Name + "_mat"); mat.AddShader((MShader)MScene.MaterialRoot.FindModuleByName(MShader.DEFAULT_SHADER)); MTexture tex = Globals.TexturePool.GetTexture(m.TextureName); mat.SetDiffuseTexture(tex); mo.SetMaterial(mat); MTexture tex2 = Globals.TexturePool.GetTexture("Textures\\terrain\\sand01b.jpg"); mat.SetMultiTexture(tex2); MTexture tex3 = Globals.TexturePool.GetTexture("Textures\\terrain\\water.jpg"); mat.SetNormalMap(tex3); double dia = 2.0 * Math.PI * m.Radius.X * 0.0000001; //double dia = 1; mat.Tex2CoordScale = new Vector2((float)dia, (float)dia); mo.SetMaterial(mat); MScene.MaterialRoot.Add(mat); if (m.HasAtmosphere) { if (Settings.DrawTerrains == true) { m.AddDynamicTerrain(); //adds tile based terrain (from e.g. tile service) } //MPhysicsObject po = new MPhysicsObject(mo, "Physics", 0, MPhysicsObject.EShape.ConcaveMesh, //false, m.Radius); MPhysicsObject po = new MPhysicsObject(mo, "Physics", 0, MPhysicsObject.EShape.Sphere, false, m.Radius); //po.SetLinearFactor(0, 0, 0); //po.SetRestitution(0.5); //MSphere moc = Helper.CreateSphere(MScene.AstroRoot, 3, m.Name+ "Clouds", Vector3d.Zero); MModel moc = Helper.CreateModel(MScene.AstroRoot, m.Name + "_clouds", @"Models\planets\clouds.3ds", Vector3d.Zero); moc.CastsShadow = false; moc.transform.Position = m.Position; moc.transform.Scale = m.Radius; moc.DistanceThreshold = m.Radius.X * 3; //moc.transform.Scale = m.Radius*2.1; moc.InstanceID = m.Name; moc.TemplateID = m.Name; moc.OwnerID = "MasterAstronomer"; moc.Tag = m; MMaterial cmat = new MMaterial("CloudMat"); cmat.AddShader((MShader)MScene.MaterialRoot.FindModuleByName(MShader.DEFAULT_SHADER)); cmat.Opacity = 1; cmat.IsSky = 1; // = new MTexture("CloudTex"); MTexture ctex = Globals.TexturePool.GetTexture(CloudTexURL); ctex.Additive = false; cmat.SetDiffuseTexture(ctex); moc.SetMaterial(cmat); MScene.MaterialRoot.Add(cmat); MModel sky = Helper.CreateModel(MScene.AstroRoot, m.Name + "_sky", @"Models\sky.3ds", Vector3d.Zero); sky.CastsShadow = false; sky.transform.Position = m.Position; sky.transform.Scale = m.Radius; //moc.transform.Scale = m.Radius*2.1; sky.InstanceID = m.Name; sky.TemplateID = m.Name; sky.OwnerID = "MasterAstronomer"; sky.Tag = m; sky.SetMaterial(cmat); sky.DistanceThreshold = m.Radius.X * 4; MObjectAnimation ani = new MObjectAnimation(); ani.AngleOffset = Quaterniond.FromEulerAngles(0, 0.002, 0); ani.Speed = 1; sky.Add(ani); sky.SetMaterial(MSkyMaterial); /*MMaterial csky = new MMaterial("Skymat"); * csky.AddShader((MShader)MScene.MaterialRoot.FindModuleByName(MShader.DEFAULT_SHADER)); * csky.Opacity = 0.5; * csky.IsSky = 1; * // = new MTexture("CloudTex"); * MTexture cSkyTex = Globals.TexturePool.GetTexture(OpaqueCloudTexURL); * cSkyTex.Additive = false; * csky.SetDiffuseTexture(cSkyTex); * sky.SetMaterial(csky); * MScene.MaterialRoot.Add(csky); */ /* MSphere water = Helper.CreateSphere(MScene.ModelRoot, 5, "Water"); * water.transform.Position = m.Position; * water.transform.Scale = m.Radius * 2.00; * MMaterial waterman = new MMaterial("Watermat"); * MShader shader = new MShader("watershader"); * shader.Load("ocean_vs.glsl", "ocean_fs.glsl"); * shader.Bind(); * shader.SetInt("diffuseTexture", 0); * shader.SetInt("shadowMap", 1); * waterman.AddShader(shader); * water.SetMaterial(waterman); */ //water.SetMaterial((MMaterial)MScene.MaterialRoot.FindModuleByName(MMaterial.DEFAULT_MATERIAL)); } else { MPhysicsObject po = new MPhysicsObject(mo, "Physics", 0, MPhysicsObject.EShape.Sphere, false, m.Radius * 0.999); po.SetLinearFactor(0, 0, 0); po.SetRestitution(0.5); } m.Setup(); //Console.WriteLine("Created:" + mo.Name + ":" + (mo.transform.Position) + " R:" + m.Radius); } }
public void PlantTrees(MAstroBody planet, MTerrainTile tile) { //DistanceThreshold = tile.DistanceThreshold; Tile = tile; if (tile.material == null) { return; } MTexture tex = Tile.Biome; if (tex == null) { return; } this.transform.Position = tile.transform.Position; Random ran = new Random(1234); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); int i = 0; for (int z = 0; z < tile.z_res - 1; z++) { for (int x = 0; x < tile.x_res - 1; x++) { float[] c = tex.GetPixel(x, z); float r = c[0]; float g = c[1]; float b = c[2]; float a = c[3]; float t = 0; //if ((b > r) && (b > g)) t = g; if ((r < 0.05) && (g > 0.1) && (b < 0.05)) { t = g; } else { continue; } if (ran.NextDouble() > Settings.TreeDensity) { continue; } //if (g < 0.7) continue; //Console.WriteLine(c[0] + " " + c[1] + " " + c[2] + " " + c[3]); if (i >= Settings.MaxTreesPerTerrain) { break; } Vector3d Treepos = new Vector3d(x, 0, z); //Vector3d PlantingPos = planet.GetNearestPointOnSphere(Treepos, 0); Matrix4d TreeScale = Matrix4d.Scale(1 + ran.NextDouble(), 1 + ran.NextDouble() * 2, 1 + ran.NextDouble()); Vector3d PlantingPos = Tile.GetPointOnSurfaceFromGrid(Treepos); //; + new Vector3d(r.NextDouble()*5, r.NextDouble() * 5, r.NextDouble()*5); Matrix4d TreePosition = Matrix4d.CreateTranslation(PlantingPos); //find point at y with raycast Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); mats[i] = final; i++; } } TotalInstances = i; for (int j = TotalInstances; j < Settings.MaxTreesPerTerrain; j++) { Matrix4 final = Matrix4.CreateTranslation(j, 0, 0); mats[j] = final; } //Setup(); //UploadBuffer(); Planted = false; }
public void LoadMetaData(MAstroBody _body) { string path = string.Format(@"earth\{0}\data\{1}_{2}", ZoomLevel, TileX, TileY) + ".json"; path = Path.Combine(Settings.TileDataPath, path); if (File.Exists(path)) { string sJSON = File.ReadAllText(path); try { //todo query against results directly, using line-proximity FeatureCollection results = JsonConvert.DeserializeObject <FeatureCollection>(sJSON); //FeatureCollection results = (FeatureCollection) if (results != null) { var features = results.Features.Select(x => x) .Where(x => x.Properties.Keys.Contains("class") && (x.Properties.Values.Contains("suburb")) || (x.Properties.Values.Contains("city")) || (x.Properties.Values.Contains("primary")) || (x.Properties.Values.Contains("minor")) ); foreach (Feature f in features) { if (!f.Properties.ContainsKey("name")) { continue; } if (f.Properties["class"].Equals("city")) { ClosestCity = f.Properties["name"].ToString(); continue; } if (f.Geometry.Type == GeoJSON.Net.GeoJSONObjectType.Point) { Point pt = (Point)f.Geometry; AddPOI(f.Properties["name"].ToString(), f.Properties["class"].ToString(), pt.Coordinates.Longitude, pt.Coordinates.Latitude, _body); } if (f.Geometry.Type == GeoJSON.Net.GeoJSONObjectType.LineString) { LineString ls = (LineString)f.Geometry; foreach (Position p in ls.Coordinates) { AddPOI(f.Properties["name"].ToString(), f.Properties["class"].ToString(), p.Longitude, p.Latitude, _body); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public void PlantGrass(MAstroBody planet, MTerrainTile tile) { // if (Planted == true) return; //DistanceThreshold = tile.DistanceThreshold; Tile = tile; if (tile.material == null) { return; } MTexture tex = Tile.Biome; if (tex == null) { return; } this.transform.Position = tile.transform.Position; //Random ran = new Random(1234); Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation()); //int i = 0; TotalInstances = 0; Vector3d AP = Globals.Avatar.GetPosition(); // Console.WriteLine(AP); Barycentric bary = new Barycentric(Tile.Boundary.TL, Tile.Boundary.BR, Tile.Boundary.TR, AP); PlantPatch(bary, 64 * 64, 2, 0.8); //PlantPatch(bary, 32 * 32, 8, 0.3); // Console.WriteLine(bary.ToString()); /* * double sq = 64 * 64; * double sq2 = Math.Sqrt(sq); * * Matrix4d TreePosition; * Matrix4 final; * Matrix4d TreeScale; * Vector3d PlantingPos = new Vector3d(); * for (int x = (int)-sq2; x < sq2; x++) * for (int z = (int)-sq2; z < sq2; z++) * { * PlantingPos.X = (int)(Tile.x_res * (1 - bary.u)) - x * 0.05; * PlantingPos.Y = 0; * PlantingPos.Z = (int)(Tile.z_res * (bary.v)) - z * 0.05; * * double yv = * (MPerlin.Noise(PlantingPos.X, PlantingPos.Z) * 0.4) + (MPerlin.Noise(PlantingPos.X * 1.1, PlantingPos.Z * 1.1) * 0.4) + ; + if (yv < 0.1) continue; + + TreeScale = Matrix4d.Scale(0.03+0.04 * yv, 0.01 + yv * 0.14 , 0.03+0.04* yv); + + PlantingPos.X += MPerlin.Noise(PlantingPos.X * 4.0, PlantingPos.Z * 4.1) * 1.1; + PlantingPos.Z += MPerlin.Noise(PlantingPos.X * 3.8, PlantingPos.Z * 4.2) * 1.1; + PlantingPos = Tile.GetInterpolatedPointOnSurfaceFromGrid2(PlantingPos); + + TreePosition = Matrix4d.CreateTranslation(PlantingPos); + final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition); + if (i < Settings.MaxGrassPerTerrain) + { + mats[i] = final; + i++; + } + } + + TotalInstances = i; */ for (int j = TotalInstances; j < Settings.MaxGrassPerTerrain; j++) { Matrix4 final = Matrix4.CreateTranslation(9999999999, 0, 0); mats[j] = final; } //Setup(); //UploadBuffer(); Planted = true; }