private void Update() { HoloToolkit.Unity.SpatialMapping.OrientedBoundingBox obb = OBBMeshIntersection.CreateWorldSpaceOBB(GetComponent <BoxCollider>()); List <int> intersecting = OBBMeshIntersection.FindTriangles(obb, m_triangleMesh.vertices, m_triangleMesh.GetTriangles(0), m_triangleXform); GetComponent <Renderer>().material.color = intersecting.Count > 0 ? Color.red : Color.green; }
private bool CreateMarginVolumeObject(out GameObject marginVolume, out Vector3 centerPointOnFrontPlane, out Vector3 centerPointOnBackPlane, GameObject embedded, HoloToolkit.Unity.SpatialMapping.SurfacePlane plane) { BoxCollider embeddedOBB = embedded.GetComponent <BoxCollider>(); if (null == embeddedOBB) { Debug.Log("ERROR: Embedded object " + embedded.name + " lacks a box collider"); marginVolume = null; centerPointOnFrontPlane = Vector3.zero; centerPointOnBackPlane = Vector3.zero; return(true); } HoloToolkit.Unity.SpatialMapping.OrientedBoundingBox surfaceBounds = plane.Plane.Bounds; /* * Compute margin volume front (i.e., the actual surface) and back (onto * which spatial mesh triangles will be projected) planes based on embedded * object thickness. * * Note that the embedded object position is not in the center of the * object but rather at a point flush with the surface it will be embedded * in. */ Vector3 intoSurfaceNormal = -Vector3.Normalize(embeddedOBB.transform.forward); float embeddedThickness = embeddedOBB.size.z * embeddedOBB.transform.lossyScale.z + extraDisplacement; Vector3 embeddedPosOnPlane = plane.transform.InverseTransformPoint(embedded.transform.position); embeddedPosOnPlane.x = 0; embeddedPosOnPlane.y = 0; centerPointOnFrontPlane = plane.transform.TransformPoint(embeddedPosOnPlane); centerPointOnBackPlane = centerPointOnFrontPlane + intoSurfaceNormal * embeddedThickness; Vector3 centerPoint = 0.5f * (centerPointOnFrontPlane + centerPointOnBackPlane); //Debug.Log("*** PlanePos: " + plane.transform.position + ", PlaneFwd: " + plane.transform.forward + ", Embed Point: " + embedded.transform.position.ToString("F2") + ", IntoNorm: " + intoSurfaceNormal.ToString("F2") + ", thick: " + embeddedThickness + ", embeddedPosOnPlane: " + embeddedPosOnPlane.ToString("F2") + ", centerFront: " + centerPointOnFrontPlane.ToString("F2") + ", centerBack: " + centerPointOnBackPlane.ToString("F2")); /* * Create a game object to describe the size, position, and orientation of * the margin volume we want to create. * * The box collider is something of a formality. The surface mesh * deformation procedure and OBB/mesh intersection tests use box colliders * to describe OBBs. Here, the box collider is made the same size as the * game object to which it is parented. The downstream intersection testing * code knows to properly scale the box based on its parent. */ marginVolume = new GameObject("deformation-" + embedded.name); marginVolume.transform.position = centerPoint; marginVolume.transform.localScale = new Vector3(2 * surfaceBounds.Extents.x, 2 * surfaceBounds.Extents.y, embeddedThickness); marginVolume.transform.rotation = surfaceBounds.Rotation; // need to use surface plane rotation because x, y differ from obj's BoxCollider obb = marginVolume.AddComponent <BoxCollider>(); obb.center = Vector3.zero; obb.size = Vector3.one; obb.enabled = false; // we do not actually want collision detection return(false); }
/// <summary> /// Builds the bounded plane to match the obb defined by xform /// </summary> public BoundedPlane(Transform xform) { Plane = new Plane(xform.forward, xform.position); Bounds = new OrientedBoundingBox() { Center = xform.position, Extents = xform.localScale / 2, Rotation = xform.rotation }; Area = Bounds.Extents.x * Bounds.Extents.y; }