void DrawMesh(bool snapToGrid = false) { if (m_MeshCopy != null && m_LineCopy != null) { if (m_MeshCreatorController.m_Vertices.Count >= 3 && m_PointerController.isCentered) { // current position should be in the same plane as the first 3 vertices m_PointerController.SetPointerPlane( m_MeshCreatorController.GetVertexWithWorldCoord(0), m_MeshCreatorController.GetVertexWithWorldCoord(1), m_MeshCreatorController.GetVertexWithWorldCoord(2) ); m_PointerController.isCentered = false; } if (!snapToGrid) { currentPosition = m_Pointer.transform.position; } else if (!m_PointerController.isCentered) // snapping to the grid and not one of the first 3 points { // TODO: needs to be some scale here so as not to round to whole numbers (divided by 2) float scale = m_ScaleDivision; currentPosition = m_PointerController.GetGridPointFromPlane(m_Pointer.transform.position, scale); } else if (m_PointerController.isCentered && m_MeshCreatorController.m_Vertices.Count == 2) { // TODO: needs to be some scale here so as not to round to whole numbers (divided by 2) float scale = m_ScaleDivision; m_PointerController.SetPointerPlane( m_MeshCreatorController.GetVertexWithWorldCoord(0), m_MeshCreatorController.GetVertexWithWorldCoord(1), currentPosition ); m_PointerController.isCentered = false; currentPosition = m_PointerController.GetGridPointFromPlane(m_Pointer.transform.position, scale); } if (!m_PointerController.isCentered) { // make sure mesh is convex // by ensuring that the pointer is within the ray created by the 2nd to last vertex // and the last vertex and the ray created by the last vertex and the first vertex int count = m_MeshCreatorController.m_Vertices.Count; Vector3 vertexN = m_MeshCreatorController.GetVertexWithWorldCoord(count - 1); Vector3 vertexNMinus1 = m_MeshCreatorController.GetVertexWithWorldCoord(count - 2); Vector3 vertex1 = m_MeshCreatorController.GetVertexWithWorldCoord(0); Vector3 A = vertexN - vertexNMinus1; Vector3 B = vertex1 - vertexN; m_PointerController.RestrictPointerToInsideRays(vertexN, A, B); } m_LineCopy.transform.position = (initialPosition + currentPosition) / 2; m_LineCopy.transform.localScale = new Vector3( m_LineCopy.transform.localScale.x, (initialPosition - currentPosition).magnitude / 2, m_LineCopy.transform.localScale.z ); if (snapToGrid && m_PointerController.isCentered) { float sd = m_ScaleDivision * 2; // round to nearest (sub)unit Vector3 scaleL = m_LineCopy.transform.localScale; scaleL.y *= sd; if (scaleL.y < 1) { scaleL.y = 1; } else { scaleL.y = Mathf.Round(scaleL.y); } scaleL.y /= sd; float deltaY = scaleL.y - m_LineCopy.transform.localScale.y; m_LineCopy.transform.localScale = scaleL; // move position to adjust for change of size Vector3 posL = m_LineCopy.transform.position; posL += m_LineCopy.transform.up * deltaY; m_LineCopy.transform.position = posL; currentPosition = m_LineCopy.transform.position + m_LineCopy.transform.up * m_LineCopy.transform.localScale.y; } if (currentPosition - initialPosition == Vector3.zero) { return; } m_LineCopy.transform.localRotation = Quaternion.LookRotation((currentPosition - initialPosition).normalized); m_LineCopy.transform.Rotate(90, 0, 0); } }