123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.IO;
- using System.Text;
- using TriLibCore.Interfaces;
- using TriLibCore.Utils;
- using UnityEngine;
- namespace TriLibCore.Samples
- {
-
-
-
- public class SimpleCustomLoaderSample : MonoBehaviour
- {
-
-
-
- private const string SmilePngData = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAJUExURQAAAP/yAP///1XtZyMAAAA+SURBVAjXY1gFBAyrQkNXMawMDc1iWBoaGsUwNTQ0jGGqoyOMAHNBxJTQUDGGqaIhQK4DYxhEMVgb2ACQUQBbZhuGX7UQtQAAAABJRU5ErkJggg==";
-
-
-
- private const string CubeObjData =
- @"mtllib cube.mtl
- g default
- v -0.500000 -0.500000 0.500000
- v 0.500000 -0.500000 0.500000
- v -0.500000 0.500000 0.500000
- v 0.500000 0.500000 0.500000
- v -0.500000 0.500000 -0.500000
- v 0.500000 0.500000 -0.500000
- v -0.500000 -0.500000 -0.500000
- v 0.500000 -0.500000 -0.500000
- vt 0.000000 0.000000
- vt 1.000000 0.000000
- vt 1.000000 1.000000
- vt 0.000000 1.000000
- vt 0.000000 1.000000
- vt 1.000000 1.000000
- vt 1.000000 0.000000
- vt 0.000000 0.000000
- vt 0.000000 0.000000
- vt 1.000000 0.000000
- vt 1.000000 1.000000
- vt 0.000000 1.000000
- vt 1.000000 0.000000
- vt 0.000000 0.000000
- vt 0.000000 1.000000
- vt 1.000000 1.000000
- vt 0.000000 0.000000
- vt 1.000000 0.000000
- vt 1.000000 1.000000
- vt 0.000000 1.000000
- vt 0.000000 1.000000
- vt 1.000000 1.000000
- vt 1.000000 0.000000
- vt 0.000000 0.000000
- vn 0.000000 0.000000 1.000000
- vn 0.000000 0.000000 1.000000
- vn 0.000000 0.000000 1.000000
- vn 0.000000 0.000000 1.000000
- vn 0.000000 1.000000 0.000000
- vn 0.000000 1.000000 0.000000
- vn 0.000000 1.000000 0.000000
- vn 0.000000 1.000000 0.000000
- vn 0.000000 0.000000 -1.000000
- vn 0.000000 0.000000 -1.000000
- vn 0.000000 0.000000 -1.000000
- vn 0.000000 0.000000 -1.000000
- vn 0.000000 -1.000000 0.000000
- vn 0.000000 -1.000000 0.000000
- vn 0.000000 -1.000000 0.000000
- vn 0.000000 -1.000000 0.000000
- vn 1.000000 0.000000 0.000000
- vn 1.000000 0.000000 0.000000
- vn 1.000000 0.000000 0.000000
- vn 1.000000 0.000000 0.000000
- vn -1.000000 0.000000 0.000000
- vn -1.000000 0.000000 0.000000
- vn -1.000000 0.000000 0.000000
- vn -1.000000 0.000000 0.000000
- s off
- g cube
- usemtl initialShadingGroup
- f 1/17/1 2/18/2 4/19/3 3/20/4
- f 3/1/5 4/2/6 6/3/7 5/4/8
- f 5/21/9 6/22/10 8/23/11 7/24/12
- f 7/5/13 8/6/14 2/7/15 1/8/16
- f 2/9/17 8/10/18 6/11/19 4/12/20
- f 7/13/21 1/14/22 3/15/23 5/16/24
- ";
-
-
-
- private const string CubeMtlData =
- @"newmtl initialShadingGroup
- illum 4
- Kd 1.00 1.00 1.00
- Ka 0.00 0.00 0.00
- Tf 1.00 1.00 1.00
- map_Kd smile.png
- Ni 1.00
- Ks 0.00 0.00 0.00
- Ns 18.00
- ";
-
-
-
- private const string CubeObjFilename = "cube.obj";
-
-
-
- private const string CubeMtlFilename = "cube.mtl";
-
-
-
- private const string SmilePngFilename = "smile.png";
-
-
-
- private void Start()
- {
- var cubeObjBytes = Encoding.UTF8.GetBytes(CubeObjData);
- SimpleCustomAssetLoader.LoadModelFromByteData(cubeObjBytes, FileUtils.GetFileExtension(CubeObjFilename, false), OnError, OnProgress, OnModelFullyLoad, CustomDataReceivingCallback, CustomFilenameReceivingCallback, CustomTextureReceivingCallback, CubeObjFilename, gameObject);
- }
-
-
-
-
-
- private Stream CustomTextureReceivingCallback(ITexture texture)
- {
- var textureShortFilename = FileUtils.GetShortFilename(texture.Filename);
- if (textureShortFilename == SmilePngFilename)
- {
- var smilePngBytes = Convert.FromBase64String(SmilePngData);
- return new MemoryStream(smilePngBytes);
- }
- return null;
- }
-
-
-
-
-
-
- private string CustomFilenameReceivingCallback(string filename)
- {
- return filename;
- }
-
-
-
-
-
- private Stream CustomDataReceivingCallback(string filename)
- {
- var externalDataShortFilename = FileUtils.GetShortFilename(filename);
- if (externalDataShortFilename == CubeMtlFilename)
- {
- var cubeMtlBytes = Encoding.UTF8.GetBytes(CubeMtlData);
- return new MemoryStream(cubeMtlBytes);
- }
- return null;
- }
-
-
-
-
- private void OnModelFullyLoad(AssetLoaderContext assetLoaderContext)
- {
- if (assetLoaderContext.RootGameObject != null)
- {
- Debug.Log("Model successfully loaded.");
- }
- }
-
-
-
-
-
- private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
- {
- Debug.Log($"Progress: {progress:P}");
- }
-
-
-
-
- private void OnError(IContextualizedError contextualizedError)
- {
- Debug.LogError($"There was an error loading your model: {contextualizedError}");
- }
- }
- }
|