protected virtual void ChangeToHighlightColor(Color color, float duration = 0f) { Renderer[] renderers = objectToAffect.GetComponentsInChildren <Renderer>(true); for (int j = 0; j < renderers.Length; j++) { Renderer renderer = renderers[j]; Material[] swapCustomMaterials = new Material[renderer.materials.Length]; for (int i = 0; i < renderer.materials.Length; i++) { Material material = renderer.materials[i]; if (customMaterial != null) { material = customMaterial; swapCustomMaterials[i] = material; } string faderRoutineID = material.GetInstanceID().ToString(); Coroutine existingFaderRoutine = VRTK_SharedMethods.GetDictionaryValue(faderRoutines, faderRoutineID); if (existingFaderRoutine != null) { StopCoroutine(existingFaderRoutine); faderRoutines.Remove(faderRoutineID); } material.EnableKeyword("_EMISSION"); if (resetMainTexture && material.HasProperty("_MainTex")) { renderer.material.SetTexture("_MainTex", new Texture2D(64, 64)); } if (material.HasProperty("_Color")) { if (duration > 0f) { VRTK_SharedMethods.AddDictionaryValue(faderRoutines, faderRoutineID, StartCoroutine(CycleColor(material, material.color, color, duration)), true); } else { material.color = color; if (material.HasProperty("_EmissionColor")) { material.SetColor("_EmissionColor", VRTK_SharedMethods.ColorDarken(color, emissionDarken)); } } } } if (customMaterial != null) { renderer.materials = swapCustomMaterials; } } }
protected virtual void StoreOriginalMaterials() { originalSharedRendererMaterials.Clear(); originalRendererMaterials.Clear(); Renderer[] renderers = objectToAffect.GetComponentsInChildren <Renderer>(true); for (int i = 0; i < renderers.Length; i++) { Renderer renderer = renderers[i]; string objectReference = renderer.gameObject.GetInstanceID().ToString(); VRTK_SharedMethods.AddDictionaryValue(originalSharedRendererMaterials, objectReference, renderer.sharedMaterials, true); VRTK_SharedMethods.AddDictionaryValue(originalRendererMaterials, objectReference, renderer.materials, true); renderer.sharedMaterials = VRTK_SharedMethods.GetDictionaryValue(originalSharedRendererMaterials, objectReference); } }
protected override void StoreOriginalMaterials() { originalMaterialPropertyBlocks.Clear(); highlightMaterialPropertyBlocks.Clear(); Renderer[] renderers = objectToAffect.GetComponentsInChildren <Renderer>(true); for (int i = 0; i < renderers.Length; i++) { Renderer renderer = renderers[i]; string objectReference = renderer.gameObject.GetInstanceID().ToString(); // get the initial material property block to restore the original material properties later on MaterialPropertyBlock originalPropertyBlock = new MaterialPropertyBlock(); renderer.GetPropertyBlock(originalPropertyBlock); VRTK_SharedMethods.AddDictionaryValue(originalMaterialPropertyBlocks, objectReference, originalPropertyBlock, true); // we need a second instance of the original material property block which will be modified for highlighting MaterialPropertyBlock highlightPropertyBlock = new MaterialPropertyBlock(); renderer.GetPropertyBlock(highlightPropertyBlock); VRTK_SharedMethods.AddDictionaryValue(highlightMaterialPropertyBlocks, objectReference, highlightPropertyBlock, true); } }
protected override void ChangeToHighlightColor(Color color, float duration = 0f) { Renderer[] renderers = objectToAffect.GetComponentsInChildren <Renderer>(true); for (int i = 0; i < renderers.Length; i++) { Renderer renderer = renderers[i]; string faderRoutineID = renderer.gameObject.GetInstanceID().ToString(); if (VRTK_SharedMethods.GetDictionaryValue(originalMaterialPropertyBlocks, faderRoutineID) == null) { continue; } Coroutine existingFaderRoutine = VRTK_SharedMethods.GetDictionaryValue(faderRoutines, faderRoutineID); if (existingFaderRoutine != null) { StopCoroutine(existingFaderRoutine); faderRoutines.Remove(faderRoutineID); } MaterialPropertyBlock highlightMaterialPropertyBlock = highlightMaterialPropertyBlocks[faderRoutineID]; renderer.GetPropertyBlock(highlightMaterialPropertyBlock); if (resetMainTexture) { highlightMaterialPropertyBlock.SetTexture("_MainTex", Texture2D.whiteTexture); } if (duration > 0f) { VRTK_SharedMethods.AddDictionaryValue(faderRoutines, faderRoutineID, StartCoroutine(CycleColor(renderer, highlightMaterialPropertyBlock, color, duration)), true); } else { highlightMaterialPropertyBlock.SetColor("_Color", color); highlightMaterialPropertyBlock.SetColor("_EmissionColor", VRTK_SharedMethods.ColorDarken(color, emissionDarken)); renderer.SetPropertyBlock(highlightMaterialPropertyBlock); } } }