private void ConvertGeometry(Matrix4x4 iTopMatrixWorld, Transform iTransform, Mesh iMesh, vrPhysicsGeometry ioGeometry, uint iGeometryIndex) { var vertices = iMesh.vertices; var triangles = iMesh.triangles; MiddleVRTools.Log(3, "[ ] PhysicsBody: Number of vertices in sub-geometry '" + iGeometryIndex + "': " + vertices.Length); MiddleVRTools.Log(3, "[ ] PhysicsBody: Number of triangles in sub-geometry '" + iGeometryIndex + "': " + triangles.Length); var m = iTopMatrixWorld.inverse * iTransform.localToWorldMatrix; // We will reuse the same vector to avoid many memory allocations. vrVec3 vPos = new vrVec3(); MiddleVRTools.Log(6, "[>] PhysicsBody: Adding vertices."); foreach (Vector3 vertex in vertices) { var vertexPos = m.MultiplyPoint3x4(vertex); MiddleVRTools.FromUnity(vertexPos, ref vPos); ioGeometry.AddVertex(vPos, iGeometryIndex); MiddleVRTools.Log(6, "[ ] PhysicsBody: Adding a vertex at position (" + vPos.x() + ", " + vPos.y() + ", " + vPos.z() + ") to sub-geometry '" + iGeometryIndex + "'."); } MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding vertices."); MiddleVRTools.Log(6, "[>] PhysicsBody: Adding triangles."); for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3) { var index0 = (uint)triangles[i + 0]; var index1 = (uint)triangles[i + 1]; var index2 = (uint)triangles[i + 2]; ioGeometry.AddTriangle(index0, index1, index2, iGeometryIndex); MiddleVRTools.Log(6, "[ ] PhysicsBody: Adding a triangle with vertex indexes (" + index0 + ", " + index1 + ", " + index2 + ") to sub-geometry '" + iGeometryIndex + "'."); } MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding triangles."); }
// On clients, handle the command call private vrValue _CommandHandler(vrValue iValue) { if (m_ClusterMgr.IsClient()) { // extract position and orientation from the vrValue vrVec3 pos = iValue[0].GetVec3(); vrQuat orient = iValue[1].GetQuat(); Vector3 p = new Vector3(pos.x(), pos.y(), pos.z()); Quaternion q = new Quaternion(orient.x(), orient.y(), orient.z(), orient.w()); transform.position = p; transform.rotation = q; } return(null); }
private void ConvertGeometry(Mesh mesh) { Vector3[] vertices = mesh.vertices; int[] triangles = mesh.triangles; MiddleVRTools.Log(3, "[ ] PhysicsBody: Number of vertices: " + vertices.Length); MiddleVRTools.Log(3, "[ ] PhysicsBody: Number of Triangles: " + triangles.Length); var noScaleWorldMat = new vrMatrix(); noScaleWorldMat.MakeTranslationRotation( new vrVec3(transform.position.x, transform.position.y, transform.position.z), new vrQuat(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w)); var unityNoScaleWorldMat = MiddleVRTools.RawToUnity(noScaleWorldMat); var scalingMatrix = unityNoScaleWorldMat.inverse * transform.localToWorldMatrix; // We will reuse the same vector to avoid many memory allocations. vrVec3 vPos = new vrVec3(); MiddleVRTools.Log(6, "[>] PhysicsBody: Adding vertices."); foreach (Vector3 vertex in vertices) { var vertexPos = scalingMatrix.MultiplyPoint3x4(vertex); MiddleVRTools.FromUnity(vertexPos, ref vPos); m_Geometry.AddVertex(vPos); MiddleVRTools.Log(6, "[ ] PhysicsBody: Adding a vertex at position (" + vPos.x() + ", " + vPos.y() + ", " + vPos.z() + ")."); } MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding vertices."); MiddleVRTools.Log(6, "[>] PhysicsBody: Adding triangles."); for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3) { var index0 = (uint)triangles[i]; var index1 = (uint)triangles[i + 1]; var index2 = (uint)triangles[i + 2]; m_Geometry.AddTriangle(index0, index1, index2); MiddleVRTools.Log(6, "[ ] PhysicsBody: Adding a triangle with vertex indexes (" + index0 + ", " + index1 + ", " + index2 + ")."); } MiddleVRTools.Log(6, "[<] PhysicsBody: End of adding triangles."); }
protected void Update() { var deviceMgr = MiddleVR.VRDeviceMgr; if (deviceMgr != null) { var headTracker = deviceMgr.GetTracker("zSpace.Head.Tracker"); var stylusTracker = deviceMgr.GetTracker("zSpace.Stylus.Tracker"); if (headTracker == null || stylusTracker == null) { MiddleVRTools.Log(0, "[X] No head or stylus tracker found for zSpace. Did you load the driver?"); enabled = false; return; } bool headTargetIsVisible = headTracker.IsTracked(); if (headTargetWasVisible != headTargetIsVisible) { MiddleVRTools.Log(2, "[+] Head is " + (headTargetIsVisible ? "visible" : "invisible") + " by the zSpace device."); headTargetWasVisible = headTargetIsVisible; } bool stylusTargetIsVisible = stylusTracker.IsTracked(); if (stylusTargetWasVisible != stylusTargetIsVisible) { MiddleVRTools.Log(2, "[+] Stylus is " + (stylusTargetIsVisible ? "visible" : "invisible") + " by the zSpace device."); stylusTargetWasVisible = stylusTargetIsVisible; } } var kernel = MiddleVR.VRKernel; if (kernel != null && deviceMgr != null) { if (deviceMgr.IsKeyToggled(MiddleVR.VRK_C)) { Color c = m_UsingLEDColor0 ? m_LEDColor0 : m_LEDColor1; vrVec3 proposedColor = new vrVec3(c.r, c.g, c.b); MiddleVRTools.Log(2, "[+] Proposed stylus LED color sent to zSpace: (" + proposedColor.x() + ", " + proposedColor.y() + ", " + proposedColor.z() + ")."); kernel.ExecuteCommand( "zSpace.SetStylusLEDColor", proposedColor); m_UsingLEDColor0 = !m_UsingLEDColor0; vrVec3 usedColorAsRGB = GetStylusLEDColor(); MiddleVRTools.Log(2, "[+] Current zSpace stylus LED color is: (" + usedColorAsRGB.x() + ", " + usedColorAsRGB.y() + ", " + usedColorAsRGB.z() + ")."); } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_L)) { bool turnOnLight = true; if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) || deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT)) { turnOnLight = false; } MiddleVRTools.Log(2, "[+] Trying to turn " + (turnOnLight ? "on" : "off") + " the zSpace stylus LED."); kernel.ExecuteCommand( "zSpace.SetStylusLEDAsTurnedOn", turnOnLight); vrValue isLEDTurnedOnValue = kernel.ExecuteCommand( "zSpace.IsStylusLEDTurnedOn", null); bool isLEDTurnedOn = isLEDTurnedOnValue.GetBool(); MiddleVRTools.Log(2, "[+] zSpace stylus LED turned on? " + (isLEDTurnedOn ? "Yes" : "No") + "."); if (isLEDTurnedOn) { vrVec3 usedColorAsRGB = GetStylusLEDColor(); MiddleVRTools.Log(2, "[+] Current zSpace stylus LED color is: (" + usedColorAsRGB.x() + ", " + usedColorAsRGB.y() + ", " + usedColorAsRGB.z() + "). If set to black, no light will appear!"); } } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_V)) { if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) || deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT)) { // Stop vibration (nothing will happen if the stylus // is not vibrating). MiddleVRTools.Log(2, "[+] Trying to stop zSpace stylus vibration."); kernel.ExecuteCommand( "zSpace.StopStylusVibration", null); } else { // Start vibration. MiddleVRTools.Log(2, "[+] Trying to start zSpace stylus vibration with parameters:\n" + "- duration vibration: " + m_DurationVibration + " s,\n" + "- duration between two vibrations: " + m_DurationBetween + " s,\n" + "- number of vibrations: " + m_NumberOfVibrations + "."); vrValue vibrationValueList = vrValue.CreateList(); vibrationValueList.AddListItem(m_DurationVibration); vibrationValueList.AddListItem(m_DurationBetween); vibrationValueList.AddListItem(m_NumberOfVibrations); kernel.ExecuteCommand( "zSpace.StartStylusVibration", vibrationValueList); } } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_T)) { MiddleVRTools.Log(2, "[+] Trying to trigger a default zSpace stylus vibration."); kernel.ExecuteCommand( "zSpace.TriggerDefaultStylusVibration", true); // or false to stop the vibration (if any). } } }
protected void Update() { var deviceMgr = MiddleVR.VRDeviceMgr; if (deviceMgr != null) { var headTracker = deviceMgr.GetTracker("zSpace.Head.Tracker"); var stylusTracker = deviceMgr.GetTracker("zSpace.Stylus.Tracker"); if (headTracker == null || stylusTracker == null) { MiddleVRTools.Log(0, "[X] No head or stylus tracker found for zSpace. Did you load the driver?"); enabled = false; return; } bool headTargetIsVisible = headTracker.IsTracked(); if (headTargetWasVisible != headTargetIsVisible) { MiddleVRTools.Log(2, "[+] Head is " + (headTargetIsVisible ? "visible" : "invisible") + " by the zSpace device."); headTargetWasVisible = headTargetIsVisible; } bool stylusTargetIsVisible = stylusTracker.IsTracked(); if (stylusTargetWasVisible != stylusTargetIsVisible) { MiddleVRTools.Log(2, "[+] Stylus is " + (stylusTargetIsVisible ? "visible" : "invisible") + " by the zSpace device."); stylusTargetWasVisible = stylusTargetIsVisible; } } var kernel = MiddleVR.VRKernel; if (kernel != null && deviceMgr != null) { if (deviceMgr.IsKeyToggled(MiddleVR.VRK_C)) { Color c = m_UsingLEDColor0 ? m_LEDColor0 : m_LEDColor1; vrVec3 proposedColor = new vrVec3(c.r, c.g, c.b); MiddleVRTools.Log(2, "[+] Proposed stylus LED color sent to zSpace: (" + proposedColor.x() + ", " + proposedColor.y() + ", " + proposedColor.z() + ")."); kernel.ExecuteCommand( "zSpace.SetStylusLEDColor", proposedColor); m_UsingLEDColor0 = !m_UsingLEDColor0; vrVec3 usedColorAsRGB = GetStylusLEDColor(); MiddleVRTools.Log(2, "[+] Current zSpace stylus LED color is: (" + usedColorAsRGB.x() + ", " + usedColorAsRGB.y() + ", " + usedColorAsRGB.z() + ")."); } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_L)) { bool turnOnLight = true; if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) || deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT)) { turnOnLight = false; } MiddleVRTools.Log(2, "[+] Trying to turn " + (turnOnLight ? "on" : "off") + " the zSpace stylus LED."); kernel.ExecuteCommand( "zSpace.SetStylusLEDAsTurnedOn", turnOnLight); vrValue isLEDTurnedOnValue = kernel.ExecuteCommand( "zSpace.IsStylusLEDTurnedOn", null); bool isLEDTurnedOn = isLEDTurnedOnValue.GetBool(); MiddleVRTools.Log(2, "[+] zSpace stylus LED turned on? " + (isLEDTurnedOn ? "Yes" : "No") + "."); if (isLEDTurnedOn) { vrVec3 usedColorAsRGB = GetStylusLEDColor(); MiddleVRTools.Log(2, "[+] Current zSpace stylus LED color is: (" + usedColorAsRGB.x() + ", " + usedColorAsRGB.y() + ", " + usedColorAsRGB.z() + "). If set to black, no light will appear!"); } } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_V)) { if (deviceMgr.IsKeyPressed(MiddleVR.VRK_LSHIFT) || deviceMgr.IsKeyPressed(MiddleVR.VRK_RSHIFT)) { // Stop vibration (nothing will happen if the stylus // is not vibrating). MiddleVRTools.Log(2, "[+] Trying to stop zSpace stylus vibration."); kernel.ExecuteCommand( "zSpace.StopStylusVibration", null); } else { // Start vibration. MiddleVRTools.Log(2, "[+] Trying to start zSpace stylus vibration with parameters:\n" + "- duration vibration: " + m_DurationVibration + " s,\n" + "- duration between two vibrations: " + m_DurationBetween + " s,\n" + "- number of vibrations: " + m_NumberOfVibrations + "."); vrValue vibrationValueList = vrValue.CreateList(); vibrationValueList.AddListItem(m_DurationVibration); vibrationValueList.AddListItem(m_DurationBetween); vibrationValueList.AddListItem(m_NumberOfVibrations); // It is also possible to set the intensity of the vibration: // pass then a 4th argument with a float between 0.0 and 1.0. //vibrationValueList.AddListItem(1.0f); kernel.ExecuteCommand( "zSpace.StartStylusVibration", vibrationValueList); } } if (deviceMgr.IsKeyToggled(MiddleVR.VRK_T)) { MiddleVRTools.Log(2, "[+] Trying to trigger a default zSpace stylus vibration."); kernel.ExecuteCommand( "zSpace.TriggerDefaultStylusVibration", true); // or false to stop the vibration (if any). } } }
private void ConvertGeometry(Mesh mesh) { Vector3[] vertices = mesh.vertices; int[] triangles = mesh.triangles; MiddleVRTools.Log(3, "PhysicsBody: Number of vertices: " + vertices.Length); MiddleVRTools.Log(3, "PhysicsBody: Number of Triangles: " + triangles.Length); // We will reuse the same vectors to avoid many memory allocations. Vector3 vertexPos = new Vector3(); vrVec3 vPos = new vrVec3(); // We compute a matrix to scale vertices according to their world // coordinates, so this scale depends on the scales of the GameObject // parents. Matrices 4x4 are used because matrices 3x3 aren't available. vrMatrix worldMatrix = MVRTools.RawFromUnity(transform.localToWorldMatrix); worldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f)); worldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f)); vrQuat invRotWorlQ = worldMatrix.GetRotation(); invRotWorlQ = invRotWorlQ.Normalize().GetInverse(); vrMatrix invRotWorldMatrix = new vrMatrix(); invRotWorldMatrix.SetRotation(invRotWorlQ); invRotWorldMatrix.SetCol(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f)); invRotWorldMatrix.SetRow(3, new vrVec4(0.0f, 0.0f, 0.0f, 0.0f)); vrMatrix scaleWorldMatrix = invRotWorldMatrix.Mult(worldMatrix); Matrix4x4 scaleWorldMatrixUnity = MiddleVRTools.RawToUnity(scaleWorldMatrix); foreach (Vector3 vertex in vertices) { vertexPos = scaleWorldMatrixUnity.MultiplyPoint3x4(vertex); MiddleVRTools.FromUnity(vertexPos, ref vPos); m_Geometry.AddVertex(vPos); MiddleVRTools.Log(6, "PhysicsBody: Adding a vertex at position (" + vPos.x() + ", " + vPos.y() + ", " + vPos.z() + ")."); } for (int i = 0, iEnd = triangles.Length; i < iEnd; i += 3) { uint index0 = (uint)triangles[i]; uint index1 = (uint)triangles[i + 1]; uint index2 = (uint)triangles[i + 2]; m_Geometry.AddTriangle(index0, index1, index2); MiddleVRTools.Log(6, "PhysicsBody: Adding a triangle with vertex indexes (" + index0 + ", " + index1 + ", " + index2 + ")."); } }
void Impact( vrVec3 iImpactPoint ) { GameObject go = GameObject.Find("Impact"); GameObject impact = GameObject.Instantiate( go ); impact.GetComponent<Impact>().enabled = true; impact.transform.position = new Vector3(iImpactPoint.x(), iImpactPoint.y(), iImpactPoint.z()); //impact.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f); //impact.AddComponent<Impact>(); //impact.GetComponent<Renderer>().material = ImpactMaterial; }