123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- using UnityEngine;
- using System.Collections;
- using System;
- namespace Valve.VR.InteractionSystem
- {
- public class RenderModel : MonoBehaviour
- {
- public GameObject handPrefab;
- protected GameObject handInstance;
- protected Renderer[] handRenderers;
- public bool displayHandByDefault = true;
- protected SteamVR_Behaviour_Skeleton handSkeleton;
- protected Animator handAnimator;
- protected string animatorParameterStateName = "AnimationState";
- protected int handAnimatorStateId = -1;
- [Space]
- public GameObject controllerPrefab;
- protected GameObject controllerInstance;
- protected Renderer[] controllerRenderers;
- protected SteamVR_RenderModel controllerRenderModel;
- public bool displayControllerByDefault = true;
- protected Material delayedSetMaterial;
- public event Action onControllerLoaded;
- protected SteamVR_Events.Action renderModelLoadedAction;
- protected SteamVR_Input_Sources inputSource;
- protected void Awake()
- {
- renderModelLoadedAction = SteamVR_Events.RenderModelLoadedAction(OnRenderModelLoaded);
- InitializeHand();
- InitializeController();
- }
- protected void InitializeHand()
- {
- if (handPrefab != null)
- {
- handInstance = GameObject.Instantiate(handPrefab);
- handInstance.transform.parent = this.transform;
- handInstance.transform.localPosition = Vector3.zero;
- handInstance.transform.localRotation = Quaternion.identity;
- handInstance.transform.localScale = handPrefab.transform.localScale;
- handSkeleton = handInstance.GetComponent<SteamVR_Behaviour_Skeleton>();
- handSkeleton.origin = Player.instance.trackingOriginTransform;
- handSkeleton.updatePose = false;
- handSkeleton.skeletonAction.onActiveChange += OnSkeletonActiveChange;
- handRenderers = handInstance.GetComponentsInChildren<Renderer>();
- if (displayHandByDefault == false)
- SetHandVisibility(false);
- handAnimator = handInstance.GetComponentInChildren<Animator>();
- if (handSkeleton.skeletonAction.activeBinding == false)
- {
- Debug.LogWarning("Skeleton action: " + handSkeleton.skeletonAction.GetPath() + " is not bound. Your controller may not support SteamVR Skeleton Input.");
- DestroyHand();
- }
- }
- }
- protected void InitializeController()
- {
- if (controllerPrefab != null)
- {
- controllerInstance = GameObject.Instantiate(controllerPrefab);
- controllerInstance.transform.parent = this.transform;
- controllerInstance.transform.localPosition = Vector3.zero;
- controllerInstance.transform.localRotation = Quaternion.identity;
- controllerInstance.transform.localScale = controllerPrefab.transform.localScale;
- controllerRenderModel = controllerInstance.GetComponent<SteamVR_RenderModel>();
- }
- }
- protected virtual void DestroyHand()
- {
- if (handSkeleton != null)
- handSkeleton.skeletonAction.onActiveChange -= OnSkeletonActiveChange;
- if (handInstance != null)
- {
- Destroy(handInstance);
- handRenderers = null;
- handInstance = null;
- handSkeleton = null;
- handAnimator = null;
- }
- }
- protected virtual void OnSkeletonActiveChange(SteamVR_Action_Skeleton changedAction, bool newState)
- {
- if (newState)
- {
- InitializeHand();
- }
- else
- {
- DestroyHand();
- }
- }
- protected void OnEnable()
- {
- renderModelLoadedAction.enabled = true;
- }
- protected void OnDisable()
- {
- renderModelLoadedAction.enabled = false;
- }
- protected void OnDestroy()
- {
- DestroyHand();
- }
- public SteamVR_Behaviour_Skeleton GetSkeleton()
- {
- return handSkeleton;
- }
- public virtual void SetInputSource(SteamVR_Input_Sources newInputSource)
- {
- inputSource = newInputSource;
- if (controllerRenderModel != null)
- controllerRenderModel.SetInputSource(inputSource);
- }
- public virtual void OnHandInitialized(int deviceIndex)
- {
- controllerRenderModel.SetInputSource(inputSource);
- controllerRenderModel.SetDeviceIndex(deviceIndex);
- }
- public void MatchHandToTransform(Transform match)
- {
- if (handInstance != null)
- {
- handInstance.transform.position = match.transform.position;
- handInstance.transform.rotation = match.transform.rotation;
- }
- }
- public void SetHandPosition(Vector3 newPosition)
- {
- if (handInstance != null)
- {
- handInstance.transform.position = newPosition;
- }
- }
- public void SetHandRotation(Quaternion newRotation)
- {
- if (handInstance != null)
- {
- handInstance.transform.rotation = newRotation;
- }
- }
- public Vector3 GetHandPosition()
- {
- if (handInstance != null)
- {
- return handInstance.transform.position;
- }
- return Vector3.zero;
- }
- public Quaternion GetHandRotation()
- {
- if (handInstance != null)
- {
- return handInstance.transform.rotation;
- }
- return Quaternion.identity;
- }
- private void OnRenderModelLoaded(SteamVR_RenderModel loadedRenderModel, bool success)
- {
- if (controllerRenderModel == loadedRenderModel)
- {
- controllerRenderers = controllerInstance.GetComponentsInChildren<Renderer>();
- if (displayControllerByDefault == false)
- SetControllerVisibility(false);
- if (delayedSetMaterial != null)
- SetControllerMaterial(delayedSetMaterial);
- if (onControllerLoaded != null)
- onControllerLoaded.Invoke();
- }
- }
- public void SetVisibility(bool state, bool overrideDefault = false)
- {
- if (state == false || displayControllerByDefault || overrideDefault)
- SetControllerVisibility(state);
- if (state == false || displayHandByDefault || overrideDefault)
- SetHandVisibility(state);
- }
- public void Show(bool overrideDefault = false)
- {
- SetVisibility(true, overrideDefault);
- }
- public void Hide()
- {
- SetVisibility(false);
- }
- public virtual void SetMaterial(Material material)
- {
- SetControllerMaterial(material);
- SetHandMaterial(material);
- }
- public void SetControllerMaterial(Material material)
- {
- if (controllerRenderers == null)
- {
- delayedSetMaterial = material;
- return;
- }
- for (int rendererIndex = 0; rendererIndex < controllerRenderers.Length; rendererIndex++)
- {
- controllerRenderers[rendererIndex].material = material;
- }
- }
- public void SetHandMaterial(Material material)
- {
- for (int rendererIndex = 0; rendererIndex < handRenderers.Length; rendererIndex++)
- {
- handRenderers[rendererIndex].material = material;
- }
- }
- public void SetControllerVisibility(bool state, bool permanent = false)
- {
- if (permanent)
- displayControllerByDefault = state;
- if (controllerRenderers == null)
- return;
- for (int rendererIndex = 0; rendererIndex < controllerRenderers.Length; rendererIndex++)
- {
- controllerRenderers[rendererIndex].enabled = state;
- }
- }
- public void SetHandVisibility(bool state, bool permanent = false)
- {
- if (permanent)
- displayHandByDefault = state;
- if (handRenderers == null)
- return;
- for (int rendererIndex = 0; rendererIndex < handRenderers.Length; rendererIndex++)
- {
- handRenderers[rendererIndex].enabled = state;
- }
- }
- public bool IsHandVisibile()
- {
- if (handRenderers == null)
- return false;
- for (int rendererIndex = 0; rendererIndex < handRenderers.Length; rendererIndex++)
- {
- if (handRenderers[rendererIndex].enabled)
- return true;
- }
- return false;
- }
- public bool IsControllerVisibile()
- {
- if (controllerRenderers == null)
- return false;
- for (int rendererIndex = 0; rendererIndex < controllerRenderers.Length; rendererIndex++)
- {
- if (controllerRenderers[rendererIndex].enabled)
- return true;
- }
- return false;
- }
- public Transform GetBone(int boneIndex)
- {
- if (handSkeleton != null)
- {
- return handSkeleton.GetBone(boneIndex);
- }
- return null;
- }
- public Vector3 GetBonePosition(int boneIndex, bool local = false)
- {
- if (handSkeleton != null)
- {
- return handSkeleton.GetBonePosition(boneIndex, local);
- }
- return Vector3.zero;
- }
- public Vector3 GetControllerPosition(string componentName = null)
- {
- if (controllerRenderModel != null)
- return controllerRenderModel.GetComponentTransform(componentName).position;
- return Vector3.zero;
- }
- public Quaternion GetBoneRotation(int boneIndex, bool local = false)
- {
- if (handSkeleton != null)
- {
- return handSkeleton.GetBoneRotation(boneIndex, local);
- }
- return Quaternion.identity;
- }
- public void SetSkeletonRangeOfMotion(EVRSkeletalMotionRange newRangeOfMotion, float blendOverSeconds = 0.1f)
- {
- if (handSkeleton != null)
- {
- handSkeleton.SetRangeOfMotion(newRangeOfMotion, blendOverSeconds);
- }
- }
- public EVRSkeletalMotionRange GetSkeletonRangeOfMotion
- {
- get
- {
- if (handSkeleton != null)
- return handSkeleton.rangeOfMotion;
- else
- return EVRSkeletalMotionRange.WithController;
- }
- }
- public void SetTemporarySkeletonRangeOfMotion(SkeletalMotionRangeChange temporaryRangeOfMotionChange, float blendOverSeconds = 0.1f)
- {
- if (handSkeleton != null)
- {
- handSkeleton.SetTemporaryRangeOfMotion((EVRSkeletalMotionRange)temporaryRangeOfMotionChange, blendOverSeconds);
- }
- }
- public void ResetTemporarySkeletonRangeOfMotion(float blendOverSeconds = 0.1f)
- {
- if (handSkeleton != null)
- {
- handSkeleton.ResetTemporaryRangeOfMotion(blendOverSeconds);
- }
- }
- public void SetAnimationState(int stateValue)
- {
- if (handSkeleton != null)
- {
- if (handSkeleton.isBlending == false)
- handSkeleton.BlendToAnimation();
- if (CheckAnimatorInit())
- handAnimator.SetInteger(handAnimatorStateId, stateValue);
- }
- }
- public void StopAnimation()
- {
- if (handSkeleton != null)
- {
- if (handSkeleton.isBlending == false)
- handSkeleton.BlendToSkeleton();
- if (CheckAnimatorInit())
- handAnimator.SetInteger(handAnimatorStateId, 0);
- }
- }
- private bool CheckAnimatorInit()
- {
- if (handAnimatorStateId == -1 && handAnimator != null)
- {
- if (handAnimator.gameObject.activeInHierarchy && handAnimator.isInitialized)
- {
- AnimatorControllerParameter[] parameters = handAnimator.parameters;
- for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
- {
- if (string.Equals(parameters[parameterIndex].name, animatorParameterStateName, StringComparison.CurrentCultureIgnoreCase))
- handAnimatorStateId = parameters[parameterIndex].nameHash;
- }
- }
- }
- return handAnimatorStateId != -1 && handAnimator != null && handAnimator.isInitialized;
- }
-
- }
- }
|