Tanoda
SimpleCustomLoaderSample.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
4using TriLibCore.Interfaces;
5using TriLibCore.Utils;
6using UnityEngine;
7
8namespace TriLibCore.Samples
9{
13 public class SimpleCustomLoaderSample : MonoBehaviour
14 {
18 private const string SmilePngData =
19 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAJUExURQAAAP/yAP///1XtZyMAAAA+SURBVAjXY1gFBAyrQkNXMawMDc1iWBoaGsUwNTQ0jGGqoyOMAHNBxJTQUDGGqaIhQK4DYxhEMVgb2ACQUQBbZhuGX7UQtQAAAABJRU5ErkJggg==";
20
24 private const string CubeObjData =
25 @"mtllib cube.mtl
26 g default
27 v -0.500000 -0.500000 0.500000
28 v 0.500000 -0.500000 0.500000
29 v -0.500000 0.500000 0.500000
30 v 0.500000 0.500000 0.500000
31 v -0.500000 0.500000 -0.500000
32 v 0.500000 0.500000 -0.500000
33 v -0.500000 -0.500000 -0.500000
34 v 0.500000 -0.500000 -0.500000
35 vt 0.000000 0.000000
36 vt 1.000000 0.000000
37 vt 1.000000 1.000000
38 vt 0.000000 1.000000
39 vt 0.000000 1.000000
40 vt 1.000000 1.000000
41 vt 1.000000 0.000000
42 vt 0.000000 0.000000
43 vt 0.000000 0.000000
44 vt 1.000000 0.000000
45 vt 1.000000 1.000000
46 vt 0.000000 1.000000
47 vt 1.000000 0.000000
48 vt 0.000000 0.000000
49 vt 0.000000 1.000000
50 vt 1.000000 1.000000
51 vt 0.000000 0.000000
52 vt 1.000000 0.000000
53 vt 1.000000 1.000000
54 vt 0.000000 1.000000
55 vt 0.000000 1.000000
56 vt 1.000000 1.000000
57 vt 1.000000 0.000000
58 vt 0.000000 0.000000
59 vn 0.000000 0.000000 1.000000
60 vn 0.000000 0.000000 1.000000
61 vn 0.000000 0.000000 1.000000
62 vn 0.000000 0.000000 1.000000
63 vn 0.000000 1.000000 0.000000
64 vn 0.000000 1.000000 0.000000
65 vn 0.000000 1.000000 0.000000
66 vn 0.000000 1.000000 0.000000
67 vn 0.000000 0.000000 -1.000000
68 vn 0.000000 0.000000 -1.000000
69 vn 0.000000 0.000000 -1.000000
70 vn 0.000000 0.000000 -1.000000
71 vn 0.000000 -1.000000 0.000000
72 vn 0.000000 -1.000000 0.000000
73 vn 0.000000 -1.000000 0.000000
74 vn 0.000000 -1.000000 0.000000
75 vn 1.000000 0.000000 0.000000
76 vn 1.000000 0.000000 0.000000
77 vn 1.000000 0.000000 0.000000
78 vn 1.000000 0.000000 0.000000
79 vn -1.000000 0.000000 0.000000
80 vn -1.000000 0.000000 0.000000
81 vn -1.000000 0.000000 0.000000
82 vn -1.000000 0.000000 0.000000
83 s off
84 g cube
85 usemtl initialShadingGroup
86 f 1/17/1 2/18/2 4/19/3 3/20/4
87 f 3/1/5 4/2/6 6/3/7 5/4/8
88 f 5/21/9 6/22/10 8/23/11 7/24/12
89 f 7/5/13 8/6/14 2/7/15 1/8/16
90 f 2/9/17 8/10/18 6/11/19 4/12/20
91 f 7/13/21 1/14/22 3/15/23 5/16/24
92 ";
93
97 private const string CubeMtlData =
98 @"newmtl initialShadingGroup
99 illum 4
100 Kd 1.00 1.00 1.00
101 Ka 0.00 0.00 0.00
102 Tf 1.00 1.00 1.00
103 map_Kd smile.png
104 Ni 1.00
105 Ks 0.00 0.00 0.00
106 Ns 18.00
107 ";
108
112 private const string CubeObjFilename = "cube.obj";
113
117 private const string CubeMtlFilename = "cube.mtl";
118
122 private const string SmilePngFilename = "smile.png";
123
127 private void Start()
128 {
129 var cubeObjBytes = Encoding.UTF8.GetBytes(CubeObjData);
131 FileUtils.GetFileExtension(CubeObjFilename, false), OnError, OnProgress, OnModelFullyLoad,
132 CustomDataReceivingCallback, CustomFilenameReceivingCallback, CustomTextureReceivingCallback,
133 CubeObjFilename, gameObject);
134 }
135
141 private Stream CustomTextureReceivingCallback(ITexture texture)
142 {
143 var textureShortFilename = FileUtils.GetShortFilename(texture.Filename);
144 if (textureShortFilename == SmilePngFilename)
145 {
146 var smilePngBytes = Convert.FromBase64String(SmilePngData);
147 return new MemoryStream(smilePngBytes);
148 }
149
150 return null;
151 }
152
159 private string CustomFilenameReceivingCallback(string filename)
160 {
161 return filename;
162 }
163
169 private Stream CustomDataReceivingCallback(string filename)
170 {
171 var externalDataShortFilename = FileUtils.GetShortFilename(filename);
172 if (externalDataShortFilename == CubeMtlFilename)
173 {
174 var cubeMtlBytes = Encoding.UTF8.GetBytes(CubeMtlData);
175 return new MemoryStream(cubeMtlBytes);
176 }
177
178 return null;
179 }
180
185 private void OnModelFullyLoad(AssetLoaderContext assetLoaderContext)
186 {
187 if (assetLoaderContext.RootGameObject != null) Debug.Log("Model successfully loaded.");
188 }
189
195 private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
196 {
197 Debug.Log($"Progress: {progress:P}");
198 }
199
204 private void OnError(IContextualizedError contextualizedError)
205 {
206 Debug.LogError($"There was an error loading your model: {contextualizedError}");
207 }
208 }
209}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Represents a class used to load models from Byte Arrays using callbacks to map External Data and Text...
static AssetLoaderContext LoadModelFromByteData(byte[] data, string modelExtension, Action< IContextualizedError > onError, Action< AssetLoaderContext, float > onProgress, Action< AssetLoaderContext > onModelFullyLoad, Func< string, Stream > customDataReceivingCallback, Func< string, string > customFilenameReceivingCallback, Func< ITexture, Stream > customTextureReceivingCallback, string modelFilename=null, GameObject wrapperGameObject=null, AssetLoaderOptions assetLoaderOptions=null, object customData=null)
Loads a model from the given Byte Array data using the given callbacks to handle events/external data...
This sample loads an OBJ model with a single texture from Strings contents as the data source.