Unity使用Mesh动态加载3D模型

2024-05-30 04:18

本文主要是介绍Unity使用Mesh动态加载3D模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Unity提供Mesh类型,允许用户动态的将顶点坐标,顶点颜色,顶点法向量,顶点UV等属性动态传输给Mesh类,在场景中绘制用户自定义的三维模型。

下面说下实现步骤:

1. 在场景中创建一个空物体,右键单击->Create Empty,命名为DynamicCubeObject。

2. 在工程中创建一个C#脚本,命名为DynamicMesh.cs,并将DynamicMesh.cs脚本拖拽到DynamicCubeObject上,DynamicMesh.cs代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DynamicMesh : MonoBehaviour
{private Mesh DynamicMeshCube;private Vector3[] VertexPositions;  private Color[] VertexColors;private Vector3[] VertexNornals;private int[] Indices;// Start is called before the first frame updatevoid Start(){DynamicMeshCube = new Mesh();GameObject go = new GameObject("Dynamic_Cube");go.transform.SetParent(this.transform, false);go.transform.localScale = new Vector3(1, 1, 1);//go.transform.rotation = Quaternion.AngleAxis(0, Vector3.up);MeshFilter meshFilter = go.AddComponent<MeshFilter>();MeshRenderer meshRenderer = go.AddComponent<MeshRenderer>();meshFilter.mesh = DynamicMeshCube;meshRenderer.material = new Material(Shader.Find("Unlit/CubeShader"));}// Update is called once per framevoid Update(){}void FixedUpdate(){VertexPositions = new Vector3[36];VertexColors = new Color[36];VertexNornals = new Vector3[36];VertexPositions[0] = new Vector3(-1.0f, 1.0f, 1.0f);VertexColors[0]    = new Color(0f, 0.5273f, 0.2656f);VertexNornals[0]   = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[1] = new Vector3(-1.0f, -1.0f, 1.0f);VertexColors[1] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[1] = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[2] = new Vector3(1.0f, 1.0f, 1.0f);VertexColors[2] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[2] = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[3] = new Vector3(-1.0f, -1.0f, 1.0f);VertexColors[3] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[3] = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[4] = new Vector3(1.0f, -1.0f, 1.0f);VertexColors[4] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[4] = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[5] = new Vector3(1.0f, 1.0f, 1.0f);VertexColors[5] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[5] = new Vector3(0.0f, 0.0f, 1.0f);VertexPositions[6] = new Vector3(1.0f, 1.0f, 1.0f);VertexColors[6] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[6] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[7] = new Vector3(1.0f, -1.0f, 1.0f);VertexColors[7] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[7] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[8] = new Vector3(1.0f, 1.0f, -1.0f);VertexColors[8] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[8] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[9] = new Vector3(1.0f, -1.0f, 1.0f);VertexColors[9] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[9] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[10] = new Vector3(1.0f, -1.0f, -1.0f);VertexColors[10] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[10] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[11] = new Vector3(1.0f, 1.0f, -1.0f);VertexColors[11] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[11] = new Vector3(1.0f, 0.0f, 0.0f);VertexPositions[12] = new Vector3(1.0f, 1.0f, -1.0f);VertexPositions[13] = new Vector3(1.0f, -1.0f, -1.0f);VertexPositions[14] = new Vector3(-1.0f, 1.0f, -1.0f);VertexPositions[15] = new Vector3(1.0f, -1.0f, -1.0f);VertexPositions[16] = new Vector3(-1.0f, -1.0f, -1.0f);VertexPositions[17] = new Vector3(-1.0f, 1.0f, -1.0f);VertexColors[12] = new Color(0f, 0.5273f, 0.2656f);VertexColors[13] = new Color(0f, 0.5273f, 0.2656f);VertexColors[14] = new Color(0f, 0.5273f, 0.2656f);VertexColors[15] = new Color(0f, 0.5273f, 0.2656f);VertexColors[16] = new Color(0f, 0.5273f, 0.2656f);VertexColors[17] = new Color(0f, 0.5273f, 0.2656f);VertexNornals[12] = new Vector3(0.0f, 0.0f, -1.0f);VertexNornals[13] = new Vector3(0.0f, 0.0f, -1.0f);VertexNornals[14] = new Vector3(0.0f, 0.0f, -1.0f);VertexNornals[15] = new Vector3(0.0f, 0.0f, -1.0f);VertexNornals[16] = new Vector3(0.0f, 0.0f, -1.0f);VertexNornals[17] = new Vector3(0.0f, 0.0f, -1.0f);VertexPositions[18] = new Vector3(-1.0f, 1.0f, -1.0f);VertexPositions[19] = new Vector3(-1.0f, -1.0f, -1.0f);VertexPositions[20] = new Vector3(-1.0f, 1.0f, 1.0f);VertexPositions[21] = new Vector3(-1.0f, -1.0f, -1.0f);VertexPositions[22] = new Vector3(-1.0f, -1.0f, 1.0f);VertexPositions[23] = new Vector3(-1.0f, 1.0f, 1.0f);VertexColors[18] = new Color(0.0f, 0.3398f, 0.9023f);VertexColors[19] = new Color(0.0f, 0.3398f, 0.9023f);VertexColors[20] = new Color(0.0f, 0.3398f, 0.9023f);VertexColors[21] = new Color(0.0f, 0.3398f, 0.9023f);VertexColors[22] = new Color(0.0f, 0.3398f, 0.9023f);VertexColors[23] = new Color(0.0f, 0.3398f, 0.9023f);VertexNornals[18] = new Vector3(-1.0f, 0.0f, 0.0f);VertexNornals[19] = new Vector3(-1.0f, 0.0f, 0.0f);VertexNornals[20] = new Vector3(-1.0f, 0.0f, 0.0f);VertexNornals[21] = new Vector3(-1.0f, 0.0f, 0.0f);VertexNornals[22] = new Vector3(-1.0f, 0.0f, 0.0f);VertexNornals[23] = new Vector3(-1.0f, 0.0f, 0.0f);VertexPositions[24] = new Vector3(-1.0f, 1.0f, -1.0f);VertexPositions[25] = new Vector3(-1.0f, 1.0f, 1.0f);VertexPositions[26] = new Vector3(1.0f, 1.0f, -1.0f);VertexPositions[27] = new Vector3(-1.0f, 1.0f, 1.0f);VertexPositions[28] = new Vector3(1.0f, 1.0f, 1.0f);VertexPositions[29] = new Vector3(1.0f, 1.0f, -1.0f);VertexColors[24] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexColors[25] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexColors[26] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexColors[27] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexColors[28] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexColors[29] = new Color(0.8359375f,  0.17578125f,  0.125f);VertexNornals[24] = new Vector3(0.0f, 1.0f, 0.0f);VertexNornals[25] = new Vector3(0.0f, 1.0f, 0.0f);VertexNornals[26] = new Vector3(0.0f, 1.0f, 0.0f);VertexNornals[27] = new Vector3(0.0f, 1.0f, 0.0f);VertexNornals[28] = new Vector3(0.0f, 1.0f, 0.0f);VertexNornals[29] = new Vector3(0.0f, 1.0f, 0.0f);VertexPositions[30] = new Vector3(1.0f, -1.0f, -1.0f);VertexPositions[31] = new Vector3(1.0f, -1.0f, 1.0f);VertexPositions[32] = new Vector3(-1.0f, -1.0f, -1.0f);VertexPositions[33] = new Vector3(1.0f, -1.0f, 1.0f);VertexPositions[34] = new Vector3(-1.0f, -1.0f, 1.0f);VertexPositions[35] = new Vector3(-1.0f, -1.0f, -1.0f);VertexColors[30] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexColors[31] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexColors[32] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexColors[33] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexColors[34] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexColors[35] = new Color(0.8359375f, 0.17578125f, 0.125f);VertexNornals[30] = new Vector3(0.0f, -1.0f, 0.0f);VertexNornals[31] = new Vector3(0.0f, -1.0f, 0.0f);VertexNornals[32] = new Vector3(0.0f, -1.0f, 0.0f);VertexNornals[33] = new Vector3(0.0f, -1.0f, 0.0f);VertexNornals[34] = new Vector3(0.0f, -1.0f, 0.0f);VertexNornals[35] = new Vector3(0.0f, -1.0f, 0.0f);Indices = new int[36];for (int i = 0; i < 36; i++){Indices[i] = i;}DynamicMeshCube.Clear();DynamicMeshCube.SetVertices(VertexPositions);DynamicMeshCube.SetIndices(Indices, MeshTopology.Triangles, 0);DynamicMeshCube.SetColors(VertexColors);DynamicMeshCube.SetNormals(VertexNornals);this.transform.Rotate(Vector3.right, 45 * Time.deltaTime, Space.Self);this.transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.Self);}
}

