using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class ModelExplorerController : Model3dController { // Start is called before the first frame update //public Model3dExplorerConfig explorerConfig = null; public float alphaLevel = 0.2f; public bool loseTextures = false; public GameObject infoPanel = null; private List selectableObjects = new List(); private List selectableNames = new List(); private List selectableHTML = new List(); private Material ghostMaterial; private Canvas rootCanvas; private GameObject selected; public Button selectedInfo; public GameObject Selected { get => selected; set { if (value != selected) { selectedInfo.onClick.RemoveAllListeners(); if (selected != null) selected.GetComponent().tween = false; if (value == null) { selectedInfo.gameObject.SetActive(false); selectedInfo.GetComponentInChildren().text = ""; } else if (value != null) { value.GetComponent().tween = true; string objectName; if (string.IsNullOrEmpty(selectableNames[selectableObjects.IndexOf(value)])) objectName = value.name; else objectName = selectableNames[selectableObjects.IndexOf(value)]; selectedInfo.gameObject.SetActive(true); selectedInfo.GetComponentInChildren().text = objectName; selectedInfo.onClick.AddListener(OpenHTML); } selected = value; } } } override protected void Start() { //config = explorerConfig; base.Start(); ConfigureCamera(navigationManager._camera.GetComponent()); rootCanvas = GetComponentInParent(); ghostMaterial = (Material)Resources.Load("Transparent Ghost", typeof(Material)); ConfigureSelectable3dObjects(); foreach (Selectable3dObject so in ((Model3dExplorerConfig)config).selectableParts) { selectableObjects.Add(so.object3d); selectableNames.Add(so.name); selectableHTML.Add(so.html); } foreach (Transform child in model3d.transform) { GameObject target = child.gameObject; if (!selectableObjects.Contains(target)) { SetTransparent(target); } else { AddHighlighter(target); AddTouchInteraction(target); } } } private void ConfigureCamera(Camera camera) { HighlightingSystem.HighlightingRenderer hr = exprivia.Utils.GetOrAddComponent(camera.gameObject); hr.iterations = 8; hr.downsampleFactor = 1; } private void ConfigureSelectable3dObjects() { for (int i = 0; i < ((Model3dExplorerConfig)config).selectableParts.Length; i++) ((Model3dExplorerConfig)config).selectableParts[i].object3d = model3d.transform.Find(((Model3dExplorerConfig)config).selectableParts[i].object3dName)?.gameObject; } // Update is called once per frame void Update() { if(Selected) { Color c = Selected.GetComponent().color; c.a = 0.5f + c.a / 2; selectedInfo.GetComponentsInChildren().ToList().ForEach((x) => x.color = c); } } public void OpenHTML() { infoPanel.gameObject.SetActive(true); string url = selectableHTML[selectableObjects.IndexOf(Selected)]; if (string.IsNullOrEmpty(url)) url = "html/" + Selected.name + ".html"; WebViewManager.Instance.Load(url, rootCanvas.gameObject, infoPanel); WebViewManager.Instance.SetWebViewVisibility(true); Debug.Log("Opening HTML: " + url); } private void AddTouchInteraction(GameObject target) { InteractablePart addModelTouchInteraction = target.AddComponent(); addModelTouchInteraction.controller = this; Rigidbody addRigidbody = target.AddComponent(); addRigidbody.isKinematic = true; addRigidbody.useGravity = false; foreach (MeshRenderer mr in target.GetComponentsInChildren()) { MeshCollider addMeshCollider = mr.gameObject.AddComponent(); addMeshCollider.convex = true; } } private void AddHighlighter(GameObject target) { target.AddComponent(); } private void SetTransparent(GameObject target) { SetRendererAlphas(target, alphaLevel); } private void SetRendererAlphas(GameObject target, float alpha) { Renderer[] mRenderers = target.GetComponentsInChildren(); for (int i = 0; i < mRenderers.Length; i++) { if (loseTextures) { Material[] mats = mRenderers[i].materials; for (int j = 0; j < mats.Length; j++) { mats[j] = ghostMaterial; StandardShaderUtils.ChangeRenderMode(mats[j], (alpha < 1) ? StandardShaderUtils.BlendMode.Fade : StandardShaderUtils.BlendMode.Cutout); Color matColor = mats[j].color; matColor.a = alpha; mats[j].color = matColor; } mRenderers[i].materials = mats; } else { for (int j = 0; j < mRenderers[i].materials.Length; j++) { if (loseTextures) { } StandardShaderUtils.ChangeRenderMode(mRenderers[i].materials[j], (alpha < 1) ? StandardShaderUtils.BlendMode.Fade : StandardShaderUtils.BlendMode.Cutout); Color matColor = mRenderers[i].materials[j].color; if (matColor != null) { matColor.a = alpha; mRenderers[i].materials[j].color = matColor; } } } } } public void HideInfoPanel() { infoPanel.gameObject.SetActive(false); WebViewManager.Instance.SetWebViewVisibility(false); } override protected void OnDestroy() { base.OnDestroy(); WebViewManager.Instance.DestroyWebView(); } }