using BitStrap; using UnityEngine; using UnityEngine.PostProcessing; using UnityEngine.UI; public class ModelViewerManager : MonoBehaviour { public Camera modelCamera; public RenderTexture renderTextureSmall; public RenderTexture renderTextureBig; public Color bgColorSmall = Color.black; public Color bgColorBig = Color.black; public Transform modelParent; public Text header; private Rigidbody modelParentRigidbody; [ReadOnly] public ModelViewerObject currentObject; [ReadOnly] public bool isBigView; [ReadOnly] public bool rotateSmall = true; public bool rotate = true; public float baitSizeFactor = 16f; [Header("Animations")] public float zoomSpeed = 1f; public float rotateSpeed = 30f; public float rotateBigSpeed = 10f; public float inputRotateSpeed = 30f; public float inputRotateForce = 1500f; [ReadOnly] public ModelViewerObject myPrefab; [ReadOnly] public EquipmentObject equipmentObject; private void Awake() { Initialize(); } public void Initialize() { modelParentRigidbody = modelParent.GetComponent(); modelCamera.targetTexture = renderTextureSmall; modelCamera.fieldOfView = 60f; } public void InstantiateObject(EquipmentObject selectedObject) { equipmentObject = selectedObject; if (equipmentObject != null) { InstantiateObject(equipmentObject.prefab.GetComponent(), selectedObject.GetFullName()); } else { InstantiateObject(null, string.Empty); } } public void InstantiateObject(ModelViewerObject prefab, string objectName) { if ((bool)currentObject) { Object.DestroyImmediate(currentObject.gameObject); } modelParent.transform.localPosition = Vector3.zero; rotate = true; myPrefab = prefab; if ((bool)prefab) { currentObject = Object.Instantiate(prefab); currentObject.transform.parent = modelParent; Utilities.SetLayerRecursively(currentObject.gameObject, LayerMask.NameToLayer("Bait")); currentObject.modelViewerManager = this; currentObject.Initialize(); currentObject.transform.localScale = prefab.transform.localScale; if (header != null) { header.text = Utilities.GetTranslation(objectName); } if (currentObject.gameObject.HasTag("FISH")) { modelParent.localEulerAngles = new Vector3(0f, -120f, 0f); Fish component = currentObject.gameObject.GetComponent(); if (component.mainMaterialId == 0) { component.rigRenderer.material = component.watchMaterial; } else { component.rigRenderer.materials[component.mainMaterialId] = component.watchMaterial; } } } else { equipmentObject = null; } } private void Update() { if (GlobalSettings.Instance.turnOnCheats) { if (Input.GetKeyDown(KeyCode.V)) { if (modelCamera.clearFlags == CameraClearFlags.Skybox) { modelCamera.clearFlags = CameraClearFlags.Color; modelCamera.GetComponent().enabled = false; } else { modelCamera.clearFlags = CameraClearFlags.Skybox; modelCamera.GetComponent().enabled = true; } } if (Input.GetKeyDown(KeyCode.R)) { rotateSmall = !rotateSmall; } if (Input.GetKeyDown(KeyCode.Mouse2)) { rotate = !rotate; } } if (isBigView) { if (VRManager.IsVROn()) { float axis = VRInputManager.GetAxis("LOOK_VERTICAL"); float axis2 = VRInputManager.GetAxis("LOOK_HORIZONTAL"); if (Mathf.Abs(axis2) > 0.1f && Mathf.Abs(axis) < 0.3f) { if (Time.deltaTime != 0f) { modelParentRigidbody.AddTorque(modelParent.up * axis2 * Time.deltaTime * inputRotateForce * 0.3f); } else { Vector3 localEulerAngles = modelParent.localEulerAngles + new Vector3(0f, axis2 * Time.unscaledDeltaTime * inputRotateSpeed, 0f); if (localEulerAngles.y > 360f) { localEulerAngles.y -= 360f; } else if (localEulerAngles.y < 0f) { localEulerAngles.y += 360f; } modelParent.localEulerAngles = localEulerAngles; } } else if (Mathf.Abs(axis) > 0.1f && Mathf.Abs(axis2) < 0.3f) { modelCamera.fieldOfView += zoomSpeed * (0f - axis) * Time.unscaledDeltaTime * 0.3f; modelCamera.fieldOfView = Mathf.Clamp(modelCamera.fieldOfView, 30f, 90f); } if (rotate && Mathf.Abs(axis2) < 0.1f && !currentObject.gameObject.HasTag("FISH")) { modelParent.localEulerAngles += modelParent.up * rotateSpeed * Time.unscaledDeltaTime; } } else { if (rotate && !Input.GetKey(KeyCode.Mouse1) && !currentObject.gameObject.HasTag("FISH")) { modelParent.localEulerAngles += modelParent.up * rotateSpeed * Time.unscaledDeltaTime; } if (Input.GetAxis("Mouse ScrollWheel") != 0f) { modelCamera.fieldOfView += zoomSpeed * ((!(Input.GetAxis("Mouse ScrollWheel") < 0f)) ? (-1f) : 1f) * Time.unscaledDeltaTime; modelCamera.fieldOfView = Mathf.Clamp(modelCamera.fieldOfView, 30f, 90f); } } } else if (rotateSmall && rotate && (bool)currentObject && !currentObject.gameObject.HasTag("FISH")) { modelParent.localEulerAngles += modelParent.up * rotateSpeed * Time.unscaledDeltaTime; } if (Time.deltaTime == 0f) { UpdateRotateInput(); } } private void FixedUpdate() { if (Time.deltaTime != 0f) { UpdateRotateInput(); } } public void UpdateRotateInput() { if (VRManager.IsVROn() || !isBigView || !Input.GetKey(KeyCode.Mouse1) || Input.GetAxis("Mouse X") == 0f) { return; } if (Time.deltaTime != 0f) { modelParentRigidbody.AddTorque(modelParent.up * (0f - Input.GetAxis("Mouse X")) * Time.deltaTime * inputRotateForce); return; } Vector3 localEulerAngles = modelParent.localEulerAngles + new Vector3(0f, (0f - Input.GetAxis("Mouse X")) * Time.unscaledDeltaTime * inputRotateSpeed, 0f); if (localEulerAngles.y > 360f) { localEulerAngles.y -= 360f; } else if (localEulerAngles.y < 0f) { localEulerAngles.y += 360f; } modelParent.localEulerAngles = localEulerAngles; } public void ChangeView(bool big) { if (!(currentObject == null)) { modelCamera.targetTexture = ((!big) ? renderTextureSmall : renderTextureBig); modelCamera.fieldOfView = 60f; modelParentRigidbody.velocity = Vector3.zero; modelParentRigidbody.angularVelocity = Vector3.zero; if (currentObject.gameObject.HasTag("FISH")) { modelParent.localEulerAngles = new Vector3(0f, -120f, 0f); } else { modelParent.localEulerAngles = new Vector3(0f, -90f, 0f); } isBigView = big; } } [Button] public void SavePng() { string pngOutPath = Application.dataPath + "/" + currentObject.name + ".png"; Utilities.RenderTextureToPng(renderTextureBig, pngOutPath); } }