如上代码所示,实现的是一个简单的Cube,当然也可以从fbx,obj等模型文件中解析出顶点拓扑信息,传输给mesh。在上面的示例为了方便展示使用的顶点颜色而不是顶点的UV,所以还需要一个自制的Shader才能正常显示。

3. 创建目录Assets->Resources,在Resources目录下新建CubeShader.shader,DynamicMesh.cs会在代码中动态加载CubeShader.shader,CubeShader.shader代码如下:

Shader "Unlit/CubeShader"
{Properties{_Color("Color Tint", Color) = (1, 1, 1, 1)_MainTex("Main Tex", 2D) = "white" {}_BumpMap("Normal Map", 2D) = "bump" {}}SubShader{Tags { "RenderType" = "Opaque" "Queue" = "Geometry"}Pass {Tags { "LightMode" = "ForwardBase" }CGPROGRAM#pragma multi_compile_fwdbase#pragma vertex vert#pragma fragment frag#include "Lighting.cginc"#include "AutoLight.cginc"fixed4 _Color;sampler2D _MainTex;float4 _MainTex_ST;sampler2D _BumpMap;float4 _BumpMap_ST;struct a2v {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float4 texcoord : TEXCOORD0;fixed4 color : COLOR0;};struct v2f {float4 pos : SV_POSITION;float4 uv : TEXCOORD0;float4 TtoW0 : TEXCOORD1;float4 TtoW1 : TEXCOORD2;float4 TtoW2 : TEXCOORD3;fixed4 color : COLOR0;SHADOW_COORDS(4)};v2f vert(a2v v) {v2f o;o.pos = UnityObjectToClipPos(v.vertex);o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;o.color = v.color;float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);TRANSFER_SHADOW(o);return o;}fixed4 frag(v2f i) : SV_Target {float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));fixed3 albedo = i.color.rgb * _Color.rgb;fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, lightDir));UNITY_LIGHT_ATTENUATION(atten, i, worldPos);return fixed4(ambient + diffuse * atten, 1.0);}ENDCG}Pass {Tags { "LightMode" = "ForwardAdd" }Blend One OneCGPROGRAM#pragma multi_compile_fwdadd// Use the line below to add shadows for point and spot lights//#pragma multi_compile_fwdadd_fullshadows#pragma vertex vert#pragma fragment frag#include "Lighting.cginc"#include "AutoLight.cginc"fixed4 _Color;sampler2D _MainTex;float4 _MainTex_ST;sampler2D _BumpMap;float4 _BumpMap_ST;struct a2v {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float4 texcoord : TEXCOORD0;fixed4 color : COLOR0;};struct v2f {float4 pos : SV_POSITION;float4 uv : TEXCOORD0;float4 TtoW0 : TEXCOORD1;float4 TtoW1 : TEXCOORD2;float4 TtoW2 : TEXCOORD3;fixed4 color : COLOR0;SHADOW_COORDS(4)};v2f vert(a2v v) {v2f o;o.pos = UnityObjectToClipPos(v.vertex);o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;o.color = v.color;float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);TRANSFER_SHADOW(o);return o;}fixed4 frag(v2f i) : SV_Target {float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));fixed3 albedo = i.color.rgb * _Color.rgb;fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, lightDir));UNITY_LIGHT_ATTENUATION(atten, i, worldPos);return fixed4(diffuse * atten, 1.0);}ENDCG}}FallBack "Diffuse"
}

4. 点击运行,效果图如下所示:

这篇关于Unity使用Mesh动态加载3D模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1015556

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三