diff options
| author | tylermurphy534 <tylermurphy534@gmail.com> | 2022-11-06 15:12:42 -0500 |
|---|---|---|
| committer | tylermurphy534 <tylermurphy534@gmail.com> | 2022-11-06 15:12:42 -0500 |
| commit | eb84bb298d2b95aec7b2ae12cbf25ac64f25379a (patch) | |
| tree | efd616a157df06ab661c6d56651853431ac6b08b /VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs | |
| download | unityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.tar.gz unityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.tar.bz2 unityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.zip | |
move to self host
Diffstat (limited to 'VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs')
| -rw-r--r-- | VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs b/VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs new file mode 100644 index 00000000..810442e5 --- /dev/null +++ b/VRCSDK3Worlds/Assets/MeshBaker/Examples/SwitchBakedObjectsTexture/MB_SwitchBakedObjectsTexture.cs @@ -0,0 +1,66 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MB_SwitchBakedObjectsTexture : MonoBehaviour { + // The target renderer where we will switch materials. + public MeshRenderer targetRenderer; + + // The list of materials to cycle through. + public Material[] materials; + + // The Mesh Baker that will do the baking + public MB3_MeshBaker meshBaker; + + public void OnGUI() + { + GUILayout.Label("Press space to switch the material on one of the cubes. " + + "This scene reuses the Texture Bake Result from the SceneBasic example."); + } + + public void Start() + { + // Bake the mesh. + meshBaker.AddDeleteGameObjects(meshBaker.GetObjectsToCombine().ToArray(),null,true); + meshBaker.Apply(); + } + + public void Update() + { + if (Input.GetKeyDown(KeyCode.Space)) + { + // Cycle the material on targetRenderer to the next material in materials. + + Material mat = targetRenderer.sharedMaterial; + //Find the index of the current material on the Renderer + int materialIdx = -1; + for (int i = 0; i < materials.Length; i++){ + if (materials[i] == mat){ + materialIdx = i; + } + } + + // Get the next material in the cycle. + materialIdx++; + if (materialIdx >= materials.Length) materialIdx = 0; + + if (materialIdx != -1) + { + // Assign the material to the disabled renderer + targetRenderer.sharedMaterial = materials[materialIdx]; + Debug.Log("Updating Material to: " + targetRenderer.sharedMaterial); + + // Update the Mesh Baker combined mesh + GameObject[] gameObjects = new GameObject[] { targetRenderer.gameObject }; + meshBaker.UpdateGameObjects(gameObjects, false, false, false, false, true, false, false, false, false); + + // We could have used AddDelteGameObjects instead of UpdateGameObjects. + // UpdateGameObjects is faster, but does not work if the material change causes + // the object to switch submeshes in the combined mesh. + // meshBaker.AddDeleteGameObjects(gameObjects, gameObjects,false); + // Apply the changes. + meshBaker.Apply(); + } + } + } +} |