unity通过WWW方式加载.obj文件


using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Globalization;

public class ObjReaderInSence : MonoBehaviour {
	Mesh _myMesh;
	Material _myMaterial = new Material(Shader.Find("Diffuse"));
	
	Vector3[] _vertexArray;
	ArrayList _vertexArrayList = new ArrayList();
	Vector3[] _normalArray;
	ArrayList _normalArrayList = new ArrayList();
	Vector2[] _uvArray;
	ArrayList _uvArrayList = new ArrayList();
	
	int[] _triangleArray;
	
	ArrayList _facesVertNormUV = new ArrayList();
	
    internal class PlacesByIndex {
		public PlacesByIndex(int index) {
			_index = index;
		}
		public int _index;
		public ArrayList _places = new ArrayList();
    }
	void initArrayLists() {
		_uvArrayList = new ArrayList();
		_normalArrayList = new ArrayList();
		_vertexArrayList = new ArrayList();
		_facesVertNormUV = new ArrayList();
	}
	
	public IEnumerator SomeFunction(string path,string _textureLink) {;
		GameObject obj_gameobject = new GameObject();
		obj_gameobject.name = path;
		initArrayLists();
		if (_myMesh != null)
			_myMesh.Clear();
		_myMesh = new Mesh();
		_myMesh.name = path;
		WWW www3d = new WWW(path);
		yield return www3d;
		string s = www3d.data;
		s = s.Replace("  ", " ");
		s = s.Replace("  ", " ");
		LoadFile(s);
		_myMesh.vertices = _vertexArray;
		_myMesh.triangles = _triangleArray;
		if (_uvArrayList.Count > 0)
			_myMesh.uv = _uvArray;
		if (_normalArrayList.Count > 0)
			_myMesh.normals = _normalArray;
		else
			_myMesh.RecalculateNormals();
		_myMesh.RecalculateBounds();
		if ((MeshFilter)obj_gameobject.GetComponent("MeshFilter") == null)
			obj_gameobject.AddComponent("MeshFilter");
		MeshFilter temp;
		temp = (MeshFilter)obj_gameobject.GetComponent("MeshFilter");
		temp.mesh = _myMesh;
		if ((MeshRenderer)obj_gameobject.GetComponent("MeshRenderer") == null)
			obj_gameobject.AddComponent("MeshRenderer");
		if (_uvArrayList.Count > 0 && _textureLink != "") {
			WWW wwwtx = new WWW(_textureLink);
			yield return wwwtx;
			_myMaterial.mainTexture = wwwtx.texture;
		}
		MeshRenderer temp2;
		temp2 = (MeshRenderer)obj_gameobject.GetComponent("MeshRenderer");
		if (_uvArrayList.Count > 0 && _textureLink != "") {
			temp2.material = _myMaterial;
			_myMaterial.shader = Shader.Find("Diffuse");
		}
		yield return new WaitForFixedUpdate();
	}
	
	public void LoadFile(string s) {
		string[] lines = s.Split("\n"[0]);
		
		foreach (string item in lines) {
			ReadLine(item);
		}
		ArrayList tempArrayList = new ArrayList();
		for (int i = 0; i < _facesVertNormUV.Count; ++i) {
			if (_facesVertNormUV[i] != null) {
				PlacesByIndex indextemp = new PlacesByIndex(i);
				indextemp._places.Add(i);
				for (int j = 0; j < _facesVertNormUV.Count; ++j) {
					if (_facesVertNormUV[j] != null) {
						if (i != j) {
							Vector3 iTemp = (Vector3)_facesVertNormUV[i];
							Vector3 jTemp = (Vector3)_facesVertNormUV[j];
							if (iTemp.x == jTemp.x && iTemp.y == jTemp.y) {
								indextemp._places.Add(j);
								_facesVertNormUV[j] = null;
							}
						}
					}
				}
				tempArrayList.Add(indextemp);
			}
		}
		_vertexArray = new Vector3[tempArrayList.Count];
		_uvArray = new Vector2[tempArrayList.Count];
		_normalArray = new Vector3[tempArrayList.Count];
		_triangleArray = new int[_facesVertNormUV.Count];
		int teller = 0;
		foreach (PlacesByIndex item in tempArrayList) {
			foreach (int item2 in item._places) {
				_triangleArray[item2] = teller;
			}
			Vector3 vTemp = (Vector3)_facesVertNormUV[item._index];
			_vertexArray[teller] = (Vector3)_vertexArrayList[(int)vTemp.x - 1];
			if (_uvArrayList.Count > 0) {
				Vector3 tVec = (Vector3)_uvArrayList[(int)vTemp.y - 1];
				_uvArray[teller] = new Vector2(tVec.x, tVec.y);
			}
			if (_normalArrayList.Count > 0) {
				_normalArray[teller] = (Vector3)_normalArrayList[(int)vTemp.z - 1];
			}
			teller++;
		}
	}
	
