summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Examples/CharacterCustomization/MB_SwapShirts.cs
blob: e01199d8607390936aa211051b9c6eeaf4333442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;


public class MB_SwapShirts : MonoBehaviour {
    public MB3_MeshBaker meshBaker;

    public Renderer[] clothingAndBodyPartsBareTorso;
    public Renderer[] clothingAndBodyPartsBareTorsoDamagedArm;
    public Renderer[] clothingAndBodyPartsHoodie;

	void Start(){
		//initial bake
		GameObject[] objs = new GameObject[ clothingAndBodyPartsBareTorso.Length ];
		for (int i = 0; i < clothingAndBodyPartsBareTorso.Length; i++) {
			objs[i] = clothingAndBodyPartsBareTorso[i].gameObject;
		}		
		meshBaker.ClearMesh ();
		meshBaker.AddDeleteGameObjects (objs,null,true);
		meshBaker.Apply ();
	}

    // Update is called once per frame
    void OnGUI () {
        if (GUILayout.Button("Wear Hoodie"))
        {
            ChangeOutfit(clothingAndBodyPartsHoodie);
        }
        if (GUILayout.Button("Bare Torso"))
        {
            ChangeOutfit(clothingAndBodyPartsBareTorso);
        }
        if (GUILayout.Button("Damaged Arm"))
        {
            ChangeOutfit(clothingAndBodyPartsBareTorsoDamagedArm);
        }
    }

    void ChangeOutfit(Renderer[] outfit)
    {
        //collect the meshes we will be removing
        List<GameObject> objectsWeAreRemoving = new List<GameObject>();
        foreach (GameObject item in meshBaker.meshCombiner.GetObjectsInCombined())
        {
            Renderer r = item.GetComponent<Renderer>();
            bool foundInOutfit = false;
            for (int i = 0; i < outfit.Length; i++)
            {
                if (r == outfit[i])
                {
                    foundInOutfit = true;
                    break;
                }
            }
            if (!foundInOutfit)
            {
                objectsWeAreRemoving.Add(r.gameObject);
                Debug.Log("Removing " + r.gameObject);
            }
        }

        //Now collect the meshes we will be adding
        List<GameObject> objectsWeAreAdding = new List<GameObject>();
        for (int i = 0; i < outfit.Length; i++)
        {
            if (!meshBaker.meshCombiner.GetObjectsInCombined().Contains(outfit[i].gameObject))
            {
                objectsWeAreAdding.Add(outfit[i].gameObject);
                Debug.Log("Adding " + outfit[i].gameObject);
            }
        }

        meshBaker.AddDeleteGameObjects(objectsWeAreAdding.ToArray(), objectsWeAreRemoving.ToArray(),true);
        meshBaker.Apply();
    }
}