## 3d模型任意切割的算法（一） 原

梁potato

``````
/*
* @authors: liangjian
* @desc:
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ClipMesh : MonoBehaviour {

// Use this for initialization
void Start () {
MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();

//顶点数组转顶点容器
List<Vector3> verticeList = new List<Vector3>();
int verticeCount = mf.mesh.vertices.Length;
for (int verticeIndex = 0; verticeIndex < verticeCount; ++verticeIndex)
{
}
//三角形数组转三角形容器
List<int> triangleList = new List<int>();
int triangleCount = mf.mesh.triangles.Length;
for (int triangleIndex = 0; triangleIndex < triangleCount; ++triangleIndex)
{
}
//uv坐标数组转uv坐标容器
List<Vector2> uvList = new List<Vector2>();
int uvCount = mf.mesh.uv.Length;
for (int uvIndex = 0; uvIndex < uvCount; ++uvIndex)
{
}
//顶点颜色数组转顶点颜色容器
List<Vector3> normalList = new List<Vector3>();
int normalCount = mf.mesh.normals.Length;
for (int normalIndex = 0; normalIndex < normalCount; ++normalIndex)
{
}

//检查每个三角面，是否存在两个顶点连接正好在直线上
for (int triangleIndex = 0; triangleIndex < triangleList.Count;)
{
int trianglePoint0 = triangleList[triangleIndex];
int trianglePoint1 = triangleList[triangleIndex + 1];
int trianglePoint2 = triangleList[triangleIndex + 2];

Vector3 point0 = verticeList[trianglePoint0];
Vector3 point1 = verticeList[trianglePoint1];
Vector3 point2 = verticeList[trianglePoint2];

float planeY = 0.3f;
//0-1，1-2相连线段被切割
if ((point0.y - planeY)* (point1.y - planeY) < 0 && (point1.y - planeY) * (point2.y - planeY) < 0)
{
//截断0-1之间的顶点
float k01 = (point1.y - point0.y) / (planeY - point0.y);
float newPointX01 = (point1.x - point0.x) / k01 + point0.x;
float newPointZ01 = (point1.z - point0.z) / k01 + point0.z;
Vector3 newPoint0_1 = new Vector3(newPointX01, planeY, newPointZ01);
//uv
if(uvList.Count > 0)
{
Vector2 uv0 = uvList[trianglePoint0];
Vector2 uv1 = uvList[trianglePoint1];
float newUV_x = (uv1.x - uv0.x) / k01 + uv0.x;
float newUV_y = (uv1.y - uv0.y) / k01 + uv0.y;
}
//法向量
Vector3 normalX0 = normalList[trianglePoint0];
Vector3 normalX1 = normalList[trianglePoint1];
Vector3 normalX2 = normalList[trianglePoint2];
float newNoramlX01 = (normalX1.x - normalX0.x) / k01 + normalX0.x;
float newNoramlY01 = (normalX1.y - normalX0.y) / k01 + normalX0.y;
float newNoramlZ01 = (normalX1.z - normalX0.z) / k01 + normalX0.z;
//截断1-2之间的顶点
float k12 = (point2.y - point1.y) / (planeY - point1.y);
float newPointX12 = (point2.x - point1.x) / k12 + point1.x;
float newPointZ12 = (point2.z - point1.z) / k12 + point1.z;
Vector3 newPoint1_2 = new Vector3(newPointX12, planeY, newPointZ12);
if (uvList.Count > 0)
{
Vector2 uv1 = uvList[trianglePoint1];
Vector2 uv2 = uvList[trianglePoint2];
float newUV_x = (uv2.x - uv1.x) / k12 + uv1.x;
float newUV_y = (uv2.y - uv1.y) / k12 + uv1.y;
}
//法向量
float newNoramlX12 = (normalX2.x - normalX1.x) / k12 + normalX1.x;
float newNoramlY12 = (normalX2.y - normalX1.y) / k12 + normalX1.y;
float newNoramlZ12 = (normalX2.z - normalX1.z) / k12 + normalX1.z;

int newVerticeCount = verticeList.Count;
//插入顶点索引，以此构建新三角形
triangleList.Insert(triangleIndex + 1, newVerticeCount - 2);
triangleList.Insert(triangleIndex + 2, newVerticeCount - 1);

triangleList.Insert(triangleIndex + 3, newVerticeCount - 1);
triangleList.Insert(triangleIndex + 4, newVerticeCount - 2);

triangleList.Insert(triangleIndex + 6, trianglePoint0);
triangleList.Insert(triangleIndex + 7, newVerticeCount - 1);
}
//1-2，2-0相连线段被切割
else if ((point1.y - planeY) * (point2.y - planeY) < 0 && (point2.y - planeY) * (point0.y - planeY) < 0)
{
//截断1-2之间的顶点
float k12 = (point2.y - point1.y) / (planeY - point1.y);
float newPointX12 = (point2.x - point1.x) / k12 + point1.x;
float newPointZ12 = (point2.z - point1.z) / k12 + point1.z;
Vector3 newPoint1_2 = new Vector3(newPointX12, planeY, newPointZ12);
if (uvList.Count > 0)
{
Vector2 uv1 = uvList[trianglePoint1];
Vector2 uv2 = uvList[trianglePoint2];
float newUV_x = (uv2.x - uv1.x) / k12 + uv1.x;
float newUV_y = (uv2.y - uv1.y) / k12 + uv1.y;
}
//法向量
Vector3 normalX0 = normalList[trianglePoint0];
Vector3 normalX1 = normalList[trianglePoint1];
Vector3 normalX2 = normalList[trianglePoint2];
float newNoramlX12 = (normalX2.x - normalX1.x) / k12 + normalX1.x;
float newNoramlY12 = (normalX2.y - normalX1.y) / k12 + normalX1.y;
float newNoramlZ12 = (normalX2.z - normalX1.z) / k12 + normalX1.z;

//截断0-2之间的顶点
float k02 = (point2.y - point0.y) / (planeY - point0.y);
float newPointX02 = (point2.x - point0.x) / k02 + point0.x;
float newPointZ02 = (point2.z - point0.z) / k02 + point0.z;
Vector3 newPoint0_2 = new Vector3(newPointX02, planeY, newPointZ02);
//uv
if (uvList.Count > 0)
{
Vector2 uv0 = uvList[trianglePoint0];
Vector2 uv2 = uvList[trianglePoint2];
float newUV_x = (uv2.x - uv0.x) / k02 + uv0.x;
float newUV_y = (uv2.y - uv0.y) / k02 + uv0.y;
}
//法向量
float newNoramlX02 = (normalX1.x - normalX0.x) / k02 + normalX0.x;
float newNoramlY02 = (normalX1.y - normalX0.y) / k02 + normalX0.y;
float newNoramlZ02 = (normalX1.z - normalX0.z) / k02 + normalX0.z;

int newVerticeCount = verticeList.Count;
//插入顶点索引，以此构建新三角形

//{0}
//{1}
triangleList.Insert(triangleIndex + 2, newVerticeCount - 2);

triangleList.Insert(triangleIndex + 3, newVerticeCount - 1);
triangleList.Insert(triangleIndex + 4, newVerticeCount - 2);
//{2}

triangleList.Insert(triangleIndex + 6, newVerticeCount - 1);
triangleList.Insert(triangleIndex + 7, trianglePoint0);
triangleList.Insert(triangleIndex + 8, newVerticeCount - 2);
}
//0-1，2-0相连线段被切割
else if((point0.y - planeY) * (point1.y - planeY) < 0 && (point2.y - planeY) * (point0.y - planeY) < 0)
{
//截断0-1之间的顶点
float k01 = (point1.y - point0.y) / (planeY - point0.y);
float newPointX01 = (point1.x - point0.x) / k01 + point0.x;
float newPointZ01 = (point1.z - point0.z) / k01 + point0.z;
Vector3 newPoint0_1 = new Vector3(newPointX01, planeY, newPointZ01);
//uv
if (uvList.Count > 0)
{
Vector2 uv0 = uvList[trianglePoint0];
Vector2 uv1 = uvList[trianglePoint1];
float newUV_x = (uv1.x - uv0.x) / k01 + uv0.x;
float newUV_y = (uv1.y - uv0.y) / k01 + uv0.y;
}
//法向量
Vector3 normalX0 = normalList[trianglePoint0];
Vector3 normalX1 = normalList[trianglePoint1];
Vector3 normalX2 = normalList[trianglePoint2];
float newNoramlX01 = (normalX1.x - normalX0.x) / k01 + normalX0.x;
float newNoramlY01 = (normalX1.y - normalX0.y) / k01 + normalX0.y;
float newNoramlZ01 = (normalX1.z - normalX0.z) / k01 + normalX0.z;

//截断0-2之间的顶点
float k02 = (point2.y - point0.y) / (planeY - point0.y);
float newPointX02 = (point2.x - point0.x) / k02 + point0.x;
float newPointZ02 = (point2.z - point0.z) / k02 + point0.z;
Vector3 newPoint0_2 = new Vector3(newPointX02, planeY, newPointZ02);
//uv
if (uvList.Count > 0)
{
Vector2 uv0 = uvList[trianglePoint0];
Vector2 uv2 = uvList[trianglePoint2];
float newUV_x = (uv2.x - uv0.x) / k02 + uv0.x;
float newUV_y = (uv2.y - uv0.y) / k02 + uv0.y;
}
//法向量
float newNoramlX02 = (normalX1.x - normalX0.x) / k02 + normalX0.x;
float newNoramlY02 = (normalX1.y - normalX0.y) / k02 + normalX0.y;
float newNoramlZ02 = (normalX1.z - normalX0.z) / k02 + normalX0.z;

int newVerticeCount = verticeList.Count;
//插入顶点索引，以此构建新三角形

//{0}
triangleList.Insert(triangleIndex + 1, newVerticeCount - 2);
triangleList.Insert(triangleIndex + 2, newVerticeCount - 1);

triangleList.Insert(triangleIndex + 3, newVerticeCount - 2);
//{1}
//{2}

triangleList.Insert(triangleIndex + 6, trianglePoint2);
triangleList.Insert(triangleIndex + 7, newVerticeCount - 1);
triangleList.Insert(triangleIndex + 8, newVerticeCount - 2);
}
//只有0-1被切
else if((point0.y - planeY) * (point1.y - planeY) < 0)
{
Debug.Log("只有01被切");
}
//只有1-2被切
else if ((point1.y - planeY) * (point2.y - planeY) < 0)
{
Debug.Log("只有12被切");
}
//只有2-0被切
else if ((point2.y - planeY) * (point0.y - planeY) < 0)
{
Debug.Log("只有02被切");
}
triangleIndex += 3;
}

//筛选出切割面两侧的顶点索引
List<int> triangles1 = new List<int>();
List<int> triangles2 = new List<int>();
for (int triangleIndex = 0; triangleIndex < triangleList.Count; triangleIndex += 3)
{
int trianglePoint0 = triangleList[triangleIndex];
int trianglePoint1 = triangleList[triangleIndex + 1];
int trianglePoint2 = triangleList[triangleIndex + 2];

Vector3 point0 = verticeList[trianglePoint0];
Vector3 point1 = verticeList[trianglePoint1];
Vector3 point2 = verticeList[trianglePoint2];
//切割面
float planeY = 0.3f;
if(point0.y > planeY || point1.y > planeY || point2.y > planeY)
{
}
else
{
}
}

mf.mesh.vertices = verticeList.ToArray();
mf.mesh.triangles = triangles1.ToArray();
if (uvList.Count > 0)
{
mf.mesh.uv = uvList.ToArray();
}
mf.mesh.normals = normalList.ToArray();

//分割模型
GameObject newModel = new GameObject("New Model");
meshFilter.mesh.vertices = mf.mesh.vertices;
meshFilter.mesh.triangles = triangles2.ToArray();
meshFilter.mesh.uv = mf.mesh.uv;
meshFilter.mesh.normals = mf.mesh.normals;
newRenderer.material = this.gameObject.GetComponent<MeshRenderer>().material;
}
}``````

### 梁potato

Flappy Bird（安卓版）逆向分析（一）

enimey
2014/03/04
6K
18
beego API开发以及自动化文档

beego API开发以及自动化文档 beego1.3版本已经在上个星期发布了，但是还是有很多人不了解如何来进行开发，也是在一步一步的测试中开发，期间QQ群里面很多人都问我如何开发，我的业余时间实在...

astaxie
2014/06/25
2.7W
22

2013/11/09
4.2K
1

Codgen是一个基于数据库元数据模型，使用freemarker模板引擎来构建输出的代码生成器。freemarker的数据模型结构通常来说都是一个Map树状结构模型，codgen也不例外，它的数据模型这棵树的根节...

2013/01/29
1.4W
2

Castle Game Engine 是一个用 Object Pascal 开发的跨平台 3D 游戏引擎。包含一个灵活的 3D 对象系统与开箱即用的水平,项目,智能生物等等。使用 X3D、VRML、Collada 和其他格式实现渲染和处理...

2013/02/05
2K
0

osc_45536bvu
52分钟前
16
0

osc_0boqdoe2
53分钟前
0
0

2017 年，全球第一个基因治疗方法 CAR-T 细胞药物 Kymriah 获得 FDA 上市批准，从此掀起了基因治疗的热潮。随着相关技术和政策的不断成熟，基因治疗市场也随之扩大。根据德勤发布的《引领...

osc_k3vwonkw
55分钟前
10
0
LightningChart.NET使用两个BarSeries创建简单的2D图表

roffey
55分钟前
0
0
Mybatis trim 标签的 2 个妙用！

osc_x03qsedc
56分钟前
10
0