读取obj文件用Mesh创建实例化
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine.Networking;
using System;
using System.Threading;
using UnityEngine.UI;
public class ObjToUnityNO : MonoBehaviour
{
public Material red;
public Text []AAA;
public static ObjToUnityNO Ins_ObjToUnityNo;
private Mesh dq_mesh;
Vector3[] _vertexArray;
ArrayList _vertexArrayList = new ArrayList();
Vector3[] _normalArray;
ArrayList _normalArrayList = new ArrayList();
Vector2[] _uvArray;
ArrayList _uvArrayList = new ArrayList();
int[] _triangleArray;
ArrayList _facesVertNormUV = new ArrayList();
private void Start()
{
Ins_ObjToUnityNo = this;
}
public void meshas()
{
//调用创建Mesh方法
Draw_mesh(Application.persistentDataPath + "/Sphere_0.obj");//安卓路径(可以)
//Draw_mesh(Application.streamingAssetsPath + "/Cube_0.obj");//PC路径
}
public void Draw_mesh(string Obj_Path)
{
//初始化mesh
GetComponent<MeshFilter>().mesh = dq_mesh = new Mesh();
dq_mesh.name = "Meshs";
//赋材质
//this.gameObject.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Diffuse"));
this.gameObject.GetComponent<MeshRenderer>().material = red;
//调用方法,获取点、面、法线
init_mesh(Obj_Path);
//点、面、法线赋值生成mesh
dq_mesh.vertices = _vertexArray;
dq_mesh.triangles = _triangleArray;
dq_mesh.normals = _normalArray;
}
void init_mesh(string Obj_Path)
{
if (File.Exists(Obj_Path))
{
//查找对应文件,加载读取
//UnityWebRequest uwr = UnityWebRequest.Get("file://" + Application.streamingAssetsPath + "/Cube_0.obj");
//UnityWebRequest uwr = UnityWebRequest.Get(Application.persistentDataPath + "/Cube_0.obj");
UnityWebRequest uwr = UnityWebRequest.Get("file://" + Obj_Path);
uwr.Send();
Thread.Sleep(2000);//时间等待
//yield return uwr.Send();或用协同程序形式
//分割读取的obj文件
if (uwr.isDone && string.IsNullOrEmpty(uwr.error))
{
string s = uwr.downloadHandler.text;
s = s.Replace(" ", " ");
s = s.Replace(" ", " ");
//调用方法,去分割好的string中找点、面、法线等,给相应的数组赋值
LoadFile(s);
}
else
{
Debug.Log("Don't DownLoad ");
}
}
}
public void LoadFile(string s)
{
string[] lines = s.Split("\n"[0]);
foreach (string item in lines)
{
//调用方法,去分割好的string中找点、面、法线等,给相应的数组赋值
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)
{
AAA[3].text = "ReadLine";
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);
}
}
}
}
internal class PlacesByIndex
{
public PlacesByIndex(int index)
{
_index = index;
}
public int _index;
public ArrayList _places = new ArrayList();
}
注:此脚本根据obj格式大神代码,根据自己需求,进行读取obj文件,添加数组,构建mesh,进行实例化,可在安卓和PC端实现功能,其他平台未尝试,obj文件不要太大,否则会造成unity卡死情况。如有雷同或更好的方案,可在评论区留言讨论.........
读取obj文件用Mesh创建实例化的更多相关文章
- [计算机图形学] OpenGL读取obj文件并显示其3D效果
读取三维网格模型(Wavefront OBJ文件) 无法向立方体:cube.obj 有法向兔子模型:bunny.obj 有法向有纹理八字模型:Eight.obj OBJ文件的格式可参考:http: ...
- 软件光栅器实现(四、OBJ文件加载)
本节介绍软件光栅器的OBJ和MTL文件加载,转载请注明出处. 在管线的应用程序阶段,我们需要设置光栅器所渲染的模型数据.这些模型数据包括模型顶点的坐标.纹理.法线和材质等等,可以由我们手动编写,也可以 ...
- 用eclipse做项目中常遇到的问题-如何创建并读取properties文件
在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...
- 在C#中创建和读取XML文件
1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...
- 手工创建tomcat应用,以及实现js读取本地文件内容
手工创建tomcat应用: 1.在webapps下面新建应用目录文件夹 2.在文件夹下创建或是从其他应用中复制:META-INF,WEB-INF这两个文件夹, 其中META-INF清空里面,WEB-I ...
- Delphi判断文件是否正在被使用(CreateFile也可以只是为了读取数据,而不是创建)
首先,我们先来认识下CreateFile函数,它的原型如下 HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名的指针 DWORD dwDesired ...
- 使用univocity-parsers创建和读取csv文件
import com.univocity.parsers.csv.CsvFormat;import com.univocity.parsers.csv.CsvParser;import com.uni ...
- C# -- 使用Aspose.Cells创建和读取Excel文件
使用Aspose.Cells创建和读取Excel文件 1. 创建Excel Aspose.Cells.License li = new Aspose.Cells.License(); li.SetLi ...
- Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流
/* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...
随机推荐
- SQLServer学习-- SQLServer
SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行M ...
- Android-读取操作系统通话记录并/拨打电话/发送短信/复制号码到拨号盘
apps目录的contacts应用(有读取通话记录功能),是访问provider目录的provider.contacts应用(有暴露通话记录),所以要阅读Android操作系统源码-->pack ...
- Autofac的简单使用
今天记录一下学习Autofac的过程. 之前对IoC与DI一直很迷糊,今天研究了前辈们的文章后,才对IoC和DI有了一个初步的了解.感谢前辈们的无私奉献! 文章地址: 依赖注入和控制反转的理解,写的太 ...
- jquery call cross-domain webapi owin self-host
<!DOCTYPE HTML> <html LANG="cn"> <head> <meta name="viewport&quo ...
- 冒泡排序算法 :BubbleSort
java中的经典算法:冒泡排序算法 $. 可以理解成当你静止一杯可乐时,里面的CO2随着你的静止,由于不不易溶于水的性质, 且会以气泡的形式逐渐向上漂浮.越大的气泡上浮速度越快. 冒泡排序算法的原理于 ...
- 201621123012 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集 ...
- leetcode 31. Next Permutation JAVA
题目: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数 ...
- fread和fwrite用法小结
fwrite和fread是以记录为单位的I/O函数,fread和fwrite函数一般用于二进制文件的输入输出. #include <stdio.h>size_t fread(void *p ...
- The server of Nginx(二)——Nginx基本功能配置
一.Nginx访问控制 (1)基于授权的访问控制 Nginx于Apache一样,可以实现基于用户授权的访问控制,当客户端要访问相应网站或者目录时要求输入用户名密码才能正常访问,配置步骤与Apache基 ...
- QQ第三方登陆示例
先上图 若想实现QQ登录,需要成为QQ互联的开发者,审核通过才可实现.注册方法可参考链接http://wiki.connect.qq.com/%E6%88%90%E4%B8%BA%E5%BC%80%E ...