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使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可