private void SetModel(string renderModelName) { // Strip mesh filter and renderer as these will get re-added if needed. var meshRenderer = GetComponent <MeshRenderer>(); if (meshRenderer != null) { Object.DestroyImmediate(meshRenderer); } var meshFilter = GetComponent <MeshFilter>(); if (meshFilter != null) { Object.DestroyImmediate(meshFilter); } using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { if (LoadComponents(holder, renderModelName)) { this.renderModelName = renderModelName; return; } Debug.Log("Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) { return; } if (verbose) { Debug.Log("Loading render model " + renderModelName); } model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) { return; } models[renderModelName] = model; this.renderModelName = renderModelName; } gameObject.AddComponent <MeshFilter>().mesh = model.mesh; gameObject.AddComponent <MeshRenderer>().sharedMaterial = model.material; } } }
private IEnumerator FreeRenderModel(IntPtr pRenderModel) { yield return(new WaitForSeconds(1.0f)); using (var holder = new RenderModelInterfaceHolder()) { var renderModels = holder.instance; renderModels.FreeRenderModel(pRenderModel); } }
private bool SetModel(string renderModelName) { StripMesh(gameObject); using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { if (LoadComponents(holder, renderModelName)) { UpdateComponents(); return(true); } Debug.Log("[" + gameObject.name + "] Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) { return(false); } if (verbose) { Debug.Log("Loading render model " + renderModelName); } model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) { return(false); } models[renderModelName] = model; } if (meshOverride != null) { gameObject.AddComponent <MeshFilter>().mesh = meshOverride; } else { gameObject.AddComponent <MeshFilter>().mesh = model.mesh; } gameObject.AddComponent <MeshRenderer>().sharedMaterial = model.material; return(true); } } return(false); }
private bool SetModel(string renderModelName) { StripMesh(gameObject); using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { componentAttachPoints.Clear(); if (LoadComponents(holder, renderModelName)) { UpdateComponents(holder.instance); return(true); } Debug.Log("<b>[SteamVR]</b> [" + gameObject.name + "] Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) { return(false); } if (verbose) { Debug.Log("<b>[SteamVR]</b> Loading render model " + renderModelName); } model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) { return(false); } models[renderModelName] = model; } Debug.Log("Adding mesh and stuff"); gameObject.AddComponent <MeshFilter>().mesh = model.mesh; MeshRenderer newRenderer = gameObject.AddComponent <MeshRenderer>(); newRenderer.sharedMaterial = model.material; meshRenderers.Add(newRenderer); return(true); } } return(false); }
public void UpdateComponents() { var t = transform; if (t.childCount == 0) { return; } using (var holder = new RenderModelInterfaceHolder()) { var controllerState = (index != SteamVR_TrackedObject.EIndex.None) ? SteamVR_Controller.Input((int)index).GetState() : new VRControllerState_t(); for (int i = 0; i < t.childCount; i++) { var child = t.GetChild(i); var renderModels = holder.instance; if (renderModels == null) { break; } var componentState = new RenderModel_ComponentState_t(); var componentModeState = new RenderModel_ControllerMode_State_t(); if (!renderModels.GetComponentState(renderModelName, child.name, ref controllerState, ref componentModeState, ref componentState)) { continue; } var componentTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentRenderModel); child.localPosition = componentTransform.pos; child.localRotation = componentTransform.rot; var attach = child.FindChild(k_localTransformName); if (attach != null) { var attachTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentLocal); attach.position = t.TransformPoint(attachTransform.pos); attach.rotation = t.rotation * attachTransform.rot; } bool visible = (componentState.uProperties & (uint)EVRComponentProperty.IsVisible) != 0; if (visible != child.gameObject.activeSelf) { child.gameObject.SetActive(visible); } } } }
IEnumerator FreeRenderModel(System.IntPtr pRenderModel) { // TODO: PaulBro - Why 1 second? Why not wait for end of frame? yield return(new WaitForSeconds(1.0f)); using (var holder = new RenderModelInterfaceHolder()) { var renderModels = holder.instance; renderModels.FreeRenderModel(pRenderModel); } // TODO: PaulBro - prevent redundant simultaneous Coroutines this._FreeRenderModelCoroutine = null; }
private bool LoadComponents(RenderModelInterfaceHolder holder, string renderModelName) { // Disable existing components (we will re-enable them if referenced by this new model). // Also strip mesh filter and renderer since these will get re-added if the new component needs them. var t = transform; for (int i = 0; i < t.childCount; i++) { var child = t.GetChild(i); child.gameObject.SetActive(false); var meshRenderer = child.GetComponent<MeshRenderer>(); if (meshRenderer != null) Object.DestroyImmediate(meshRenderer); var meshFilter = child.GetComponent<MeshFilter>(); if (meshFilter != null) Object.DestroyImmediate(meshFilter); } // If no model specified, we're done; return success. if (string.IsNullOrEmpty(renderModelName)) return true; var renderModels = holder.instance; if (renderModels == null) return false; var count = renderModels.GetComponentCount(renderModelName); if (count == 0) return false; for (int i = 0; i < count; i++) { var capacity = renderModels.GetComponentName(renderModelName, (uint)i, null, 0); if (capacity == 0) continue; var componentName = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentName(renderModelName, (uint)i, componentName, capacity) == 0) continue; // Create (or reuse) a child object for this component (some components are dynamic and don't have meshes). t = FindComponent(componentName.ToString()); if (t != null) { t.gameObject.SetActive(true); } else { t = new GameObject(componentName.ToString()).transform; t.parent = transform; // Also create a child 'attach' object for attaching things. var attach = new GameObject(k_localTransformName).transform; attach.parent = t; attach.localPosition = Vector3.zero; attach.localRotation = Quaternion.identity; attach.localScale = Vector3.one; } // Reset transform. t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; capacity = renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), null, 0); if (capacity == 0) continue; var componentRenderModelName = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), componentRenderModelName, capacity) == 0) continue; // Check the cache or load into memory. var model = models[componentRenderModelName] as RenderModel; if (model == null || model.mesh == null) { if (verbose) Debug.Log("Loading render model " + componentRenderModelName); model = LoadRenderModel(renderModels, componentRenderModelName.ToString(), renderModelName); if (model == null) continue; models[componentRenderModelName] = model; } t.gameObject.AddComponent<MeshFilter>().mesh = model.mesh; t.gameObject.AddComponent<MeshRenderer>().sharedMaterial = model.material; } return true; }
IEnumerator SetModelAsync(string renderModelName) { if (string.IsNullOrEmpty(renderModelName)) { yield break; } // Preload all render models before asking for the data to create meshes. using (var holder = new RenderModelInterfaceHolder()) { var renderModels = holder.instance; if (renderModels == null) { yield break; } // Gather names of render models to preload. string[] renderModelNames; var count = renderModels.GetComponentCount(renderModelName); if (count > 0) { renderModelNames = new string[count]; for (int i = 0; i < count; i++) { var capacity = renderModels.GetComponentName(renderModelName, (uint)i, null, 0); if (capacity == 0) { continue; } var componentName = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentName(renderModelName, (uint)i, componentName, capacity) == 0) { continue; } capacity = renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), null, 0); if (capacity == 0) { continue; } var name = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), name, capacity) == 0) { continue; } var s = name.ToString(); // Only need to preload if not already cached. var model = models[s] as RenderModel; if (model == null || model.mesh == null) { renderModelNames[i] = s; } } } else { // Only need to preload if not already cached. var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { renderModelNames = new string[] { renderModelName }; } else { renderModelNames = new string[0]; } } // Keep trying every 100ms until all components finish loading. while (true) { var loading = false; foreach (var name in renderModelNames) { if (string.IsNullOrEmpty(name)) { continue; } var pRenderModel = System.IntPtr.Zero; var error = renderModels.LoadRenderModel_Async(name, ref pRenderModel); if (error == EVRRenderModelError.Loading) { loading = true; } else if (error == EVRRenderModelError.None) { // Preload textures as well. var renderModel = (RenderModel_t)Marshal.PtrToStructure(pRenderModel, typeof(RenderModel_t)); // Check the cache first. var material = materials[renderModel.diffuseTextureId] as Material; if (material == null || material.mainTexture == null) { var pDiffuseTexture = System.IntPtr.Zero; error = renderModels.LoadTexture_Async(renderModel.diffuseTextureId, ref pDiffuseTexture); if (error == EVRRenderModelError.Loading) { loading = true; } } } } if (loading) { yield return(new WaitForSeconds(0.1f)); } else { break; } } } bool success = SetModel(renderModelName); SteamVR_Events.RenderModelLoaded.Send(this, success); }
} // 0x0000000180CCB420-0x0000000180CCB540 private bool LoadComponents(RenderModelInterfaceHolder holder, string renderModelName) => default; // 0x0000000180CC9350-0x0000000180CC9AF0
public void UpdateComponents() { var t = transform; if (t.childCount == 0) return; using (var holder = new RenderModelInterfaceHolder()) { var controllerState = (index != SteamVR_TrackedObject.EIndex.None) ? SteamVR_Controller.Input((int)index).GetState() : new VRControllerState_t(); for (int i = 0; i < t.childCount; i++) { var child = t.GetChild(i); var renderModels = holder.instance; if (renderModels == null) break; var componentState = new RenderModel_ComponentState_t(); if (!renderModels.GetComponentState(renderModelName, child.name, ref controllerState, ref controllerModeState, ref componentState)) continue; var componentTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentRenderModel); child.localPosition = componentTransform.pos; child.localRotation = componentTransform.rot; var attach = child.FindChild(k_localTransformName); if (attach != null) { var attachTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentLocal); attach.position = t.TransformPoint(attachTransform.pos); attach.rotation = t.rotation * attachTransform.rot; } bool visible = (componentState.uProperties & (uint)EVRComponentProperty.IsVisible) != 0; if (visible != child.gameObject.activeSelf) { child.gameObject.SetActive(visible); } } } }
IEnumerator SetModelAsync(string newRenderModelName) { meshRenderers.Clear(); if (string.IsNullOrEmpty(newRenderModelName)) { yield break; } // Preload all render models before asking for the data to create meshes. using (RenderModelInterfaceHolder holder = new RenderModelInterfaceHolder()) { CVRRenderModels renderModels = holder.instance; if (renderModels == null) { yield break; } // Gather names of render models to preload. string[] renderModelNames; uint count = renderModels.GetComponentCount(newRenderModelName); if (count > 0) { renderModelNames = new string[count]; for (int componentIndex = 0; componentIndex < count; componentIndex++) { uint capacity = renderModels.GetComponentName(newRenderModelName, (uint)componentIndex, null, 0); if (capacity == 0) { continue; } var componentNameStringBuilder = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentName(newRenderModelName, (uint)componentIndex, componentNameStringBuilder, capacity) == 0) { continue; } string componentName = componentNameStringBuilder.ToString(); capacity = renderModels.GetComponentRenderModelName(newRenderModelName, componentName, null, 0); if (capacity == 0) { continue; } var nameStringBuilder = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentRenderModelName(newRenderModelName, componentName, nameStringBuilder, capacity) == 0) { continue; } var s = nameStringBuilder.ToString(); // Only need to preload if not already cached. RenderModel model = models[s] as RenderModel; if (model == null || model.mesh == null) { renderModelNames[componentIndex] = s; } } } else { // Only need to preload if not already cached. RenderModel model = models[newRenderModelName] as RenderModel; if (model == null || model.mesh == null) { renderModelNames = new string[] { newRenderModelName }; } else { renderModelNames = new string[0]; } } // Keep trying every 100ms until all components finish loading. while (true) { var loading = false; for (int renderModelNameIndex = 0; renderModelNameIndex < renderModelNames.Length; renderModelNameIndex++) { if (string.IsNullOrEmpty(renderModelNames[renderModelNameIndex])) { continue; } var pRenderModel = System.IntPtr.Zero; var error = renderModels.LoadRenderModel_Async(renderModelNames[renderModelNameIndex], ref pRenderModel); //Debug.Log("<b>[SteamVR]</b> renderModels.LoadRenderModel_Async(" + renderModelNames[renderModelNameIndex] + ": " + error.ToString()); if (error == EVRRenderModelError.Loading) { loading = true; } else if (error == EVRRenderModelError.None) { // Preload textures as well. var renderModel = MarshalRenderModel(pRenderModel); // Check the cache first. var material = materials[renderModel.diffuseTextureId] as Material; if (material == null || material.mainTexture == null) { var pDiffuseTexture = System.IntPtr.Zero; error = renderModels.LoadTexture_Async(renderModel.diffuseTextureId, ref pDiffuseTexture); //Debug.Log("<b>[SteamVR]</b> renderModels.LoadRenderModel_Async(" + renderModelNames[renderModelNameIndex] + ": " + error.ToString()); if (error == EVRRenderModelError.Loading) { loading = true; } } } } if (loading) { yield return(new WaitForSecondsRealtime(0.1f)); } else { break; } } } bool success = SetModel(newRenderModelName); this.renderModelName = newRenderModelName; SteamVR_Events.RenderModelLoaded.Send(this, success); }
IEnumerator SetModelAsync(string renderModelName) { if (string.IsNullOrEmpty(renderModelName)) yield break; // Preload all render models before asking for the data to create meshes. using (var holder = new RenderModelInterfaceHolder()) { var renderModels = holder.instance; if (renderModels == null) yield break; // Gather names of render models to preload. string[] renderModelNames; var count = renderModels.GetComponentCount(renderModelName); if (count > 0) { renderModelNames = new string[count]; for (int i = 0; i < count; i++) { var capacity = renderModels.GetComponentName(renderModelName, (uint)i, null, 0); if (capacity == 0) continue; var componentName = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentName(renderModelName, (uint)i, componentName, capacity) == 0) continue; capacity = renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), null, 0); if (capacity == 0) continue; var name = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentRenderModelName(renderModelName, componentName.ToString(), name, capacity) == 0) continue; var s = name.ToString(); // Only need to preload if not already cached. var model = models[s] as RenderModel; if (model == null || model.mesh == null) { renderModelNames[i] = s; } } } else { // Only need to preload if not already cached. var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { renderModelNames = new string[] { renderModelName }; } else { renderModelNames = new string[0]; } } // Keep trying every 100ms until all components finish loading. while (true) { var loading = false; foreach (var name in renderModelNames) { if (string.IsNullOrEmpty(name)) continue; var pRenderModel = System.IntPtr.Zero; var error = renderModels.LoadRenderModel_Async(name, ref pRenderModel); if (error == EVRRenderModelError.Loading) { loading = true; } else if (error == EVRRenderModelError.None) { // Preload textures as well. var renderModel = (RenderModel_t)Marshal.PtrToStructure(pRenderModel, typeof(RenderModel_t)); // Check the cache first. var material = materials[renderModel.diffuseTextureId] as Material; if (material == null || material.mainTexture == null) { var pDiffuseTexture = System.IntPtr.Zero; error = renderModels.LoadTexture_Async(renderModel.diffuseTextureId, ref pDiffuseTexture); if (error == EVRRenderModelError.Loading) { loading = true; } } } } if (loading) { yield return new WaitForSeconds(0.1f); } else { break; } } } bool success = SetModel(renderModelName); SteamVR_Utils.Event.Send("render_model_loaded", this, success); }
void Update() { #if UNITY_EDITOR if (!Application.isPlaying) { // See if anything has changed since this gets called whenever anything gets touched. var fields = GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); bool modified = false; if (values == null) { modified = true; } else { foreach (var f in fields) { if (!values.Contains(f) || !f.GetValue(this).Equals(values[f])) { modified = true; break; } } } if (modified) { SetModel(modelOverride); values = new Hashtable(); foreach (var f in fields) { values[f] = f.GetValue(this); } } return; // Do not update transforms (below) when not playing in Editor (to avoid keeping OpenVR running all the time). } #endif // Update component transforms dynamically. if (updateDynamically) { using (var holder = new RenderModelInterfaceHolder()) { var controllerState = SteamVR_Controller.Input((int)index).GetState(); var t = transform; var baseTransform = new SteamVR_Utils.RigidTransform(t); for (int i = 0; i < t.childCount; i++) { var child = t.GetChild(i); var renderModels = holder.instance; if (renderModels == null) { break; } var componentState = new RenderModel_ComponentState_t(); if (!renderModels.GetComponentState(renderModelName, child.name, ref controllerState, ref componentState)) { continue; } var componentTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentRenderModel); child.localPosition = componentTransform.pos; child.localRotation = componentTransform.rot; var attach = child.FindChild(k_localTransformName); if (attach != null) { var attachTransform = baseTransform * new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentLocal); attach.position = attachTransform.pos; attach.rotation = attachTransform.rot; } bool visible = (componentState.uProperties & (uint)EVRComponentProperty.IsVisible) != 0; if (visible != child.gameObject.activeSelf) { child.gameObject.SetActive(visible); } } } } }
private bool SetModel(string renderModelName) { StripMesh(gameObject); using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { // BUGBUG: PaulBro - workaround for Error: Unity SteamVR: Failed to load IVRRenderModels interface version IVRRenderModels_005 // See https://steamcommunity.com/app/358720/discussions/0/357286663674402763/ // Note: Don't touchh RenderModelInterfaceHolder.instance unless steam is active, else it will throw an exception on null IVRRenderModels_005 //if (LoadComponents(holder, renderModelName)) //{ // UpdateComponents(holder.instance); // return true; //} CVRRenderModels renderModels = null; if (SteamVR.active) { renderModels = holder.instance; } if (LoadComponents(renderModels, renderModelName)) { UpdateComponents(renderModels); return(true); } Debug.Log("[" + gameObject.name + "] Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) { return(false); } if (verbose) { Debug.Log("Loading render model " + renderModelName); } model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) { return(false); } models[renderModelName] = model; } gameObject.AddComponent <MeshFilter>().mesh = model.mesh; gameObject.AddComponent <MeshRenderer>().sharedMaterial = model.material; return(true); } } return(false); }
private void SetModel(string renderModelName) { // Strip mesh filter and renderer as these will get re-added if needed. var meshRenderer = GetComponent<MeshRenderer>(); if (meshRenderer != null) Object.DestroyImmediate(meshRenderer); var meshFilter = GetComponent<MeshFilter>(); if (meshFilter != null) Object.DestroyImmediate(meshFilter); using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { if (LoadComponents(holder, renderModelName)) { this.renderModelName = renderModelName; return; } Debug.Log("Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) return; if (verbose) Debug.Log("Loading render model " + renderModelName); model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) return; models[renderModelName] = model; this.renderModelName = renderModelName; } gameObject.AddComponent<MeshFilter>().mesh = model.mesh; gameObject.AddComponent<MeshRenderer>().sharedMaterial = model.material; } } }
void Update() { #if UNITY_EDITOR if (!Application.isPlaying) { // See if anything has changed since this gets called whenever anything gets touched. var fields = GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); bool modified = false; if (values == null) { modified = true; } else { foreach (var f in fields) { if (!values.Contains(f) || !f.GetValue(this).Equals(values[f])) { modified = true; break; } } } if (modified) { SetModel(modelOverride); values = new Hashtable(); foreach (var f in fields) values[f] = f.GetValue(this); } return; // Do not update transforms (below) when not playing in Editor (to avoid keeping OpenVR running all the time). } #endif // Update component transforms dynamically. if (updateDynamically) { using (var holder = new RenderModelInterfaceHolder()) { var controllerState = SteamVR_Controller.Input((int)index).GetState(); var t = transform; var baseTransform = new SteamVR_Utils.RigidTransform(t); for (int i = 0; i < t.childCount; i++) { var child = t.GetChild(i); var renderModels = holder.instance; if (renderModels == null) break; var componentState = new RenderModel_ComponentState_t(); if (!renderModels.GetComponentState(renderModelName, child.name, ref controllerState, ref componentState)) continue; var componentTransform = new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentRenderModel); child.localPosition = componentTransform.pos; child.localRotation = componentTransform.rot; var attach = child.FindChild(k_localTransformName); if (attach != null) { var attachTransform = baseTransform * new SteamVR_Utils.RigidTransform(componentState.mTrackingToComponentLocal); attach.position = attachTransform.pos; attach.rotation = attachTransform.rot; } bool visible = (componentState.uProperties & (uint)EVRComponentProperty.VRComponentProperty_IsVisible) != 0; if (visible != child.gameObject.activeSelf) { child.gameObject.SetActive(visible); } } } } }
private bool SetModel(string renderModelName) { StripMesh(gameObject); using (var holder = new RenderModelInterfaceHolder()) { if (createComponents) { if (LoadComponents(holder, renderModelName)) { UpdateComponents(); return true; } Debug.Log("[" + gameObject.name + "] Render model does not support components, falling back to single mesh."); } if (!string.IsNullOrEmpty(renderModelName)) { var model = models[renderModelName] as RenderModel; if (model == null || model.mesh == null) { var renderModels = holder.instance; if (renderModels == null) return false; if (verbose) Debug.Log("Loading render model " + renderModelName); model = LoadRenderModel(renderModels, renderModelName, renderModelName); if (model == null) return false; models[renderModelName] = model; } gameObject.AddComponent<MeshFilter>().mesh = model.mesh; gameObject.AddComponent<MeshRenderer>().sharedMaterial = model.material; return true; } } return false; }
private bool LoadComponents(RenderModelInterfaceHolder holder, string renderModelName) { // Disable existing components (we will re-enable them if referenced by this new model). // Also strip mesh filter and renderer since these will get re-added if the new component needs them. var t = transform; for (int childIndex = 0; childIndex < t.childCount; childIndex++) { var child = t.GetChild(childIndex); child.gameObject.SetActive(false); StripMesh(child.gameObject); } // If no model specified, we're done; return success. if (string.IsNullOrEmpty(renderModelName)) { return(true); } var renderModels = holder.instance; if (renderModels == null) { return(false); } var count = renderModels.GetComponentCount(renderModelName); if (count == 0) { return(false); } for (int i = 0; i < count; i++) { var capacity = renderModels.GetComponentName(renderModelName, (uint)i, null, 0); if (capacity == 0) { continue; } System.Text.StringBuilder componentNameStringBuilder = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentName(renderModelName, (uint)i, componentNameStringBuilder, capacity) == 0) { continue; } string componentName = componentNameStringBuilder.ToString(); // Create (or reuse) a child object for this component (some components are dynamic and don't have meshes). t = FindTransformByName(componentName); if (t != null) { t.gameObject.SetActive(true); componentAttachPoints[componentName] = FindTransformByName(k_localTransformName, t); } else { t = new GameObject(componentName).transform; t.parent = transform; t.gameObject.layer = gameObject.layer; // Also create a child 'attach' object for attaching things. var attach = new GameObject(k_localTransformName).transform; attach.parent = t; attach.localPosition = Vector3.zero; attach.localRotation = Quaternion.identity; attach.localScale = Vector3.one; attach.gameObject.layer = gameObject.layer; componentAttachPoints[componentName] = attach; } // Reset transform. t.localPosition = Vector3.zero; t.localRotation = Quaternion.identity; t.localScale = Vector3.one; capacity = renderModels.GetComponentRenderModelName(renderModelName, componentName, null, 0); if (capacity == 0) { continue; } var componentRenderModelNameStringBuilder = new System.Text.StringBuilder((int)capacity); if (renderModels.GetComponentRenderModelName(renderModelName, componentName, componentRenderModelNameStringBuilder, capacity) == 0) { continue; } string componentRenderModelName = componentRenderModelNameStringBuilder.ToString(); // Check the cache or load into memory. var model = models[componentRenderModelName] as RenderModel; if (model == null || model.mesh == null) { if (verbose) { Debug.Log("<b>[SteamVR]</b> Loading render model " + componentRenderModelName); } model = LoadRenderModel(renderModels, componentRenderModelName, renderModelName); if (model == null) { continue; } models[componentRenderModelName] = model; } t.gameObject.AddComponent <MeshFilter>().mesh = model.mesh; MeshRenderer newRenderer = t.gameObject.AddComponent <MeshRenderer>(); newRenderer.sharedMaterial = model.material; meshRenderers.Add(newRenderer); } return(true); }
IEnumerator FreeRenderModel(System.IntPtr pRenderModel) { yield return new WaitForSeconds(1.0f); using (var holder = new RenderModelInterfaceHolder()) { var renderModels = holder.instance; renderModels.FreeRenderModel(pRenderModel); } }