public static TriangleHit?Raycast(Ray ray, int level, BoundsMap bounds, Func <Cell, float> heightAt, Transform gridTransform = null) { if (gridTransform != null) { ray = new Ray(gridTransform.InverseTransformPoint(ray.origin), gridTransform.InverseTransformDirection(ray.direction)); } var closest = (ray.origin - Vector3.Dot(ray.origin, ray.direction) * ray.direction).magnitude; var candidates = new List <Triangle>(Triangle.AtLevel(0)); for (int i = 0; i < level; i++) { var triangles = candidates; candidates = new List <Triangle>(); foreach (var triangle in triangles) { var max = bounds.GetMax(triangle, i); if (max < closest) { continue; } if (intersectsCell(ray, triangle, i, bounds.GetMin(triangle, i), max)) { candidates.AddRange(triangle.GetChildren(i + 1)); } } } return(candidates.Select(t => t.Raycast(ray, level, heightAt)).Where(h => h.HasValue).WithMin(t => t.Value.Distance)); }
public static Cell?Raycast(Ray ray, int level, BoundsMap bounds, Func <Cell, float> heightAt, Transform gridTransform = null) { var hit = Triangle.Raycast(ray, level, bounds, heightAt, gridTransform); if (!hit.HasValue) { return(null); } var barycentric = hit.Value.BarycentricCoordinate; var triangle = hit.Value.Triangle; if ((barycentric.x >= barycentric.y) && (barycentric.x >= barycentric.z)) { return(triangle.GetVertices(level).ElementAt(0)); } else if ((barycentric.y >= barycentric.x) && (barycentric.y >= barycentric.z)) { return(triangle.GetVertices(level).ElementAt(1)); } else { return(triangle.GetVertices(level).ElementAt(2)); } }
public void Start() { GameObject.DontDestroyOnLoad(this); Instance = this; heightRatio = getHeightRatioMap(PD.body); BM = new BoundsMap(heightRatio, PD.gridLevel); kerbGO = new GameObject(); kerbGO.name = "kerbGO"; kerbGO.transform.parent = PD.body.MapObject.transform; kerbGO.transform.localScale = Vector3.one * 1000; kerbGO.transform.localPosition = Vector3.zero; kerbGO.transform.localRotation = Quaternion.identity; or = gameObject.AddComponent <OverlayRenderer>(); or.SetGridLevel(PD.gridLevel); foreach (PlanetData pd in WeatherDatabase.PlanetaryData) { if (!bodyRadii.ContainsKey(pd.body)) { bodyRadii.Add(pd.body, bodyradius); } else { bodyRadii[pd.body] = bodyradius; } } }
public DebugGUI(PlanetSimulator pSim) { this.pSim = pSim; mainWindowID = Guid.NewGuid().GetHashCode(); basicWindowID = Guid.NewGuid().GetHashCode(); bMap = new BoundsMap(x => 1, pSim.level); }
public void Update() { if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION) { Destroy(gameObject); return; } if (!MapView.MapIsEnabled || !ShowOverlay || MapView.MapCamera == null || KethaneData.Current == null) { overlayRenderer.IsVisible = false; return; } overlayRenderer.IsVisible = true; var target = MapView.MapCamera.target; var newBody = getTargetBody(target); var bodyChanged = (newBody != null) && (newBody != body); if (bodyChanged) { body = newBody; heightAt = getHeightRatioMap(); bounds = new BoundsMap(heightAt, KethaneData.GridLevel); overlayRenderer.SetHeightMap(heightAt); var radius = bodyRadii.ContainsKey(body) ? bodyRadii[body] : 1.025; var parent = ScaledSpace.Instance.scaledSpaceTransforms.FirstOrDefault(t => t.name == body.name); overlayRenderer.SetRadiusMultiplier((float)radius); overlayRenderer.SetTarget(parent); } if (bodyChanged || resource == null || resource.Resource != SelectedResource) { resource = KethaneController.ResourceDefinitions.Where(r => r.Resource == SelectedResource).Single(); refreshCellColors(); } var ray = MapView.MapCamera.camera.ScreenPointToRay(Input.mousePosition); hoverCell = Cell.Raycast(ray, KethaneData.GridLevel, bounds, heightAt, gameObject.transform); }
public void Update() { if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION) { or.IsVisible = false; return; } if (!MapView.MapIsEnabled || !showOverlay || MapView.MapCamera == null) { or.IsVisible = false; return; } or.IsVisible = true; var target = MapView.MapCamera.target; var newBody = getTargetBody(target); var bodyChanged = (newBody != null) && (newBody != body); if (bodyChanged) { body = newBody; heightRatio = getHeightRatioMap(body); bounds = new BoundsMap(heightRatio, PD.gridLevel); or.SetHeightMap(heightRatio); var radius = bodyRadii.ContainsKey(body) ? bodyRadii[body] : 1.025; or.SetRadiusMultiplier((float)radius); or.SetTarget(body.MapObject.transform); } if (bodyChanged) { refreshCellColours(); } Ray ray = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition); hoverCell = Cell.Raycast(ray, PD.gridLevel, BM, heightRatio, kerbGO.transform); if (hoverCell.HasValue) { //or.SetCellColor(hoverCell.Value, getCellColor(0, hoverCell.Value)); } }
private void refreshCollider() { bounds = new BoundsMap(heightAt, KethaneData.GridLevel); }