Unity3d 如何在3d模型上动态应用纹理?
我有一个文件,其中写入了顶点、索引、uv、纹理等的数组,用两个词表示所有内容,以便绘制带有纹理的模型。例如,它应该是具有类似木材纹理的3d立方体 所以,我现在拥有的是——我可以展示立方体的顶点(我看到我的模型),但我不知道如何应用纹理 这是我的密码-Unity3d 如何在3d模型上动态应用纹理?,unity3d,Unity3d,我有一个文件,其中写入了顶点、索引、uv、纹理等的数组,用两个词表示所有内容,以便绘制带有纹理的模型。例如,它应该是具有类似木材纹理的3d立方体 所以,我现在拥有的是——我可以展示立方体的顶点(我看到我的模型),但我不知道如何应用纹理 这是我的密码- public void Start() { m_stream = DecoderAPI.create_stream_decoder_obj(); string pathToFile = "path_
public void Start()
{
m_stream = DecoderAPI.create_stream_decoder_obj();
string pathToFile = "path_to_my_file";
bool isInitialized = DecoderAPI.stream_init_model(m_stream, pathToFile);
if (isInitialized)
{
m_curFrame = DecoderAPI.stream_get_frame_obj(m_stream, 1);
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
Mesh mesh = new Mesh();
//Vertices***
int vertexCount = DecoderAPI.frame_get_vertex_count(m_curFrame);
int xyzArrSize = vertexCount * 3;
float[] xyzArray = new float[xyzArrSize];
IntPtr xyz = DecoderAPI.frame_get_vertex_xyz(m_curFrame);
Marshal.Copy(xyz, xyzArray, 0, xyzArrSize);
Vector3[] vertices = new Vector3[vertexCount];
for (int i = 0; i < vertexCount; i++)
{
vertices[i] = new Vector3(xyzArray[i * 3], xyzArray[i * 3 + 1], xyzArray[i * 3 + 2]);
}
mesh.vertices = vertices;
//***
//Faces***
int faceCount = DecoderAPI.frame_face_count(m_curFrame);
int trisArrSize = faceCount * 3;
int[] tris = new int[trisArrSize];
IntPtr indices = DecoderAPI.frame_face_indices(m_curFrame);
Marshal.Copy(indices, tris, 0, trisArrSize);
mesh.triangles = tris;
//***
mesh.RecalculateNormals();
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
//TEXTURE ****
int uvCount = DecoderAPI.frame_get_uv_count(m_curFrame);
IntPtr uvData = DecoderAPI.frame_get_uv_data(m_curFrame);
IntPtr textureObj = DecoderAPI.frame_get_texture_obj(m_curFrame);
DecoderAPI.TextureInfo textureInfo = DecoderAPI.texture_get_info(textureObj);
int width = textureInfo.width;
int height = textureInfo.height;
int channels = textureInfo.channels;
int stride = textureInfo.stride;
DecoderAPI.ColorType color_type = textureInfo.color_type;
IntPtr pixels = textureInfo.pixels;
HOW TO APPLY THIS TEXTURE DATA TO MY MODEL????
//***
DecoderAPI.frame_release(m_curFrame);
}
}
public void Start()
{
m_stream=DecoderAPI.create_stream_decoder_obj();
string pathToFile=“路径到我的文件”;
bool isInitialized=DecoderAPI.stream_init_模型(m_stream,pathToFile);
如果(已初始化)
{
m_curFrame=DecoderAPI.stream_get_frame_obj(m_stream,1);
MeshRenderer=gameObject.AddComponent();
meshRenderer.sharedMaterial=新材质(Shader.Find(“标准”);
网格=新网格();
//顶点***
int vertexCount=解码帧获取顶点计数(m\u curFrame);
int xyzArrSize=顶点计数*3;
float[]xyzArray=新的float[xyzArrSize];
IntPtr xyz=解码帧_获取_顶点_xyz(m_curFrame);
封送处理副本(xyz,xyzArray,0,xyzArrSize);
Vector3[]顶点=新Vector3[vertexCount];
对于(int i=0;i
我找到了这个答案-
但我需要知道如何动态地应用它
有什么建议吗?或者也许有些人认为是教程
编辑
public void Start()
{
m_stream = DecoderAPI.create_stream_decoder_obj();
string pathToFile = "my_path_to_file";
bool isInitialized = DecoderAPI.stream_init_model(m_stream, pathToFile);
if (isInitialized)
{
m_curFrame = DecoderAPI.stream_get_frame_obj(m_stream, 1);
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
Mesh mesh = new Mesh();
//Vertices***
int vertexCount = DecoderAPI.frame_get_vertex_count(m_curFrame);
int xyzArrSize = vertexCount * 3;
float[] xyzArray = new float[xyzArrSize];
IntPtr xyz = DecoderAPI.frame_get_vertex_xyz(m_curFrame);
Marshal.Copy(xyz, xyzArray, 0, xyzArrSize);
Vector3[] vertices = new Vector3[vertexCount];
for (int i = 0; i < vertexCount; i++)
{
vertices[i] = new Vector3(xyzArray[i * 3], xyzArray[i * 3 + 1], xyzArray[i * 3 + 2]);
}
mesh.vertices = vertices;
//***
//Faces***
int faceCount = DecoderAPI.frame_face_count(m_curFrame);
int trisArrSize = faceCount * 3;
int[] tris = new int[trisArrSize];
IntPtr indices = DecoderAPI.frame_face_indices(m_curFrame);
Marshal.Copy(indices, tris, 0, trisArrSize);
mesh.triangles = tris;
//***
mesh.RecalculateNormals();
//UV***
int uvCount = DecoderAPI.frame_get_uv_count(m_curFrame);
IntPtr uvData = DecoderAPI.frame_get_uv_data(m_curFrame);
int uvArrSize = uvCount * 2;
float[] uvArr = new float[uvArrSize];
Vector2[] uv = new Vector2[uvCount];
Marshal.Copy(uvData, uvArr, 0, uvArrSize);
for(int i = 0; i < uvCount; i++)
{
uv[i] = new Vector2(uvArr[i * 2], uvArr[i * 2 + 1]);
}
mesh.uv = uv;
//***
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
//TEXTURE ****
IntPtr textureObj = DecoderAPI.frame_get_texture_obj(m_curFrame);
DecoderAPI.TextureInfo textureInfo = DecoderAPI.texture_get_info(textureObj);
int width = textureInfo.width;
int height = textureInfo.height;
int channels = textureInfo.channels;
int stride = textureInfo.stride;
DecoderAPI.ColorType color_type = textureInfo.color_type;
IntPtr pixels = textureInfo.pixels;
Texture2D texture = new Texture2D(width, height);
texture.LoadRawTextureData(pixels, width * channels *height);
texture.Apply();
meshRenderer.material.SetTexture("_MainText", texture);
//***
DecoderAPI.frame_release(m_curFrame);
}
}
public void Start()
{
m_stream=DecoderAPI.create_stream_decoder_obj();
string pathToFile=“my_path_to_file”;
bool isInitialized=DecoderAPI.stream_init_模型(m_stream,pathToFile);
如果(已初始化)
{
m_curFrame=DecoderAPI.stream_get_frame_obj(m_stream,1);
MeshRenderer=gameObject.AddComponent();
meshRenderer.sharedMaterial=新材质(Shader.Find(“标准”);
网格=新网格();
//顶点***
int vertexCount=解码帧获取顶点计数(m\u curFrame);
int xyzArrSize=顶点计数*3;
float[]xyzArray=新的float[xyzArrSize];
IntPtr xyz=解码帧_获取_顶点_xyz(m_curFrame);
封送处理副本(xyz,xyzArray,0,xyzArrSize);
Vector3[]顶点=新Vector3[vertexCount];
对于(int i=0;iUnityException: LoadRawTextureData: not enough data provided (will result in overread).
UnityEngine.Texture2D.LoadRawTextureData (System.IntPtr data, System.Int32 size) (at <a9810827dce3444a8e5c4e9f3f5e0828>:0)
Model.Start () (at Assets/Scripts/Model.cs:98)
myTexture.LoadRawTextureData(myPixels);
myTexture.Apply();