summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Examples/BakeTexturesAtRuntime/BakeTexturesAtRuntime.cs
blob: ec89ecf2d149acaa64aabae47c5cf6b9ab207fed (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
79
80
81
using UnityEngine;
using System.Collections;
using DigitalOpus.MB.Core;

/*
 * For building atlases at runtime it is very important that:
 * 		- textures be in trucolor/RBGA32 format
 * 		- textures have read flag set
 * 
 * 
 * It is also Highly recommended to avoid resizing
 *      - build padding into textures in editor
 *      - don't use padding when creating atlases
 *      - don't use tiled materials
 * 
 * If you are having problems look at the Debug Log on the device
 */
public class BakeTexturesAtRuntime : MonoBehaviour {
	public GameObject target;
	float elapsedTime = 0;
	MB3_TextureCombiner.CreateAtlasesCoroutineResult result = new MB3_TextureCombiner.CreateAtlasesCoroutineResult();
	
	void OnGUI(){
		GUILayout.Label("Time to bake textures: " + elapsedTime);
		if (GUILayout.Button("Combine textures & build combined mesh all at once")){
			MB3_MeshBaker meshbaker = target.GetComponentInChildren<MB3_MeshBaker>();
			MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();
			
			//These can be assets configured at runtime or you can create them
			// on the fly like this
			textureBaker.textureBakeResults = ScriptableObject.CreateInstance<MB2_TextureBakeResults>();
			textureBaker.resultMaterial = new Material( Shader.Find("Diffuse") ); 
			
			float t1 = Time.realtimeSinceStartup;
			textureBaker.CreateAtlases();
			elapsedTime = Time.realtimeSinceStartup - t1;	
			
			meshbaker.ClearMesh(); //only necessary if your not sure whats in the combined mesh
			meshbaker.textureBakeResults = textureBaker.textureBakeResults;
			//Add the objects to the combined mesh
			meshbaker.AddDeleteGameObjects(textureBaker.GetObjectsToCombine().ToArray(), null, true);
			
			meshbaker.Apply();
		}

        if (GUILayout.Button("Combine textures & build combined mesh using coroutine"))
        {
            Debug.Log("Starting to bake textures on frame " + Time.frameCount);
            MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();

            //These can be assets configured at runtime or you can create them
            // on the fly like this
            textureBaker.textureBakeResults = ScriptableObject.CreateInstance<MB2_TextureBakeResults>();
            textureBaker.resultMaterial = new Material(Shader.Find("Diffuse"));

            //register an OnSuccess function to be called when texture baking is complete
            textureBaker.onBuiltAtlasesSuccess = new MB3_TextureBaker.OnCombinedTexturesCoroutineSuccess(OnBuiltAtlasesSuccess);
			StartCoroutine(textureBaker.CreateAtlasesCoroutine(null,result,false,null,.01f));

        }
    }

    void OnBuiltAtlasesSuccess()
    {
        Debug.Log("Calling success callback. baking meshes");
        MB3_MeshBaker meshbaker = target.GetComponentInChildren<MB3_MeshBaker>();
        MB3_TextureBaker textureBaker = target.GetComponent<MB3_TextureBaker>();
        //elapsedTime = Time.realtimeSinceStartup - t1;

        if (result.isFinished &&
            result.success)
        {
            meshbaker.ClearMesh(); //only necessary if your not sure whats in the combined mesh
            meshbaker.textureBakeResults = textureBaker.textureBakeResults;
            //Add the objects to the combined mesh
            meshbaker.AddDeleteGameObjects(textureBaker.GetObjectsToCombine().ToArray(), null, true);
            meshbaker.Apply();
        }
        Debug.Log("Completed baking textures on frame " + Time.frameCount);
    }
}