	public void ReadLine(string s) {
		char[] charsToTrim = {' ', '\n', '\t', '\r'};
		s= s.TrimEnd(charsToTrim);
		string[] words = s.Split(" "[0]);
		foreach (string item in words)
			item.Trim();
		if (words[0] == "v")
			_vertexArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[3], CultureInfo.InvariantCulture)));

		if (words[0] == "vn")
			_normalArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[3], CultureInfo.InvariantCulture)));
		if (words[0] == "vt") 
			_uvArrayList.Add(new Vector3(System.Convert.ToSingle(words[1], CultureInfo.InvariantCulture), System.Convert.ToSingle(words[2], CultureInfo.InvariantCulture)));
		if (words[0] == "f") {
			ArrayList temp = new ArrayList();
			ArrayList triangleList = new ArrayList();
			for (int j = 1; j < words.Length; ++j)
			{
				Vector3 indexVector = new Vector3(0,0);
				string[] indices = words[j].Split("/"[0]);
				indexVector.x = System.Convert.ToInt32(indices[0], CultureInfo.InvariantCulture);
				if (indices.Length > 1) {
					if (indices[1] != "")
						indexVector.y = System.Convert.ToInt32(indices[1], CultureInfo.InvariantCulture);
				}
				if (indices.Length > 2) {
					if (indices[2] != "")
						indexVector.z = System.Convert.ToInt32(indices[2], CultureInfo.InvariantCulture);
				}
				temp.Add(indexVector);
			}
			for (int i = 1; i < temp.Count - 1; ++i) {
				triangleList.Add(temp[0]);
				triangleList.Add(temp[i]);
				triangleList.Add(temp[i+1]);
			}

			foreach (Vector3 item in triangleList) {
				_facesVertNormUV.Add(item);
			}
		}
	}
}


using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	// Use this for initialization
	void Start () {
		string path = "file:///Users/dilitech/Desktop/panzhiwei/unity356/testobj/Test/AW_055_YIGUI_1(Clone).obj";
		string _textureLink = "";
		ObjReaderInSence obj = new ObjReaderInSence();
		StartCoroutine(obj.SomeFunction(path,_textureLink));//obj路径&&贴图路径
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
加载.obj文件是一种常见的三维模型加载方式。.obj文件是一种保存三维模型数据的文件格式,其中包含了模型的顶点位置、法向量、纹理坐标等信息。通过使用WWW方式加载.obj文件,可以实现在网络环境中加载和显示模型。 首先,我们需要创建一个空的游戏对象来显示模型。在该游戏对象上添加一个MeshFilter组件和一个MeshRenderer组件。 接下来,我们使用WWW类来加载.obj文件。在Unity中,WWW类提供了一种简单的方式来从网络中加载文件。我们需要提供.obj文件的完整URL作为WWW的参数。例如,如果.obj文件存储在服务器上,URL可能是"http://www.example.com/model.obj"。 然后,我们使用WWW类的GetAssetBundle方法来加载.obj文件加载完成后,我们可以通过访问WWW类的assetBundle属性来获取模型的数据。 最后,使用Mesh类的FromObj方法将获取到的数据转换为Mesh。然后,将转换后的Mesh赋值给MeshFilter组件的mesh属性。 完成以上步骤后,我们就可以在场景中显示加载的.obj文件了。通过将创建的游戏对象添加到场景中,我们可以在编辑器中或者在运行时看到加载的三维模型。 需要注意的是,加载.obj文件可能需要较长的时间,需要在加载过程中显示加载中的提示信息,避免用户等待太久而感到不适。 总的来说,通过WWW方式加载.obj文件是一种较为常见的做法,可以让我们在Unity中方便地加载和显示网络中的三维模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值