|
@@ -4,10 +4,15 @@ using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.IO.Compression;
|
|
|
+using System.Linq;
|
|
|
using System.Reflection;
|
|
|
using TriLibCore;
|
|
|
using TriLibCore.Extensions;
|
|
|
+using TriLibCore.Mappers;
|
|
|
+using TriLibCore.Samples;
|
|
|
+using TriLibCore.Utils;
|
|
|
using UnityEngine;
|
|
|
+using UnityEngine.Rendering;
|
|
|
|
|
|
public class TemplateModel : BaseTemPlate
|
|
|
{
|
|
@@ -217,6 +222,7 @@ public class TemplateModel : BaseTemPlate
|
|
|
|
|
|
DownLoadMaterial data = new DownLoadMaterial();
|
|
|
this.Data = data;
|
|
|
+ data.name = value.name;
|
|
|
data.downLoadPath = value.DownloadPath;
|
|
|
data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
|
|
|
data.updataTime = updateTime;
|
|
@@ -380,7 +386,6 @@ public class TemplateModel : BaseTemPlate
|
|
|
|
|
|
public bool ExtractZipFile(string zipFilePath)
|
|
|
{
|
|
|
- bool isAB=false;
|
|
|
if(!zipFilePath.Contains(".zip"))
|
|
|
{
|
|
|
return false;
|
|
@@ -388,71 +393,21 @@ public class TemplateModel : BaseTemPlate
|
|
|
Debug.Log("ExtractZipFile===>1");
|
|
|
using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
|
|
|
{
|
|
|
- byte[] ghbbytes = null;
|
|
|
- Debug.Log("ExtractZipFile===>2");
|
|
|
- foreach (ZipArchiveEntry entry in archive.Entries)
|
|
|
+ foreach (ZipArchiveEntry entry in archive.Entries)
|
|
|
+ {
|
|
|
+ Debug.Log("ExtractZipFile==FullName=>2" + entry.FullName);
|
|
|
+ if (entry.FullName.Contains("GHZPointCloud"))
|
|
|
{
|
|
|
- Debug.Log("ExtractZipFile==FullName=>2" + entry.FullName);
|
|
|
- if (entry.FullName.Contains(".ghb") || !entry.FullName.Contains("."))
|
|
|
- {
|
|
|
- isAB = true;
|
|
|
- if (entry.Name != "")
|
|
|
- {
|
|
|
- entry.ExtractToFile(Application.persistentDataPath+"/"+ entry.FullName,true);
|
|
|
- FileInfo finfo = new FileInfo(Application.persistentDataPath + "/" + entry.FullName);
|
|
|
- byte[] bytes= readbytes(finfo.OpenRead());
|
|
|
- if (!entry.FullName.Contains(".bytes"))
|
|
|
- {
|
|
|
- ghbbytes = bytes;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
- }
|
|
|
- else if(entry.FullName.Contains(".mp3"))
|
|
|
- {
|
|
|
-
|
|
|
- if (entry.Name != ""&&File.Exists(Application.persistentDataPath + "/" + entry.FullName))
|
|
|
- {
|
|
|
- entry.ExtractToFile(Application.persistentDataPath + "/" + entry.FullName, true);
|
|
|
- FileInfo finfo = new FileInfo(Application.persistentDataPath + "/" + entry.FullName);
|
|
|
- byte[] bytes = readbytes(finfo.OpenRead());
|
|
|
- float[] fs = bytesToFloat(bytes);
|
|
|
- AudioClip clip = AudioClip.Create(entry.FullName, fs.Length, 1, 16000, false);
|
|
|
- clip.SetData(fs, 0);
|
|
|
- if(!this.gameObject.GetComponent<AudioSource>())
|
|
|
- {
|
|
|
- this.gameObject.AddComponent<AudioSource>();
|
|
|
- }
|
|
|
- this.gameObject.GetComponent<AudioSource>().clip = clip;
|
|
|
- this.gameObject.GetComponent<AudioSource>().Play();
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- if(isAB)
|
|
|
- {
|
|
|
- Debug.Log("ExtractZipFile===>3");
|
|
|
- if (ghbbytes!=null)
|
|
|
- {
|
|
|
- Debug.Log("ExtractZipFile===>5");
|
|
|
- AssetBundle ab = AssetBundle.LoadFromMemory(ghbbytes);
|
|
|
- var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
|
|
|
- m_ModelObj = Instantiate(prefab, transform);
|
|
|
- ab.Unload(false);
|
|
|
+ return true;
|
|
|
|
|
|
- ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
|
|
|
- manipulation.enabled = false;
|
|
|
- BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
|
|
|
- boundingBox.enabled = false;
|
|
|
- }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
|
|
|
- return isAB;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
public static float[] bytesToFloat(byte[] byteArray)
|
|
@@ -495,73 +450,187 @@ public class TemplateModel : BaseTemPlate
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- Debug.Log("模型加载完成TriLibModel" + Data.localLoadPath);
|
|
|
+ Debug.Log("模型加载完成TriLibModel" + Data.downLoadPath);
|
|
|
|
|
|
- if (false)
|
|
|
+ if (false)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ AssetLoaderOptions AssetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
|
|
|
+ if (ExtractZipFile(Data.localLoadPath))
|
|
|
+ {
|
|
|
+ AssetLoaderOptions.LoadPointClouds = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ AssetLoaderOptions.LoadPointClouds = false;
|
|
|
+ }
|
|
|
|
|
|
- Debug.Log("模型加载完成TriLibModel" + Data.localLoadPath);
|
|
|
-
|
|
|
- try
|
|
|
+ AssetLoaderOptions.TextureMappers = new TextureMapper[] { ScriptableObject.CreateInstance<FilePickerTextureMapper>() };
|
|
|
+
|
|
|
+ if (!(AssetLoaderOptions.ExternalDataMapper is FilePickerExternalDataMapper))
|
|
|
{
|
|
|
+ AssetLoaderOptions.ExternalDataMapper = ScriptableObject.CreateInstance<FilePickerExternalDataMapper>();
|
|
|
+ }
|
|
|
+ if (AssetLoaderOptions.LoadPointClouds)
|
|
|
+ {
|
|
|
+ Debug.Log("模型加载完成TriLibModel" + AssetLoaderOptions.LoadPointClouds);
|
|
|
+ AssetLoader.LoadModelFromFile(Data.localLoadPath, (AssetLoaderContext ac) => {
|
|
|
|
|
|
- TriLibModelLoad.Load(Data.localLoadPath, (AssetLoaderContext ac) => {
|
|
|
Debug.Log("模型加载完成");
|
|
|
+ }, (AssetLoaderContext ac) => {
|
|
|
+ Debug.Log("载材质加完成");
|
|
|
+ HandlePointClouds(ac);
|
|
|
+
|
|
|
+ ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
|
|
|
+ manipulation.enabled = false;
|
|
|
+ BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
|
|
|
+ boundingBox.enabled = false;
|
|
|
+ ac.RootGameObject.SetActive(false);
|
|
|
+
|
|
|
+ if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
|
|
|
+ {
|
|
|
+ Debug.Log("Animation===>1" + m_ModelObj.name);
|
|
|
+
|
|
|
+ Animation _animation = m_ModelObj.GetComponent<Animation>();
|
|
|
+ List<AnimationClip> _animations = _animation.GetAllAnimationClips();
|
|
|
+ m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
|
|
|
+ }
|
|
|
+ if (this.gameObject.GetComponent<BoxCollider>())
|
|
|
+ {
|
|
|
+ this.gameObject.GetComponent<BoxCollider>().size = Vector3.one * 0.4f;
|
|
|
+ }
|
|
|
+
|
|
|
+ }, (AssetLoaderContext ac, float f) => {
|
|
|
+
|
|
|
+ Debug.Log("加载中==》" + f);
|
|
|
+ }, (IContextualizedError error) => {
|
|
|
+
|
|
|
+ Debug.Log("加载失败" + error);
|
|
|
+ }, null, AssetLoaderOptions);
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ TriLibLocalLoad.Load(Data.localLoadPath, (AssetLoaderContext ac) => {
|
|
|
+
|
|
|
}, (AssetLoaderContext ac) => {
|
|
|
Debug.Log("载材质加完成");
|
|
|
- Bounds bounds = GetAllBounds.GetRendererBounds(ac.RootGameObject);
|
|
|
- GameObject obj = new GameObject("Test");
|
|
|
- obj.transform.position = bounds.center;
|
|
|
- obj.transform.SetParent(ac.RootGameObject.transform);
|
|
|
- float bizhi = 0.4f / bounds.size.x;
|
|
|
- ac.RootGameObject.transform.localScale = Vector3.one * bizhi;
|
|
|
- ac.RootGameObject.transform.SetParent(this.transform.parent);
|
|
|
- Vector3 v3local = (Vector3.zero - obj.transform.position) ;
|
|
|
- ac.RootGameObject.transform.SetParent(this.transform);
|
|
|
- ac.RootGameObject.transform.localPosition = v3local;
|
|
|
- ac.RootGameObject.transform.localScale = ac.RootGameObject.transform.localScale * this.transform.localScale.x;
|
|
|
- ac.RootGameObject.transform.localEulerAngles = Vector3.zero;
|
|
|
- ac.RootGameObject.SetActive(true);
|
|
|
-
|
|
|
-
|
|
|
- m_ModelObj = Instantiate(ac.RootGameObject, transform);
|
|
|
- ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
|
|
|
- manipulation.enabled = false;
|
|
|
- BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
|
|
|
- boundingBox.enabled = false;
|
|
|
- ac.RootGameObject.SetActive(false);
|
|
|
-
|
|
|
- if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
|
|
|
- {
|
|
|
- Debug.Log("Animation===>1" + m_ModelObj.name);
|
|
|
+ Bounds bounds = GetAllBounds.GetRendererBounds(ac.RootGameObject);
|
|
|
+ GameObject obj = new GameObject("Test");
|
|
|
+ obj.transform.position = bounds.center;
|
|
|
+ obj.transform.SetParent(ac.RootGameObject.transform);
|
|
|
+ float bizhi = 0.4f / bounds.size.x;
|
|
|
+ if(bizhi==float.NaN || bizhi == float.PositiveInfinity || bizhi == float.NegativeInfinity)
|
|
|
+ { bizhi = 1; }
|
|
|
+
|
|
|
+ ac.RootGameObject.transform.localScale = Vector3.one * bizhi;
|
|
|
+ ac.RootGameObject.transform.SetParent(this.transform.parent);
|
|
|
+ Vector3 v3local = (Vector3.zero - obj.transform.position);
|
|
|
+ ac.RootGameObject.transform.SetParent(this.transform);
|
|
|
+ ac.RootGameObject.transform.localPosition = v3local;
|
|
|
+ ac.RootGameObject.transform.localScale = ac.RootGameObject.transform.localScale * this.transform.localScale.x;
|
|
|
+ ac.RootGameObject.transform.localEulerAngles = Vector3.zero;
|
|
|
+ ac.RootGameObject.SetActive(true);
|
|
|
+
|
|
|
+
|
|
|
+ m_ModelObj = Instantiate(ac.RootGameObject, transform);
|
|
|
+ ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
|
|
|
+ manipulation.enabled = false;
|
|
|
+ BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
|
|
|
+ boundingBox.enabled = false;
|
|
|
+ ac.RootGameObject.SetActive(false);
|
|
|
|
|
|
- Animation _animation = m_ModelObj.GetComponent<Animation>();
|
|
|
- List<AnimationClip> _animations = _animation.GetAllAnimationClips();
|
|
|
- m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
|
|
|
- }
|
|
|
- if (this.gameObject.GetComponent<BoxCollider>())
|
|
|
- {
|
|
|
- this.gameObject.GetComponent<BoxCollider>().size = Vector3.one * 0.4f;
|
|
|
- }
|
|
|
+ if (m_ModelObj && m_ModelObj.GetComponent<Animation>())
|
|
|
+ {
|
|
|
+ Debug.Log("Animation===>1" + m_ModelObj.name);
|
|
|
+
|
|
|
+ Animation _animation = m_ModelObj.GetComponent<Animation>();
|
|
|
+ List<AnimationClip> _animations = _animation.GetAllAnimationClips();
|
|
|
+ m_ModelObj.GetComponent<Animation>().Play(_animations[0].name, PlayMode.StopAll);
|
|
|
+ }
|
|
|
+ if (this.gameObject.GetComponent<BoxCollider>())
|
|
|
+ {
|
|
|
+ this.gameObject.GetComponent<BoxCollider>().size = Vector3.one * 0.4f;
|
|
|
+ }
|
|
|
}, (AssetLoaderContext ac, float f) => {
|
|
|
|
|
|
Debug.Log("加载中==》" + f);
|
|
|
}, (IContextualizedError error) => {
|
|
|
|
|
|
Debug.Log("加载失败" + error);
|
|
|
- });
|
|
|
+ },null, AssetLoaderOptions);
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
Debug.Log("加载失败" + Data.localLoadPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void HandlePointClouds(AssetLoaderContext assetLoaderContext)
|
|
|
+ {
|
|
|
+ Material material = null;
|
|
|
+ foreach (var gameObject in assetLoaderContext.GameObjects.Values)
|
|
|
+ {
|
|
|
+ m_ModelObj = Instantiate(gameObject, transform);
|
|
|
+ if (m_ModelObj.TryGetComponent<MeshFilter>(out var meshFilter))
|
|
|
+ {
|
|
|
+ var points = meshFilter.sharedMesh.vertices;
|
|
|
+ var colors = meshFilter.sharedMesh.colors32;
|
|
|
+ if (colors == null || colors.Length != points.Length)
|
|
|
+ {
|
|
|
+ colors = new Color32[points.Length];
|
|
|
+ for (var i = 0; i < colors.Length; i++)
|
|
|
+ {
|
|
|
+ colors[i] = Color.white;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D11 && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Direct3D12)
|
|
|
+ {
|
|
|
+ UnityEngine.Debug.Log("graphicsDeviceTypegraphicsDeviceTypegraphicsDeviceType" + SystemInfo.graphicsDeviceType);
|
|
|
+ var mesh = new Mesh();
|
|
|
+ mesh.indexFormat = IndexFormat.UInt32;
|
|
|
+ mesh.SetVertices(points);
|
|
|
+ mesh.SetColors(colors);
|
|
|
+ mesh.SetIndices(
|
|
|
+ Enumerable.Range(0, points.Length).ToArray(),
|
|
|
+ MeshTopology.Points, 0
|
|
|
+ );
|
|
|
+ mesh.UploadMeshData(!assetLoaderContext.Options.ReadEnabled);
|
|
|
+ meshFilter.sharedMesh = mesh;
|
|
|
+ var meshRenderer = m_ModelObj.AddComponent<MeshRenderer>();
|
|
|
+ var materials = new Material[meshFilter.sharedMesh.subMeshCount];
|
|
|
+ if (material == null)
|
|
|
+ {
|
|
|
+ material = new Material(Shader.Find("Hidden/PointCloud_GL"));
|
|
|
+ }
|
|
|
+ for (var i = 0; i < materials.Length; i++)
|
|
|
+ {
|
|
|
+ materials[i] = material;
|
|
|
+ }
|
|
|
+ meshRenderer.materials = materials;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var pointCloudRenderer = m_ModelObj.AddComponent<PointCloudRenderer>();
|
|
|
+ var data = ScriptableObject.CreateInstance<PointCloudData>();
|
|
|
+ data.Initialize(points, colors);
|
|
|
+ pointCloudRenderer.sourceData = data;
|
|
|
+ pointCloudRenderer.destroyData = true;
|
|
|
+ pointCloudRenderer.pointSize = 0.01f;
|
|
|
+ assetLoaderContext.Allocations.Add(data);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
public override void HideCollider()
|
|
|
{
|
|
|
base.HideCollider();
|