holopy3/Assets/PlattarExporter/UnityGLTF/Scripts/GLTFComponent.cs
Lena Biresch 490ef558ef CleanUp
2021-01-28 13:07:52 +01:00

63 lines
1.5 KiB
C#

using System.Collections;
using System.IO;
using UnityEngine;
namespace UnityGLTF {
/// <summary>
/// Component to load a GLTF scene with
/// </summary>
class GLTFComponent : MonoBehaviour
{
public string Url;
public bool Multithreaded = true;
public bool UseStream = false;
public int MaximumLod = 300;
public Shader GLTFStandard;
public Shader GLTFStandardSpecular;
public Shader GLTFConstant;
public bool addColliders = false;
IEnumerator Start()
{
GLTFSceneImporter loader = null;
FileStream gltfStream = null;
if (UseStream)
{
var fullPath = Application.streamingAssetsPath + Url;
gltfStream = File.OpenRead(fullPath);
loader = new GLTFSceneImporter(
fullPath,
gltfStream,
gameObject.transform,
addColliders
);
}
else
{
loader = new GLTFSceneImporter(
Url,
gameObject.transform,
addColliders
);
}
loader.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.PbrMetallicRoughness, GLTFStandard);
loader.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.KHR_materials_pbrSpecularGlossiness, GLTFStandardSpecular);
loader.SetShaderForMaterialType(GLTFSceneImporter.MaterialType.CommonConstant, GLTFConstant);
loader.MaximumLod = MaximumLod;
yield return loader.Load(-1, Multithreaded);
if(gltfStream != null)
{
#if WINDOWS_UWP
gltfStream.Dispose();
#else
gltfStream.Close();
#endif
}
}
}
}