本文整理汇总了C#中UnityEngine.Mesh类的典型用法代码示例。如果您正苦于以下问题:C# Mesh类的具体用法?C# Mesh怎么用?C# Mesh使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Mesh类属于UnityEngine命名空间,在下文中一共展示了Mesh类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FileToMesh
// Use this for initialization
public static Mesh FileToMesh(string filePath)
{
meshStruct newMesh = createMeshStruct(filePath);
populateMeshStruct(ref newMesh);
Vector3[] newVerts = new Vector3[newMesh.faceData.Length];
Vector2[] newUVs = new Vector2[newMesh.faceData.Length];
Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
int i = 0;
/* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
* for the appropriate Unity mesh array.
*/
foreach (Vector3 v in newMesh.faceData)
{
newVerts[i] = newMesh.vertices[(int)v.x - 1];
if (v.y >= 1)
newUVs[i] = newMesh.uv[(int)v.y - 1];
if (v.z >= 1)
newNormals[i] = newMesh.normals[(int)v.z - 1];
i++;
}
Mesh mesh = new Mesh();
mesh.vertices = newVerts;
mesh.uv = newUVs;
mesh.normals = newNormals;
mesh.triangles = newMesh.triangles;
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}
示例2: JoinMeshes
// todo : make extensdion method
public static Mesh JoinMeshes( Mesh first, Mesh second )
{
int newVertLength = first.vertices.Length + second.vertices.Length;
int newTriLength = first.triangles.Length + second.triangles.Length;
Vector3[] newVerts = new Vector3[ newVertLength ];
Vector2[] newUVs = new Vector2[ newVertLength ];
int[] newTris = new int[ newTriLength ];
for ( int v=0; v<newVertLength; v++ )
{
if ( v == second.vertices.Length ) break;
newVerts[ v ] = v >= first.vertices.Length ? second.vertices[ v ] : first.vertices[ v ];
newUVs[ v ] = v >= first.vertices.Length ? second.uv[ v ] : first.uv[ v ];
}
for ( int t=0; t<newTriLength; t++ )
{
if ( t == second.triangles.Length ) break;
newTris[ t ] = t >= first.triangles.Length ? second.triangles[ t ] : first.triangles[ t ];
}
Mesh newMesh = new Mesh();
newMesh.vertices = newVerts;
newMesh.uv = newUVs;
newMesh.triangles = newTris;
newMesh.RecalculateNormals();
newMesh.RecalculateBounds();
return newMesh;
}
示例3: WriteChunk
protected override void WriteChunk(out byte[] chunkData, Mesh mesh)
{
//mesh.uv1 and mesh.uv2 are the second uv set
var uv1 = mesh.uv;
var uv2 = mesh.uv1;
var chunkLength = uv1.Length*UV_SIZE + uv2.Length*UV_SIZE + HEADER_SIZE;
chunkData = new byte[chunkLength];
var uv1Floats = new float[uv1.Length*2];
var uv2Floats = new float[uv2.Length*2];
for (var i = 0; i < uv1Floats.Length; i += 2)
{
var vert = uv1[i / 2];
uv1Floats[i] = vert.x;
uv1Floats[i + 1] = vert.y;
}
for (var i = 0; i < uv2Floats.Length; i += 2)
{
var vert = uv2[i / 2];
uv2Floats[i] = vert.x;
uv2Floats[i + 1] = vert.y;
}
//do actual writing
//byte counts
var uv1Count = uv1Floats.Length*sizeof (float);
var uv2Count = uv2Floats.Length*sizeof (float);
//header
CopyBytes(uv1Count, chunkData, 0);
CopyBytes(uv2Count, chunkData, sizeof(int));
//data
Buffer.BlockCopy(uv1Floats, 0, chunkData, HEADER_SIZE, uv1Count);
Buffer.BlockCopy(uv2Floats, 0, chunkData, HEADER_SIZE + uv1Count, uv2Count);
}
示例4: ModifyMesh
public override void ModifyMesh(Mesh mesh) {
if (this.IsActive() == false) {
return;
}
var list = new List<UIVertex>();
using (var vertexHelper = new VertexHelper(mesh)) {
vertexHelper.GetUIVertexStream(list);
}
this.ModifyVertices(list); // calls the old ModifyVertices which was used on pre 5.2
using (var vertexHelper = new VertexHelper()) {
vertexHelper.AddUIVertexTriangleStream(list);
vertexHelper.FillMesh(mesh);
}
}
示例5: Cut
/// <summary>
/// cut mesh by plane
/// </summary>
/// <param name="mesh">mesh to cut</param>
/// <param name="meshTransform">transformation of the mesh</param>
/// <param name="plane">cutting plane</param>
/// <param name="triangulateHoles">flag for triangulation of holes</param>
/// <param name="crossSectionVertexColor">this color will be assigned to cross section, valid only for vertex color shaders</param>
/// <param name="crossUV">uv mapping area for cross section</param>
/// <param name="allowOpenMesh">allow cutting of open mesh</param>
/// <returns>processing time</returns>
public float Cut(Mesh mesh, Transform meshTransform, Math.Plane plane, bool triangulateHoles, bool allowOpenMesh, ref List<CutterMesh> meshes,
Color crossSectionVertexColor, Vector4 crossUV)
{
this.crossSectionVertexColour = crossSectionVertexColor;
this.crossSectionUV = crossUV;
return Cut(mesh, meshTransform, plane, triangulateHoles, allowOpenMesh, ref meshes);
}
示例6: ExportToString
public static string ExportToString (Mesh mesh, Renderer renderer, bool uselhcoords = true, bool separateSubmeshes = true){
Material[] mats = renderer.sharedMaterials;
// Initiation
StringBuilder sb = new StringBuilder();
//Header
sb.Append("o ").Append("Plane").Append("\n");
foreach(Vector3 v in mesh.vertices) {
sb.Append(string.Format("v {0:0.000000} {1:0.000000} {2:0.000000}\n",(uselhcoords?-v.x:v.x),v.y,v.z));
}
sb.Append("\n");
foreach(Vector3 v in mesh.normals) {
sb.Append(string.Format("vn {0:0.000000} {1:0.000000} {2:0.000000}\n",v.x,v.y,v.z));
}
sb.Append("\n");
foreach(Vector3 v in mesh.uv) {
sb.Append(string.Format("vt {0:0.000000} {1:0.000000}\n",v.x,v.y));
}
for (int material=0; material < mesh.subMeshCount; material ++) {
sb.Append("\n");
if (separateSubmeshes){
sb.Append("g ").Append(mats[material].name).Append("\n");
}
sb.Append("usemtl ").Append(mats[material].name).Append("\n");
sb.Append("usemap ").Append(mats[material].name).Append("\n");
int[] triangles = mesh.GetTriangles(material);
for (int i=0;i<triangles.Length;i+=3) {
sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
triangles[(uselhcoords?i+1:i)]+1, triangles[(uselhcoords?i:i+1)]+1, triangles[i+2]+1));
}
}
return sb.ToString();
}
示例7: CombineMeshes
public static void CombineMeshes(Queue<CombineInstance> items
, byte area
, InputGeometryCompiler compiler)
{
const int MaxTris = 65000;
List<CombineInstance> combineInstancesPart = new List<CombineInstance>();
byte[] areas = NMGen.CreateAreaBuffer(MaxTris, area);
while (items.Count != 0)
{
int vertCount = 0;
while (items.Count > 0
&& (vertCount + items.Peek().mesh.vertexCount < MaxTris))
{
vertCount += items.Peek().mesh.vertexCount;
combineInstancesPart.Add(items.Dequeue());
}
Mesh meshPart = new Mesh();
meshPart.CombineMeshes(combineInstancesPart.ToArray(), true, true);
compiler.AddTriangles(meshPart.vertices, meshPart.vertexCount
, meshPart.triangles, areas, meshPart.triangles.Length / 3);
Object.DestroyImmediate(meshPart);
combineInstancesPart.Clear();
}
}
示例8: Set
/// <summary>
/// Sets the pivot to a given Vector3 offset from the center of the Mesh bounds.
/// </summary>
public static Vector3 Set (Mesh mesh, Vector3 offset)
{
mesh.RecalculateBounds ();
Vector3 diff = mesh.bounds.center - offset;
PivotTools.Move (mesh, diff);
return diff;
}
示例9: OnPopulateMesh
protected override void OnPopulateMesh(Mesh toFill)
{
Texture mainTexture = this.mainTexture;
if (mainTexture != null)
{
Vector4 zero = Vector4.zero;
int num = Mathf.RoundToInt(mainTexture.width * this.uvRect.width);
int num2 = Mathf.RoundToInt(mainTexture.height * this.uvRect.height);
float num3 = ((num & 1) != 0) ? ((float) (num + 1)) : ((float) num);
float num4 = ((num2 & 1) != 0) ? ((float) (num2 + 1)) : ((float) num2);
zero.x = 0f;
zero.y = 0f;
zero.z = ((float) num) / num3;
zero.w = ((float) num2) / num4;
zero.x -= base.rectTransform.pivot.x;
zero.y -= base.rectTransform.pivot.y;
zero.z -= base.rectTransform.pivot.x;
zero.w -= base.rectTransform.pivot.y;
zero.x *= base.rectTransform.rect.width;
zero.y *= base.rectTransform.rect.height;
zero.z *= base.rectTransform.rect.width;
zero.w *= base.rectTransform.rect.height;
using (VertexHelper helper = new VertexHelper())
{
Color color = base.color;
helper.AddVert(new Vector3(zero.x, zero.y), color, new Vector2(this.m_UVRect.xMin, this.m_UVRect.yMin));
helper.AddVert(new Vector3(zero.x, zero.w), color, new Vector2(this.m_UVRect.xMin, this.m_UVRect.yMax));
helper.AddVert(new Vector3(zero.z, zero.w), color, new Vector2(this.m_UVRect.xMax, this.m_UVRect.yMax));
helper.AddVert(new Vector3(zero.z, zero.y), color, new Vector2(this.m_UVRect.xMax, this.m_UVRect.yMin));
helper.AddTriangle(0, 1, 2);
helper.AddTriangle(2, 3, 0);
helper.FillMesh(toFill);
}
}
}
示例10: ModifyMesh
public override void ModifyMesh(Mesh mesh)
{
if (this.IsActive())
{
List<UIVertex> stream = new List<UIVertex>();
using (VertexHelper helper = new VertexHelper(mesh))
{
helper.GetUIVertexStream(stream);
}
int num = stream.Count * 5;
if (stream.Capacity < num)
{
stream.Capacity = num;
}
int start = 0;
int count = stream.Count;
base.ApplyShadowZeroAlloc(stream, base.effectColor, start, stream.Count, base.effectDistance.x, base.effectDistance.y);
start = count;
count = stream.Count;
base.ApplyShadowZeroAlloc(stream, base.effectColor, start, stream.Count, base.effectDistance.x, -base.effectDistance.y);
start = count;
count = stream.Count;
base.ApplyShadowZeroAlloc(stream, base.effectColor, start, stream.Count, -base.effectDistance.x, base.effectDistance.y);
start = count;
count = stream.Count;
base.ApplyShadowZeroAlloc(stream, base.effectColor, start, stream.Count, -base.effectDistance.x, -base.effectDistance.y);
using (VertexHelper helper2 = new VertexHelper())
{
helper2.AddUIVertexTriangleStream(stream);
helper2.FillMesh(mesh);
}
}
}
示例11: Center
/// <summary>
/// Centers the pivot of the mesh
/// </summary>
/// <returns>
/// The offset the geometry was moved by to center the pivot.
/// </returns>
/// <param name='mesh'>
/// Mesh
/// </param>
public static Vector3 Center (Mesh mesh)
{
mesh.RecalculateBounds ();
Vector3 diff = mesh.bounds.center;
PivotTools.Move (mesh, diff);
return diff;
}
示例12: CreateMesh
public static Mesh CreateMesh(Vector2 dimensions)
{
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[] {
new Vector3(dimensions.x, dimensions.y, 0),
new Vector3(dimensions.x, -dimensions.y, 0),
new Vector3(-dimensions.x, dimensions.y, 0),
new Vector3(-dimensions.x, -dimensions.y, 0),
};
Vector2[] uv = new Vector2[] {
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(0, 0),
};
int[] triangles = new int[] {
0, 1, 2,
2, 1, 3,
};
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
示例13: Export
public static bool Export(string folder, string filename, Mesh mesh, ExportMaterial[] textures, bool copyTextures)
{
exportMesh = mesh;
exportTextures = textures;
targetFolder = folder;
targetName = filename;
_copyTextures = copyTextures;
if(folder.Contains(" "))
{
EditorUtility.DisplayDialog("Filename Error","The filename can't contain spaces","I'm sorry");
return false;
}
if(filename.Contains(" "))
{
EditorUtility.DisplayDialog("Filename Error","The filename can't contain spaces","I'm sorry");
return false;
}
/*if (!CreateTargetFolder())
{
Debug.LogError("There was a problem with the destination folder");
return false;
}*/
MeshToFile(targetFolder, targetName);
exportMesh = null;
exportTextures = null;
return true;
}
示例14: GenerateProportionalUVs
static Vector2[] GenerateProportionalUVs( Vector3[] vertices, Mesh original )
{
Vector2[] result = new Vector2[ vertices.Length ];
int vertexIndexToCalculateDiff = 0;
for ( int i = 1; i < original.vertexCount; i++ ) {
if ( original.vertices[ 0 ].x != original.vertices[ i ].x &&
original.vertices[ 0 ].y != original.vertices[ i ].y ) {
vertexIndexToCalculateDiff = i;
break;
}
}
if ( vertexIndexToCalculateDiff == 0 ) {
throw new System.Exception( "Couldn't find vertexes with different x and y coordinates!" );
}
Vector3 twoFirstVerticesDiff = original.vertices[ vertexIndexToCalculateDiff ] - original.vertices[ 0 ];
Vector2 twoFirstUVsDiff = original.uv[ vertexIndexToCalculateDiff ] - original.uv[ 0 ];
Vector2 distanceToUVMap = new Vector2();
distanceToUVMap.x = twoFirstUVsDiff.x / twoFirstVerticesDiff.x;
distanceToUVMap.y = twoFirstUVsDiff.y / twoFirstVerticesDiff.y;
for ( int i = 0; i < vertices.Length; i++ ) {
result[ i ] = ( vertices[ i ] - original.vertices[ 0 ] );
result[ i ] = new Vector2( result[ i ].x * distanceToUVMap.x,
result[ i ].y * distanceToUVMap.y );
result[ i ] += original.uv[ 0 ];
}
return result;
}
示例15: CreateConvexHull
public static void CreateConvexHull(ConvexHullShape shape, Mesh mesh)
{
ShapeHull hull = new ShapeHull(shape);
hull.BuildHull(shape.Margin);
List<UnityEngine.Vector3> verts = new List<UnityEngine.Vector3>();
List<int> tris = new List<int>();
//int vertexCount = hull.NumVertices;
UIntArray indices = hull.Indices;
Vector3Array points = hull.Vertices;
for (int i = 0; i < indices.Count; i+=3)
{
verts.Add(points[(int)indices[i]].ToUnity());
verts.Add(points[(int)indices[i+1]].ToUnity());
verts.Add(points[(int)indices[i+2]].ToUnity());
tris.Add(i);
tris.Add(i + 1);
tris.Add(i + 2);
}
mesh.vertices = verts.ToArray();
mesh.triangles = tris.ToArray();
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}