void TurnEffectOn() { if (HapticDevice == null) { return; //If there is no device, bail out early. } // If a haptic effect has not been assigned through Open Haptics, assign one now. if (FXID == -1) { FXID = HapticPlugin.effects_assignEffect(HapticDevice.configName); if (FXID == -1) // Still broken? { Debug.LogError("Unable to assign Haptic effect."); return; } } // Send the effect settings to OpenHaptics. double[] pos = { 0.0, 0.0, 0.0 }; // Position (not used for vibration) double[] dir = { 0.0, 1.0, 0.0 }; // Direction of vibration HapticPlugin.effects_settings( HapticDevice.configName, FXID, 0.33, // Gain 0.33, // Magnitude 300, // Frequency pos, // Position (not used for vibration) dir); //Direction. HapticPlugin.effects_type(HapticDevice.configName, FXID, 4); // Vibration effect == 4 HapticPlugin.effects_startEffect(HapticDevice.configName, FXID); }
// Use this for initialization void Start() { Haptic = gameObject.GetComponent(typeof(HapticPlugin)) as HapticPlugin; if (Haptic == null) { Debug.LogError("HapticPluginSafetyScript must be attached to the same object as the HapticPlugin script."); } }
void TurnEffectOff() { if (HapticDevice == null) { return; //If there is no device, bail out early. } if (FXID == -1) { return; //If there is no effect, bail out early. } HapticPlugin.effects_stopEffect(HapticDevice.configName, FXID); }
void Start() { vibrationOn = false; if (HapticDevice == null) { HapticDevice = (HapticPlugin)FindObjectOfType(typeof(HapticPlugin)); } if (HapticDevice /* STILL */ == null) { Debug.LogError("This script requires that Haptic Device be assigned."); } }
void OnDestroy() { //For every haptic device, send a Stop event to OpenHaptics for (int ii = 0; ii < devices.Length; ii++) { HapticPlugin device = devices [ii]; if (device == null) { continue; } int ID = FXID [ii]; HapticPlugin.effects_stopEffect(device.configName, ID); } }
// Use this for initialization void Start() { // find the Haptic Device Haptic = gameObject.GetComponent(typeof(HapticPlugin)) as HapticPlugin; if (Haptic == null) { Debug.LogError("HapticMouse script must be attached to the same object as the HapticPlugin script."); } // Find the camera if (camera == null) { camera = FindObjectOfType <Camera>(); } }
//public GameObject bt = null; void Start() { //hapticPlugin = gameObject.GetComponent<HapticPlugin>(); hapticPlugin = GameObject.Find("HapticDevice").GetComponent <HapticPlugin>(); instCurette = GameObject.Find("17221_Curette"); drill = GameObject.Find("DrillAcima"); //filler = GameObject.Find("Aparelhodental2"); instPick = GameObject.Find("16972_dentist_pick"); uv = GameObject.Find("Aparelhodental1"); PopulateList(); instCurette.SetActive(true); drill.SetActive(false); instPick.SetActive(false); uv.SetActive(false); }
//! Stop grabbing an object. (Like opening a claw.) Normally called when the button is released. void release() { if (grabbing == null) //Nothing to release { return; } Debug.Assert(joint != null); //Recréer une forme haptique pour les objets qu'on a manipulé et qu'on lâche //Ne sert que si on utilise le ShapeRemove quand on grab. Si on utilise le repositionnement de la shape haptique, bout de code inutile Mesh mesh = null; MeshCollider collider = grabbing.GetComponent <MeshCollider>(); if (collider != null) { mesh = collider.sharedMesh; } if (mesh == null) { MeshFilter filter = grabbing.GetComponent <MeshFilter>(); if (filter != null) { mesh = filter.mesh; } } double[] vertices = HapticPlugin.Vector3ArrayToDoubleArray(mesh.vertices); int[] triangles = mesh.triangles; //Remettre l'objet en Touchable pour pouvoir ravoir l'haptique dès qu'on l'a laché grabbing.tag = "Touchable"; //HapticPlugin.shape_define(grabbing.GetInstanceID(), grabbing.name, vertices, triangles, vertices.Length, triangles.Length); joint.connectedBody = null; Destroy(joint); grabbing = null; if (physicsToggleStyle != PhysicsToggleStyle.none) { hapticDevice.GetComponent <HapticPlugin>().PhysicsManipulationEnabled = false; } }
//! Start() is called at the beginning of the simulation. //! //! It will identify the Haptic devices, initialize variables internal to this script, //! and request an Effect ID from Open Haptics. (One for each device.) //! void Start() { //Initialize the list of haptic devices. devices = (HapticPlugin[])Object.FindObjectsOfType(typeof(HapticPlugin)); inTheZone = new bool[devices.Length]; devicePoint = new Vector3[devices.Length]; delta = new float[devices.Length]; FXID = new int[devices.Length]; // Generate an OpenHaptics effect ID for each of the devices. for (int ii = 0; ii < devices.Length; ii++) { inTheZone [ii] = false; devicePoint [ii] = Vector3.zero; delta [ii] = 0.0f; FXID [ii] = HapticPlugin.effects_assignEffect(devices [ii].configName); } }
// Simple initialization. void Start() { if (HapticDevice == null) { HapticDevice = (HapticPlugin)FindObjectOfType(typeof(HapticPlugin)); } if (HapticDevice /* STILL */ == null) { Debug.LogError("This script requires that Haptic Device be assigned."); } if (depthMeter == null) { Debug.LogError("This script requires a UI Image be linked to 'depthMeter'"); } if (speedMeter == null) { Debug.LogError("This script requires a UI Image be linked to 'speedMeter'"); } }
// Update is called once per frame void Update() { // Make sure camera and plugin exist. if (masterCamera == null) { return; } // Deal with the greeting float T = Time.realtimeSinceStartup - greetingTime; if (T > 0 && Greeting != null) { float alpha = Mathf.Max(1.0f - T, 0f); Greeting.GetComponent <CanvasGroup>().alpha = alpha; } HapticPlugin plugin = (HapticPlugin)gameObject.GetComponent(typeof(HapticPlugin)); if (plugin == null) { Debug.LogError("HapticFitToCamera must be attached to a HapticDevice."); return; } // Extents are in array, these are the indexes const int minX = 0; const int minY = 1; const int minZ = 2; const int maxX = 3; const int maxY = 4; const int maxZ = 5; // dimensions and position of haptic volume (In raw, unscaled coordinates.) float hapticWidth; float hapticHeight; float hapticDepth; Vector3 hapticCenter; if (FitToWorkspace == BoxType.usableWorkspace) { hapticWidth = (float)(plugin.usable_extents[maxX] - plugin.usable_extents [minX]); hapticHeight = (float)(plugin.usable_extents[maxY] - plugin.usable_extents [minY]); hapticDepth = (float)(plugin.usable_extents [maxZ] - plugin.usable_extents [minZ]); hapticCenter = new Vector3( (float)(plugin.usable_extents[maxX] + plugin.usable_extents [minX]) / 2, (float)(plugin.usable_extents[maxY] + plugin.usable_extents [minY]) / 2, (float)(plugin.usable_extents[maxZ] + plugin.usable_extents [minZ]) / 2); } else { hapticWidth = (float)(plugin.max_extents[maxX] - plugin.max_extents [minX]); hapticHeight = (float)(plugin.max_extents[maxY] - plugin.max_extents [minY]); hapticDepth = (float)(plugin.max_extents [maxZ] - plugin.max_extents [minZ]); hapticCenter = new Vector3( (float)(plugin.max_extents [maxX] + plugin.max_extents [minX]) / 2, (float)(plugin.max_extents [maxY] + plugin.max_extents [minY]) / 2, (float)(plugin.max_extents [maxZ] + plugin.max_extents [minZ]) / 2); } // Add the margin to the dimensions of the box we're trying to place. hapticWidth *= (1.0f + margin); hapticHeight *= (1.0f + margin); hapticDepth *= (1.0f + margin); if (gameObject.transform.parent != null) { hapticWidth *= gameObject.transform.parent.localScale.x; hapticHeight *= gameObject.transform.parent.localScale.y; hapticDepth *= gameObject.transform.parent.localScale.z; } // How Close to the camera should the haptic box get. float camPlacementPlane = MinimumDepth; // Dimantions of the box defined by camera-to-box distance defined above, and the camera frustrum float camDepth = masterCamera.nearClipPlane + (masterCamera.farClipPlane - masterCamera.nearClipPlane) * camPlacementPlane; // Halfway through the camera space. float camWidth = (masterCamera.ViewportToWorldPoint(new Vector3(0, 0, camDepth)) - masterCamera.ViewportToWorldPoint(new Vector3(1, 0, camDepth))).magnitude; float camHeight = (masterCamera.ViewportToWorldPoint(new Vector3(0, 0, camDepth)) - masterCamera.ViewportToWorldPoint(new Vector3(0, 1, camDepth))).magnitude; // Compare the haptic box with the camera box to determine caling. float ratioX = camWidth / hapticWidth; float ratioY = camHeight / hapticHeight; float ratioZ = (masterCamera.farClipPlane - camDepth) / (hapticDepth); if (constraint == ConstraintType.uniform) { // Uniform scaling must take the smallest dimension. (Otherwise there's an overhang.) float ratio = Mathf.Min(ratioZ, Mathf.Min(ratioX, ratioY)); ratioX = ratio; ratioY = ratio; ratioZ = ratio; } else if (constraint == ConstraintType.nonuniformConstrainZ) { // Don't let the Z go out to the horizon. Find some reasonable value for it. float ratio = Mathf.Min(ratioZ, Mathf.Max(ratioX, ratioY)); ratioZ = ratioX; } // Apply the rotation, scale and translation to the haptics objectt. gameObject.transform.rotation = masterCamera.transform.rotation; gameObject.transform.localScale = new Vector3(ratioX, ratioY, ratioZ); gameObject.transform.position = masterCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDepth + (ratioZ * hapticDepth) / 2)); /* * // If the haptics volume was offset from zero, add that compensation back in. * hapticCenter.Scale(gameObject.transform.localScale); * gameObject.transform.position -= gameObject.transform.rotation *hapticCenter;*/ }
//! Update is called once per frame and updates OpenHaptics with the current suface materials. void Update() { bool needUpdate = false; if (hlStiffness != oldStiffness) { needUpdate = true; } if (hlDamping != oldDamping) { needUpdate = true; } if (hlStaticFriction != oldStaticFriction) { needUpdate = true; } if (hlDynamicFriction != oldDynamicFriction) { needUpdate = true; } if (hlPopThrough != oldPopThrough) { needUpdate = true; } if (snapDistance != oldSnapDistance) { needUpdate = true; } if (hlTouchModel != oldTouchModel) { needUpdate = true; } if (Flip_Normals != oldFlipNormals) { needUpdate = true; } if (hlTouchable != oldFacing) { needUpdate = true; } if (needUpdate) { HapticPlugin.shape_settings(gameObject.GetInstanceID(), hlStiffness, hlDamping, hlStaticFriction, hlDynamicFriction, hlPopThrough); int M = 0; if (hlTouchModel == HLTOUCH_MODEL.HL_CONSTRAINT) { M = 1; } HapticPlugin.shape_constraintSettings(gameObject.GetInstanceID(), M, snapDistance); HapticPlugin.shape_flipNormals(gameObject.GetInstanceID(), Flip_Normals); int T = 1; if (hlTouchable == HLFACING.HL_BACK) { T = 2; } if (hlTouchable == HLFACING.HL_FRONT_AND_BACK) { T = 3; } HapticPlugin.shape_facing(gameObject.GetInstanceID(), T); oldStiffness = hlStiffness; oldDamping = hlDamping; oldStaticFriction = hlStaticFriction; oldDynamicFriction = hlDynamicFriction; oldTouchModel = hlTouchModel; oldSnapDistance = snapDistance; oldPopThrough = hlPopThrough; oldFlipNormals = Flip_Normals; oldFacing = hlTouchable; } }
void OnDestroy() { HapticPlugin.shape_remove(gameObject.GetInstanceID()); }
//! Begin grabbing an object. (Like closing a claw.) Normally called when the button is pressed. void grab() { GameObject touchedObject = touching; if (touchedObject == null) // No Unity Collision? { // Maybe there's a Haptic Collision touchedObject = hapticDevice.GetComponent <HapticPlugin>().touching; } if (grabbing != null) // Already grabbing { return; } if (touchedObject == null) // Nothing to grab { return; } // Grabbing a grabber is bad news. if (touchedObject.CompareTag("Gripper")) { return; } Debug.Log(" Object : " + touchedObject.name + " Tag : " + touchedObject.tag); grabbing = touchedObject; //Debug.logger.Log("Grabbing Object : " + grabbing.name); Rigidbody body = grabbing.GetComponent <Rigidbody>(); // If this doesn't have a rigidbody, walk up the tree. // It may be PART of a larger physics object. while (body == null) { //Debug.logger.Log("Grabbing : " + grabbing.name + " Has no body. Finding Parent. "); if (grabbing.transform.parent == null) { grabbing = null; return; } GameObject parent = grabbing.transform.parent.gameObject; if (parent == null) { grabbing = null; return; } grabbing = parent; body = grabbing.GetComponent <Rigidbody>(); } joint = (FixedJoint)gameObject.AddComponent(typeof(FixedJoint)); joint.connectedBody = body; //Tentative pour enlever le retour haptique d'un objet qu'on est en train d'attraper, car sinon les collisions permanentes avec l'objet créent des forces wtf //Enlever aussi tout shape haptique pour pas avoir de forme fantome - marche bien, mais la réinstanciation est un peu sèche... if (body != null) { //HapticPlugin.shape_remove(touchedObject.GetInstanceID()); grabbing.tag = "Untagged"; //test pour déplacer la shape haptique sur un proxy qui est hors scène - marche a peu près aussi, mais sec quand on pousse vers la shape... //Pas vraiment plus sec que le passage untagged/touchable en fait, il y a toujours ces problèmes quand on lache... Matrix4x4 M = GameObject.Find("Proxy_Haptique").transform.localToWorldMatrix; M = this.transform.worldToLocalMatrix * M; double[] M16 = HapticPlugin.MatrixToDoubleArray(M); HapticPlugin.shape_setTransform(touchedObject.GetInstanceID(), M16); } }
//! Update() is called once per frame. //! //! This function //! - Determines if a haptic stylus is inside the collider //! - Updates the effect settings. //! - Starts and stops the effect when appropriate. void Update() { // Find the pointer to the collider that defines the "zone". Collider collider = gameObject.GetComponent <Collider>(); if (collider == null) { Debug.LogError("This Haptic Effect Zone requires a collider"); return; } // Update the World-Space vectors focusPointWorld = transform.TransformPoint(Position); directionWorld = transform.TransformDirection(Direction); // Update the effect seperately for each haptic device. for (int ii = 0; ii < devices.Length; ii++) { HapticPlugin device = devices [ii]; bool oldInTheZone = inTheZone[ii]; int ID = FXID [ii]; // If a haptic effect has not been assigned through Open Haptics, assign one now. if (ID == -1) { FXID [ii] = HapticPlugin.effects_assignEffect(devices [ii].configName); ID = FXID [ii]; if (ID == -1) // Still broken? { Debug.LogError("Unable to assign Haptic effect."); continue; } } // Determine if the stylus is in the "zone". Vector3 StylusPos = device.stylusPositionWorld; //World Coordinates Vector3 CP = collider.ClosestPoint(StylusPos); //World Coordinates devicePoint[ii] = CP; delta[ii] = (CP - StylusPos).magnitude; //If the stylus is within the Zone, The ClosestPoint and the Stylus point will be identical. if (delta[ii] <= Mathf.Epsilon) { inTheZone [ii] = true; // Convert from the World coordinates to coordinates relative to the haptic device. Vector3 focalPointDevLocal = device.transform.InverseTransformPoint(focusPointWorld); Vector3 rotationDevLocal = device.transform.InverseTransformDirection(directionWorld); double[] pos = { focalPointDevLocal.x, focalPointDevLocal.y, focalPointDevLocal.z }; double[] dir = { rotationDevLocal.x, rotationDevLocal.y, rotationDevLocal.z }; double Mag = Magnitude; if (device.isInSafetyMode()) { Mag = 0; } // Send the current effect settings to OpenHaptics. HapticPlugin.effects_settings( device.configName, ID, Gain, Mag, Frequency, pos, dir); HapticPlugin.effects_type( device.configName, ID, (int)effectType); } else { inTheZone [ii] = false; // Note : If the device is not in the "Zone", there is no need to update the effect settings. } // If the on/off state has changed since last frame, send a Start or Stop event to OpenHaptics if (oldInTheZone != inTheZone [ii]) { if (inTheZone [ii]) { HapticPlugin.effects_startEffect(device.configName, ID); } else { HapticPlugin.effects_stopEffect(device.configName, ID); } } } }
// Use this for initialization void Start() { device = (HapticPlugin)Object.FindObjectOfType(typeof(HapticPlugin)); }
// Update is called once per frame void Update() { // Find the pointer to the collider that defines the "zone". Collider collider = gameObject.GetComponent <Collider>(); if (collider == null) { Debug.LogError("This Haptic Surface Demo Effect requires a collider"); return; } if (FrictionTexture == null) { Debug.LogError("This Haptic Surface Demo Effect requires a texture assigned to it."); return; } Vector3 StylusPos = device.stylusPositionWorld; //World Coordinates Vector3 CP = collider.ClosestPointOnBounds(StylusPos); //World Coordinates float delta = (CP - StylusPos).magnitude; if (delta < 1.0f) { Vector3 direction = transform.position - CP; direction.Normalize(); // Cast a ray between the stylus and the center of the collider RaycastHit[] hits = Physics.RaycastAll(CP, direction); //There may be some false positives in the list, so loop through //and find the hit on the current collider. foreach (RaycastHit H in hits) { if (H.collider == collider) { // This is the correct hit, so retrieve the UV values... Vector2 UV = H.textureCoord; // Scale the UV to the size of the texture... int U = (int)(UV.x * FrictionTexture.width); int V = (int)(UV.y * FrictionTexture.height); // Retrieve the color of that pixel. Color C = FrictionTexture.GetPixel(U, V); luminocity = C.grayscale; break; } } // Assign the haptic material setttings so that they transition proportionatly // between the two values based on the luminocity of the texel. float Value = luminocity; float inVal = 1.0f - Value; HapticPlugin.shape_settings(gameObject.GetInstanceID(), hlStiffness * Value + hlStiffness2 * inVal, hlDamping * Value + hlDamping2 * inVal, hlStaticFriction * Value + hlStaticFriction2 * inVal, hlDynamicFriction * Value + hlDynamicFriction2 * inVal, hlPopThrough * Value + hlPopThrough2 * inVal); } }
// Use this for initialization void Start() { Haptic = gameObject.GetComponent(typeof(HapticPlugin)) as HapticPlugin; }