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

相关文章

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA