summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs
diff options
context:
space:
mode:
authortylermurphy534 <tylermurphy534@gmail.com>2022-11-06 15:12:42 -0500
committertylermurphy534 <tylermurphy534@gmail.com>2022-11-06 15:12:42 -0500
commiteb84bb298d2b95aec7b2ae12cbf25ac64f25379a (patch)
treeefd616a157df06ab661c6d56651853431ac6b08b /VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs
downloadunityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.tar.gz
unityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.tar.bz2
unityprojects-eb84bb298d2b95aec7b2ae12cbf25ac64f25379a.zip
move to self host
Diffstat (limited to 'VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs')
-rw-r--r--VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs116
1 files changed, 116 insertions, 0 deletions
diff --git a/VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs b/VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs
new file mode 100644
index 00000000..78e2af5e
--- /dev/null
+++ b/VRCSDK3Worlds/Assets/MeshBaker/Examples/SkinnedMeshRenderer/MB_SkinnedMeshSceneController.cs
@@ -0,0 +1,116 @@
+using UnityEngine;
+using System.Collections;
+using System.Collections.Generic;
+
+public class MB_SkinnedMeshSceneController : MonoBehaviour {
+ public GameObject swordPrefab;
+ public GameObject hatPrefab;
+ public GameObject glassesPrefab;
+ public GameObject workerPrefab;
+
+ public GameObject targetCharacter;
+
+ public MB3_MeshBaker skinnedMeshBaker;
+
+ GameObject swordInstance;
+ GameObject glassesInstance;
+ GameObject hatInstance;
+
+ void Start () {
+ //To demonstrate lets add a character to the combined mesh
+ GameObject worker1 = (GameObject) Instantiate(workerPrefab);
+ worker1.transform.position = new Vector3(1.31f, 0.985f, -0.25f);
+ Animation anim = worker1.GetComponent<Animation>();
+ anim.wrapMode = WrapMode.Loop;
+ //IMPORTANT set the culling type to something other than renderer. Animations may not play
+ //if animation.cullingType is left on BasedOnRenderers. This appears to be a bug in Unity
+ //the animation gets confused about the bounds if the skinnedMeshRenderer is changed
+ anim.cullingType = AnimationCullingType.AlwaysAnimate; //IMPORTANT
+ anim.Play("run");
+
+ //create an array with everything we want to add
+ //It is important to add the gameObject with the Renderer/mesh attached
+ GameObject[] objsToAdd = new GameObject[1] {worker1.GetComponentInChildren<SkinnedMeshRenderer>().gameObject};
+
+ //add the objects. This will disable the renderers on the source objects
+ skinnedMeshBaker.AddDeleteGameObjects(objsToAdd, null, true);
+ //apply the changes to the mesh
+ skinnedMeshBaker.Apply();
+ }
+
+ void OnGUI () {
+ if (GUILayout.Button ("Add/Remove Sword")) {
+ if (swordInstance == null){
+ Transform hand = SearchHierarchyForBone(targetCharacter.transform,"RightHandAttachPoint");
+ swordInstance = (GameObject) Instantiate(swordPrefab);
+ swordInstance.transform.parent = hand;
+ swordInstance.transform.localPosition = Vector3.zero;
+ swordInstance.transform.localRotation = Quaternion.identity;
+ swordInstance.transform.localScale = Vector3.one;
+ GameObject[] objsToAdd = new GameObject[1] {swordInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
+ skinnedMeshBaker.Apply();
+ } else if (skinnedMeshBaker.CombinedMeshContains(swordInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
+ GameObject[] objsToDelete = new GameObject[1] {swordInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
+ skinnedMeshBaker.Apply();
+ Destroy(swordInstance);
+ swordInstance = null;
+ }
+ }
+ if (GUILayout.Button ("Add/Remove Hat")) {
+ if (hatInstance == null){
+ Transform hand = SearchHierarchyForBone(targetCharacter.transform,"HeadAttachPoint");
+ hatInstance = (GameObject) Instantiate(hatPrefab);
+ hatInstance.transform.parent = hand;
+ hatInstance.transform.localPosition = Vector3.zero;
+ hatInstance.transform.localRotation = Quaternion.identity;
+ hatInstance.transform.localScale = Vector3.one;
+ GameObject[] objsToAdd = new GameObject[1] {hatInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
+ skinnedMeshBaker.Apply();
+ } else if (skinnedMeshBaker.CombinedMeshContains(hatInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
+ GameObject[] objsToDelete = new GameObject[1] {hatInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
+ skinnedMeshBaker.Apply();
+ Destroy(hatInstance);
+ hatInstance = null;
+ }
+ }
+ if (GUILayout.Button ("Add/Remove Glasses")) {
+ if (glassesInstance == null){
+ Transform hand = SearchHierarchyForBone(targetCharacter.transform,"NoseAttachPoint");
+ glassesInstance = (GameObject) Instantiate(glassesPrefab);
+ glassesInstance.transform.parent = hand;
+ glassesInstance.transform.localPosition = Vector3.zero;
+ glassesInstance.transform.localRotation = Quaternion.identity;
+ glassesInstance.transform.localScale = Vector3.one;
+ GameObject[] objsToAdd = new GameObject[1] {glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(objsToAdd,null, true);
+ skinnedMeshBaker.Apply();
+ } else if (skinnedMeshBaker.CombinedMeshContains(glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject)) {
+ GameObject[] objsToDelete = new GameObject[1] {glassesInstance.GetComponentInChildren<MeshRenderer>().gameObject};
+ skinnedMeshBaker.AddDeleteGameObjects(null,objsToDelete, true);
+ skinnedMeshBaker.Apply();
+ Destroy(glassesInstance);
+ glassesInstance = null;
+ }
+ }
+ }
+
+
+ public Transform SearchHierarchyForBone(Transform current, string name)
+ {
+ if (current.name.Equals( name ))
+ return current;
+
+ for (int i = 0; i < current.childCount; ++i)
+ {
+ Transform found = SearchHierarchyForBone(current.GetChild(i), name);
+
+ if (found != null)
+ return found;
+ }
+ return null;
+ }
+}