Advertisement
Ed94

IWVR fixed code to make marker fully work.

Oct 12th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.97 KB | None | 0 0
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Demonstrates how to create a simple interactable object
  4. //
  5. //=============================================================================
  6.  
  7. using UnityEngine;
  8. using System.Collections;
  9.  
  10. namespace Valve.VR.InteractionSystem
  11. {
  12.     //-------------------------------------------------------------------------
  13.     [RequireComponent(typeof(Interactable))]
  14.     public class BoardInteraction : MonoBehaviour
  15.     {
  16.         private Vector3    oldPosition;
  17.         private Quaternion oldRotation;
  18.  
  19.         private float attachTime;
  20.  
  21.         private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & (~Hand.AttachmentFlags.SnapOnAttach) & (~Hand.AttachmentFlags.DetachOthers);
  22.  
  23.         private bool lineActive;
  24.  
  25.         //Private
  26.         private int numCLicks = 0;
  27.  
  28.         private GameObject line;
  29.  
  30.         private LineRenderer currentLine;
  31.  
  32.         public float width = 0.05f;
  33.  
  34.         public Color color = Color.black;
  35.  
  36.         private MarkerTip marker;
  37.  
  38.         //-------------------------------------------------
  39.         void Awake()
  40.         {
  41.  
  42.         }
  43.  
  44.  
  45.         //-------------------------------------------------
  46.         // Called when a Hand starts hovering over this object
  47.         //-------------------------------------------------
  48.         private void OnHandHoverBegin(Hand hand)
  49.         {
  50.         }
  51.  
  52.  
  53.         //-------------------------------------------------
  54.         // Called when a Hand stops hovering over this object
  55.         //-------------------------------------------------
  56.         private void OnHandHoverEnd(Hand hand)
  57.         {
  58.         }
  59.  
  60.  
  61.         //-------------------------------------------------
  62.         // Called every Update() while a Hand is hovering over this object
  63.         //-------------------------------------------------
  64.         private void HandHoverUpdate(Hand hand)
  65.         {
  66.             if (hand.GetStandardInteractionButtonDown() || ((hand.controller != null) && hand.controller.GetPressDown(Valve.VR.EVRButtonId.k_EButton_Grip)))
  67.             {
  68.                 if (hand.currentAttachedObject != gameObject)
  69.                 {
  70.                     // Save our position/rotation so that we can restore it when we detach
  71.                     oldPosition = transform.position;
  72.                     oldRotation = transform.rotation;
  73.  
  74.                     // Call this to continue receiving HandHoverUpdate messages,
  75.                     // and prevent the hand from hovering over anything else
  76.                     hand.HoverLock(GetComponent<Interactable>());
  77.  
  78.                     // Attach this object to the hand
  79.                     hand.AttachObject(gameObject, attachmentFlags);
  80.                 }
  81.                 else
  82.                 {
  83.                     // Detach this object from the hand
  84.                     hand.DetachObject(gameObject);
  85.  
  86.                     // Call this to undo HoverLock
  87.                     hand.HoverUnlock(GetComponent<Interactable>());
  88.  
  89.                     // Restore position/rotation
  90.                     //transform.position = oldPosition;
  91.                     //transform.rotation = oldRotation;
  92.                 }
  93.             }
  94.         }
  95.  
  96.  
  97.         private void OnMarkerHoverBegin(MarkerTip marker)
  98.         {
  99.             line = new GameObject();
  100.  
  101.             currentLine = line.AddComponent<LineRenderer>();
  102.  
  103.             currentLine.startWidth = width;
  104.             currentLine.endWidth = width;
  105.  
  106.             currentLine.startColor = Color.grey;
  107.             currentLine.endColor = Color.grey;
  108.  
  109.             Debug.Log(currentLine.material);
  110.  
  111.             numCLicks = 0;
  112.  
  113.             lineActive = true;
  114.  
  115.             this.marker = marker;
  116.         }
  117.  
  118.         void Update()
  119.         {
  120.             if (lineActive)
  121.             {
  122.                 currentLine.positionCount = numCLicks + 1;
  123.  
  124.                 currentLine.SetPosition(numCLicks, new Vector3(marker.transform.position.x, marker.transform.position.y, transform.position.z - 0.01f));
  125.  
  126.                 numCLicks++;
  127.             }
  128.         }
  129.         //this update is i guesss what you wanted ^^.... from the below.
  130.         private void OnMarkerHoverUpdate(MarkerTip marker)
  131.         {
  132.            
  133.         }
  134.  
  135.         private void OnMarkerHoverEnd(MarkerTip marker)
  136.         {
  137.             lineActive = false;
  138.         }
  139.  
  140.  
  141.  
  142.         //-------------------------------------------------
  143.         // Called when this GameObject becomes attached to the hand
  144.         //-------------------------------------------------
  145.         private void OnAttachedToHand(Hand hand)
  146.         {
  147.             attachTime = Time.time;
  148.         }
  149.  
  150.  
  151.         //-------------------------------------------------
  152.         // Called when this GameObject is detached from the hand
  153.         //-------------------------------------------------
  154.         private void OnDetachedFromHand(Hand hand)
  155.         {
  156.         }
  157.  
  158.  
  159.         //-------------------------------------------------
  160.         // Called every Update() while this GameObject is attached to the hand
  161.         //-------------------------------------------------
  162.         private void HandAttachedUpdate(Hand hand)
  163.         {
  164.         }
  165.  
  166.  
  167.         //-------------------------------------------------
  168.         // Called when this attached GameObject becomes the primary attached object
  169.         //-------------------------------------------------
  170.         private void OnHandFocusAcquired(Hand hand)
  171.         {
  172.         }
  173.  
  174.  
  175.         //-------------------------------------------------
  176.         // Called when another attached GameObject becomes the primary attached object
  177.         //-------------------------------------------------
  178.         private void OnHandFocusLost(Hand hand)
  179.         {
  180.         }
  181.     }
  182. }
  183.  
  184.  
  185. using System.Collections;
  186. using System.Collections.Generic;
  187. using UnityEngine;
  188. using Valve.VR.InteractionSystem;
  189.  
  190. public class MarkerTip : MonoBehaviour {
  191.  
  192.     public Transform hoverSphereTransform;
  193.     public float hoverSphereRadius = 0.05f;
  194.     public LayerMask hoverLayerMask = -1;
  195.     public float hoverUpdateInterval = 0.1f;
  196.  
  197.     public bool hoverLocked { get; private set; }
  198.  
  199.     private Interactable _hoveringInteractable;
  200.  
  201.     private int prevOverlappingColliders = 0;
  202.  
  203.     private const int ColliderArraySize = 16;
  204.     private Collider[] overlappingColliders;
  205.  
  206.     private Player playerInstance;
  207.  
  208.     private void Awake()
  209.     {
  210.         if (hoverSphereTransform == null)
  211.         {
  212.             hoverSphereTransform = this.transform;
  213.         }
  214.     }
  215.     // Use this for initialization
  216.     void Start () {
  217.         playerInstance = Player.instance;
  218.  
  219.         // allocate array for colliders
  220.         overlappingColliders = new Collider[ColliderArraySize];
  221.         hoverLocked = false;
  222.     }
  223.    
  224.     // Update is called once per frame
  225.     void Update ()
  226.     {
  227.         UpdateHovering();
  228.     }
  229.  
  230.     //-------------------------------------------------
  231.     // The Interactable object this Hand is currently hovering over
  232.     //-------------------------------------------------
  233.     public Interactable hoveringInteractable
  234.     {
  235.         get { return _hoveringInteractable; }
  236.         set
  237.         {
  238.             if (_hoveringInteractable != value)
  239.             {
  240.                 if (_hoveringInteractable != null)
  241.                 {
  242.                     //_hoveringInteractable.SendMessage("OnHandHoverEnd", this, SendMessageOptions.DontRequireReceiver);
  243.                     _hoveringInteractable.SendMessage("OnMarkerHoverEnd", this, SendMessageOptions.DontRequireReceiver);
  244.  
  245.                 }
  246.  
  247.                 _hoveringInteractable = value;
  248.  
  249.                 if (_hoveringInteractable != null)
  250.                 {
  251.                     //_hoveringInteractable.SendMessage("OnHandHoverBegin", this, SendMessageOptions.DontRequireReceiver);
  252.                     _hoveringInteractable.SendMessage("OnMarkerHoverBegin", this, SendMessageOptions.DontRequireReceiver);
  253.  
  254.                 }
  255.             }
  256.         }
  257.     }
  258.  
  259.     private void UpdateHovering()
  260.     {
  261.  
  262.         if (hoverLocked)
  263.             return;
  264.  
  265.         float closestDistance = float.MaxValue;
  266.         Interactable closestInteractable = null;
  267.  
  268.         // Pick the closest hovering
  269.         float flHoverRadiusScale   = playerInstance.transform.lossyScale.x * 2.5f;
  270.         float flScaledSphereRadius = hoverSphereRadius * flHoverRadiusScale;
  271.  
  272.         // if we're close to the floor, increase the radius to make things easier to pick up
  273.         float handDiff = Mathf.Abs(transform.position.y - playerInstance.trackingOriginTransform.position.y);
  274.         float boxMult = Util.RemapNumberClamped(handDiff, 0.0f, 0.5f * flHoverRadiusScale, 5.0f, 1.0f) * flHoverRadiusScale;
  275.  
  276.         // null out old vals
  277.         for (int i = 0; i < overlappingColliders.Length; ++i)
  278.         {
  279.             overlappingColliders[i] = null;
  280.         }
  281.  
  282.         //Physics.OverlapBoxNonAlloc(
  283.         //    hoverSphereTransform.position - new Vector3(0, flScaledSphereRadius * boxMult - flScaledSphereRadius, 0),
  284.            
  285.         //    new Vector3(flScaledSphereRadius, flScaledSphereRadius * boxMult * 2.0f, flScaledSphereRadius),
  286.         //    overlappingColliders,
  287.         //    Quaternion.identity,
  288.         //    hoverLayerMask.value
  289.         //);
  290.  
  291.         Physics.OverlapSphereNonAlloc(
  292.             hoverSphereTransform.position - new Vector3(0, flScaledSphereRadius * boxMult - flScaledSphereRadius, 0),
  293.             flScaledSphereRadius,
  294.             overlappingColliders,
  295.             hoverLayerMask.value
  296.             );
  297.  
  298.         // DebugVar
  299.         int iActualColliderCount = 0;
  300.  
  301.         foreach (Collider collider in overlappingColliders)
  302.         {
  303.             Debug.Log(collider);
  304.  
  305.             if (collider == null)
  306.                 continue;
  307.  
  308.             Interactable contacting = collider.GetComponentInParent<Interactable>();
  309.  
  310.             // Yeah, it's null, skip
  311.             if (contacting == null)
  312.                 continue;
  313.  
  314.             // Ignore this collider for hovering
  315.             IgnoreHovering ignore = collider.GetComponent<IgnoreHovering>();
  316.             if (ignore != null)
  317.             {
  318.                 if (ignore.onlyIgnoreHand == null || ignore.onlyIgnoreHand == this)
  319.                 {
  320.                     continue;
  321.                 }
  322.             }
  323.  
  324.             // Best candidate so far...
  325.             float distance = Vector3.Distance(contacting.transform.position, hoverSphereTransform.position);
  326.  
  327.             if (distance < closestDistance && !collider.Equals(this.GetComponentInParent<CapsuleCollider>()))
  328.             {
  329.                 closestDistance     = distance  ;
  330.                 closestInteractable = contacting;
  331.             }
  332.  
  333.             iActualColliderCount++;
  334.         }
  335.  
  336.         // Hover on this one
  337.         hoveringInteractable = closestInteractable;
  338.  
  339.         if (iActualColliderCount > 0 && iActualColliderCount != prevOverlappingColliders)
  340.         {
  341.             prevOverlappingColliders = iActualColliderCount;
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement