public void UpdateHover(Model renderModel, uint modelPolygonId) { HoverPolygon = null; if(renderModel != null) { if(HoverModel != renderModel) { HoverModel = renderModel; HoverGroup.Clear(); HoverGroup.Add(renderModel); } //if(Configuration.hoverDebug) { PolygonIds.Clear(); PolygonIds.Add(modelPolygonId); // Temp debug code IMeshSource meshSource = HoverModel.Batch.MeshSource; if (meshSource is GeometryMesh original) { if (original.Geometry.PolygonAttributes.Contains<Vector3>("polygon_normals")) { if (modelPolygonId < original.Geometry.Polygons.Count) { HoverPolygon = original.Geometry.Polygons[(int)modelPolygonId]; #if true //selectionMesh.Geometry = Geometry.Clone(original.Geometry, SelectedPolygonIds).Destination; //selectionMesh.BuildMeshFromGeometry(); Frame hoverFrame = HoverModel.Frame; Polygon polygon = HoverPolygon; var polygonNormals = original.Geometry.PolygonAttributes.Find<Vector3>("polygon_normals"); var polygonCentroids = original.Geometry.PolygonAttributes.Find<Vector3>("polygon_centroids"); Vector3 normalInModel = polygonNormals[polygon]; Vector3 positionInModel = hoverFrame.LocalToWorld.InverseMatrix.TransformPoint(HoverPosition); var pointLocations = original.Geometry.PointAttributes.Find<Vector3>("point_locations"); Corner pivotCorner = original.Geometry.ClosestPolygonCorner(polygon, positionInModel); Point pivotPoint = pivotCorner.Point; HoverPoint = pivotPoint; Edge firstEdge; Edge secondEdge; original.Geometry.PolygonCornerEdges(polygon, pivotCorner, out firstEdge, out secondEdge); Point firstEdgeOutPoint = firstEdge.Other(pivotPoint); Point secondEdgeOutPoint = secondEdge.Other(pivotPoint); Vector3 pivotLocation = hoverFrame.LocalToWorld.Matrix.TransformPoint(pointLocations[pivotPoint]); Vector3 firstOutLocation = hoverFrame.LocalToWorld.Matrix.TransformPoint(pointLocations[firstEdgeOutPoint]); Vector3 secondOutLocation = hoverFrame.LocalToWorld.Matrix.TransformPoint(pointLocations[secondEdgeOutPoint]); Vector3 normal = hoverFrame.LocalToWorld.Matrix.TransformDirection(normalInModel); Vector3 firstDirection = Vector3.Normalize(firstOutLocation - pivotLocation); Vector3 secondDirection = Vector3.Normalize(secondOutLocation - pivotLocation); Vector3 centroid = hoverFrame.LocalToWorld.Matrix.TransformPoint(polygonCentroids[polygon]); if (Configuration.hoverDebug) { lineRenderer.Begin(); lineRenderer.Line(HoverPosition, HoverPosition + 0.5f * normal, new Vector4(1.0f, 1.0f, 1.0f, 1.0f)); lineRenderer.Line(centroid, centroid + 0.25f * normal, new Vector4(0.5f, 0.5f, 0.5f, 1.0f)); /*LineRenderer.Line(pivotLocation, pivotLocation + normal); LineRenderer.Line(pivotLocation - firstDirection * 1.0f, pivotLocation + firstDirection * 1.0f); LineRenderer.Line(pivotLocation - secondDirection * 1.0f, pivotLocation + secondDirection * 1.0f);*/ lineRenderer.End(); } } #endif } } } } else { if(HoverModel != null) { HoverGroup.Clear(); } HoverModel = null; } }
public void ProcessIdBuffer(IDRenderer.HoverInfo hover, int px, int py) { ResetHover(); uint compareId = 0xfffffeu; if(hover.Id == compareId) { HoverModel = null; HoverGroup.Clear(); return; } try { Vector3 positionInWorld = sceneManager.Camera.WorldToClip.InverseMatrix.UnProject( (float)px, (float)py, hover.Depth, renderer.Requested.Viewport.X, renderer.Requested.Viewport.Y, renderer.Requested.Viewport.Width, renderer.Requested.Viewport.Height ); HoverPosition = positionInWorld; if(userInterfaceManager.AutoFocus) { Camera camera = sceneManager.Camera; StereoParameters stereo = camera.Projection.StereoParameters; float distance = camera.Frame.LocalToWorld.Matrix.GetColumn(3).Xyz.Distance(positionInWorld); stereo.ViewportCenter.Z = distance; } HoverDepth = hover.Depth; uint modelPolygonId = 0; uint manipulatorPolygonId = 0; IDListEntry compareKey = new IDListEntry(hover.Id); Model renderModel = null; if(manipulatorManager != null) { manipulatorManager.ManipulatorModel = manipulatorManager.ManipulatorGroup.IdTest(compareKey, out manipulatorPolygonId); if(manipulatorManager.ManipulatorModel == null) { foreach(var group in hover.Groups) { renderModel = group.IdTest(compareKey, out modelPolygonId); if(renderModel != null) { break; } } } } UpdateHover(renderModel, modelPolygonId); } catch(System.Exception) { return; } }