obj导入unity顶点数量改变

相关网页

 为什么 unity 中正方体网格的顶点数量为 24?_unity quad顶点数_Beetxm_的博客-CSDN博客

你知道Unity3D中一个cube有几个顶点吗?_unity顶点数_Marco&GalaxyDragon的博客-CSDN博客

 为什么Unity中的顶点数比Max中的多?_blender unity 顶点数量不一致-CSDN博客 

为什么是24个顶点?因为盒子的尖角处不是1个顶点,是3个重合的顶点,从法线图里就看的很明显:

这个图就很形象:

Splitting Up Verticies - Questions & Answers - Unity Discussions

实验 

obj文件,是个cube,里面有8个顶点12个面。

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# 创建的文件:11.04.2023 13:05:22

#
# object Box001
#

v  -15.0000 -15.0000 15.0000
v  -15.0000 -15.0000 -15.0000
v  15.0000 -15.0000 -15.0000
v  15.0000 -15.0000 15.0000
v  -15.0000 15.0000 15.0000
v  15.0000 15.0000 15.0000
v  15.0000 15.0000 -15.0000
v  -15.0000 15.0000 -15.0000
# 8 vertices

g Box001
f 1 2 3 
f 3 4 1 
f 5 6 7 
f 7 8 5 
f 1 4 6 
f 6 5 1 
f 4 3 7 
f 7 6 4 
f 3 2 8 
f 8 7 3 
f 2 1 5 
f 5 8 2 
# 12 faces

导入unity:

8个顶点变24个顶点

12个面还是12个面

解决方法1

直接把法线设置成无就行了:

顶点依旧是8个:

就是没有法线,也就没有光照了。

解决方法2

如果既不想改变顶点数量,又想要光照的话,可以这么搞:

操作方法

关于模型导入面板的介绍:

Unity模型导入相关知识_unity导入fbx_虫虫!的博客-CSDN博客

设置方法:

Imported model vertex count doesn't match .obj file - Unity Forum

主要是把光滑角度拉到180度

结果:

关于光滑

大概是这种感觉:

How to get a smooth mesh for Unity? - Questions & Answers - Unity Discussions

聊聊图形学中的Flat shading、Gouraud shading、Phong shading - 知乎 (zhihu.com)

拉到180,180是最大的角度了,所以,所有的edge都需要光滑。光滑,类似高斯模糊的那种感觉。你中有我,我中有你。

拉到0,所有的edge都没有光滑,就挺硬,硬边界,泾渭分明。

相关代码 

完了的话,可以打印一下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowNormals : MonoBehaviour
{
    MeshFilter meshFilter = null;
    int vertexNum = 0;
    Vector3[] vertices = null;
    Vector3[] normals = null;
    

    // Start is called before the first frame update
    void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
        vertexNum = meshFilter.mesh.vertexCount;

        vertices = meshFilter.mesh.vertices;

        GetComponent<MeshFilter>().mesh.RecalculateNormals(); 

        normals = meshFilter.mesh.normals;


        int[] triangles = meshFilter.mesh.triangles;

        Debug.Log("顶点数"+vertexNum+"三角形数"+triangles.Length/3);

        for(int i = 0;i<triangles.Length/3;i++){
            Debug.Log(triangles[i*3+0]+" "+triangles[i*3+1]+" "+triangles[i*3+2]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        // 可视化法线
        for(int i=0;i<vertexNum;i++){
            Debug.DrawLine(vertices[i] , vertices[i] + 15 * normals[i],Color.green,1000,true);
        }
    }
}

把内容都打印出来,和obj里的稍微有点不一样…… 

因为obj里顶点索引是从1开始算的,unity里是从0开始算的

比如第一个面,obj里是1,2,3;unity里打印的是0,2,1。unity里都加1就是1,3,2。

为什么顺序不一样呢,可能是因为3dmax和unity坐标系不同,顶点排列顺序就是反的,它顺时针,它逆时针什么的。

总体是对的上的。

百度:

When 3d models (OBJ) are imported into Unity, the Unity editor apparently adds more vertices to it. I found a workaround to get the number of vertices back to the original, but it messes up the original indices of vertices. Any idea how to fix this? : r/Unity3D (reddit.com)

OBJ import changes vertex order with Optimize Mesh set to "Nothing" - Unity Forum

3DS Max -> Unity Morphing Vertex problem? - Unity Forum

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值