// 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); } }
//! 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; } }