本文整理汇总了C#中UnityEngine.Mesh.SetNormals方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.SetNormals方法的具体用法?C# Mesh.SetNormals怎么用?C# Mesh.SetNormals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Mesh
的用法示例。
在下文中一共展示了Mesh.SetNormals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
// Use this for initialization
void Start()
{
var meshMarkers = transform.FindChild("MeshMarkers");
Transform firstChild = null;
var meshFilter = gameObject.AddComponent<MeshFilter>();
Mesh m = new Mesh();
var vertices = new List<Vector3>();
var normals = new List<Vector3>();
foreach (Transform child in meshMarkers) {
if (!firstChild) { firstChild = child;}
vertices.Add(child.position);
normals.Add(new Vector3(0,0,-1));
}
m.SetVertices(vertices);
var indices = new List<int>();
for (int i = 0; i < vertices.Count -2; i++) {
indices.Add(i);
indices.Add(i+1);
indices.Add(i+2);
}
m.SetIndices(indices.ToArray(), MeshTopology.Triangles, 0);
m.SetNormals(normals);
meshFilter.mesh = m;
var pathmarkers = transform.FindChild("PathMarkers");
var tp = from Transform path in pathmarkers select path.position;
var tr = from Transform path in pathmarkers select path.GetComponent<Renderer>();
foreach (var renderer1 in tr) {
renderer1.sortingLayerName = "Middle";
renderer1.material.color = Color.blue;
}
//tracePosList = Curver.MakeSmoothCurve(tp.ToArray(), 6);
tracePosList = tp.ToArray();
GetComponent<MeshRenderer>().sortingLayerName = "Background";
line = new GameObject().AddComponent<LineRenderer>();
line.transform.parent = transform;
line.SetWidth(0.5f,0.5f);
line.material = mat;
line.SetColors(Color.black, Color.black);
trace = new GameObject().AddComponent<LineRenderer>();
trace.transform.parent = transform;
trace.SetWidth(0.5f, 0.5f);
trace.material = mat;
trace.SetColors(Color.red, Color.red);
marble.transform.position = tracePosList[0];
marble.GetComponent<Renderer>().sortingLayerName = "Marble";
marble.GetComponent<MeshRenderer>().material.color = Color.yellow;
}
示例2: MakeStripped
public static Mesh MakeStripped(List<Vector3> vertices, List<Vector3> normals = null, List<Vector2> uvs = null, List<Color32> colors = null, bool isBacksided = false)
{
Mesh mesh = new Mesh();
mesh.SetVertices(vertices);
if (normals != null)
{
mesh.SetNormals(normals);
}
if (uvs != null)
{
mesh.SetUVs(0, uvs);
}
if (colors != null)
{
mesh.SetColors(colors);
}
List<int> _tris = new List<int>();
for (int i = 1; i < vertices.Count - 1; i += 2)
{
_tris.Add(i);
_tris.Add(i - 1);
_tris.Add(i + 1);
if (isBacksided)
{
_tris.Add(i + 1);
_tris.Add(i - 1);
_tris.Add(i);
}
if (i + 2 < vertices.Count)
{
_tris.Add(i);
_tris.Add(i + 1);
_tris.Add(i + 2);
if (isBacksided)
{
_tris.Add(i + 2);
_tris.Add(i + 1);
_tris.Add(i);
}
}
}
mesh.SetTriangles(_tris, 0);
return mesh;
}
示例3: MakePrism
public static Mesh MakePrism()
{
var prism = new Mesh();
const int count = 3;
var verts = new Vector3[count * 2];
var indices = new List<int>(3 * count + 6); // inc caps
var normals = new List<Vector3>(count * 2);
for (int i = 0; i < count; i++)
{
float step = Mathf.PI * 2 / count;
verts[i * 2 + 0] = new Vector3(Mathf.Cos(step * i), -0.5f, Mathf.Sin(step * i));
verts[i * 2 + 1] = new Vector3(Mathf.Cos(step * i), +0.5f, Mathf.Sin(step * i));
normals.Add(verts[i * 2 + 0].normalized);
normals.Add(verts[i * 2 + 1].normalized);
indices.Add((i * 2 + 2) % (count * 2));
indices.Add((i * 2 + 1) % (count * 2));
indices.Add((i * 2 + 0) % (count * 2));
indices.Add((i * 2 + 1) % (count * 2));
indices.Add((i * 2 + 2) % (count * 2));
indices.Add((i * 2 + 3) % (count * 2));
}
// The cap depends on the fat that is is a triangular prism.
indices.AddRange(new int[] { 1, 3, 5 });
indices.AddRange(new int[] { 4, 2, 0 });
indices.Reverse();
prism.vertices = verts;
prism.SetNormals(normals);
prism.SetIndices(indices.ToArray(), MeshTopology.Triangles, 0);
return prism;
}
示例4: BakeMesh
private Mesh BakeMesh(InstanceData[] instances)
{
if (meshFilter == null)
{
meshFilter = GetComponent<MeshFilter>();
}
Mesh mesh = new Mesh();
var meshVertices = meshFilter.sharedMesh.vertices;
var meshUVs = meshFilter.sharedMesh.uv;
var indices = meshFilter.sharedMesh.GetIndices(0);
var normals = meshFilter.sharedMesh.normals;
var vertexData = new List<Vector3>();
var normalData = new List<Vector3>();
var uv0Data = new List<Vector2>();
for (int instanceIdx = 0; instanceIdx < instances.Length; instanceIdx++)
{
var scale = Vector3.Lerp(minScale, maxScale, UnityEngine.Random.value);
var cur = instances[instanceIdx];
var instancePos = new Vector3(Mathf.Cos(cur.startAngle), cur.verticalOffset, Mathf.Sin(cur.startAngle)) * cur.ringRadius;
var instanceRotation = Quaternion.AngleAxis(UnityEngine.Random.value * 360, UnityEngine.Random.insideUnitSphere);
vertexData.AddRange(meshVertices.Select(v => instancePos + instanceRotation * (new Vector3(v.x * scale.x, v.y * scale.y, v.z * scale.z) * meshScale)));
normalData.AddRange(normals.Select(v => Vector3.Normalize(instanceRotation * new Vector3(v.x * scale.x, v.y * scale.y, v.z * scale.z) * meshScale)));
uv0Data.AddRange(meshUVs);
}
mesh.SetVertices(vertexData);
mesh.SetIndices(Enumerable.Range(0, instances.Length).SelectMany(i => indices.Select(vi => i * meshVertices.Length + vi)).ToArray(), MeshTopology.Triangles, 0);
mesh.SetUVs(0, uv0Data);
mesh.SetNormals(normalData);
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}
示例5: FillMesh
protected void FillMesh(Mesh mesh)
{
mesh.Clear();
if (positions.Count > 65000)
throw new System.ArgumentException("Mesh cannot have more than 65000 vertices."); // Limitation based on UnityEngine.UI.VertexHelper
mesh.SetVertices(positions);
//mesh.SetColors(colors); // 5.3 mesh.SetColors(Color) is broken in UI.
mesh.SetColors(colors32);
mesh.SetUVs(0, uvs);
mesh.SetNormals(normals);
mesh.SetTriangles(indices, 0);
mesh.RecalculateBounds();
}
示例6: FillMesh
/// <summary>
/// <para>Fill the given mesh with the stream data.</para>
/// </summary>
/// <param name="mesh"></param>
public void FillMesh(Mesh mesh)
{
mesh.Clear();
if (this.m_Positions.Count >= 0xfde8)
{
throw new ArgumentException("Mesh can not have more than 65000 vertices");
}
mesh.SetVertices(this.m_Positions);
mesh.SetColors(this.m_Colors);
mesh.SetUVs(0, this.m_Uv0S);
mesh.SetUVs(1, this.m_Uv1S);
mesh.SetNormals(this.m_Normals);
mesh.SetTangents(this.m_Tangents);
mesh.SetTriangles(this.m_Indices, 0);
mesh.RecalculateBounds();
}
示例7: CreateNavmeshOutlineVisualization
//.........这里部分代码省略.........
// Loop throgh neighbours to figure
// out which edges are shared
if (other != null && other.GraphIndex == node.GraphIndex) {
for (int v = 0; v < 3; v++) {
for (int v2 = 0; v2 < 3; v2++) {
if (node.GetVertexIndex(v) == other.GetVertexIndex((v2+1)%3) && node.GetVertexIndex((v+1)%3) == other.GetVertexIndex(v2)) {
// Found a shared edge with the other node
sharedEdges[v] = true;
v = 3;
break;
}
}
}
}
}
for (int v = 0; v < 3; v++) {
if (!sharedEdges[v]) {
edgeList.Add(node.GetVertex(v));
edgeList.Add(node.GetVertex((v+1)%3));
var color = (Color32)AstarColor.GetAreaColor(node.Area);
colorList.Add(color);
colorList.Add(color);
}
}
}
// Use pooled lists to avoid excessive allocations
var vertices = ListPool<Vector3>.Claim(edgeList.Count*2);
var colors = ListPool<Color32>.Claim(edgeList.Count*2);
var normals = ListPool<Vector3>.Claim(edgeList.Count*2);
var tris = ListPool<int>.Claim(edgeList.Count*3);
// Loop through each endpoint of the lines
// and add 2 vertices for each
for (int j = 0; j < edgeList.Count; j++) {
var vertex = (Vector3)edgeList[j];
vertices.Add(vertex);
vertices.Add(vertex);
// Encode the side of the line
// in the alpha component
var color = colorList[j];
colors.Add(new Color32(color.r, color.g, color.b, 0));
colors.Add(new Color32(color.r, color.g, color.b, 255));
}
// Loop through each line and add
// one normal for each vertex
for (int j = 0; j < edgeList.Count; j += 2) {
var lineDir = (Vector3)(edgeList[j+1] - edgeList[j]);
lineDir.Normalize();
// Store the line direction in the normals
// A line consists of 4 vertices
// The line direction will be used to
// offset the vertices to create a
// line with a fixed pixel thickness
normals.Add(lineDir);
normals.Add(lineDir);
normals.Add(lineDir);
normals.Add(lineDir);
}
// Setup triangle indices
// A triangle consists of 3 indices
// A line (4 vertices) consists of 2 triangles, so 6 triangle indices
for (int j = 0, v = 0; j < edgeList.Count*3; j += 6, v += 4) {
// First triangle
tris.Add(v+0);
tris.Add(v+1);
tris.Add(v+2);
// Second triangle
tris.Add(v+1);
tris.Add(v+3);
tris.Add(v+2);
}
// Set all data on the mesh
mesh.SetVertices(vertices);
mesh.SetTriangles(tris, 0);
mesh.SetColors(colors);
mesh.SetNormals(normals);
// Upload all data and mark the mesh as unreadable
mesh.UploadMeshData(true);
// Release the lists back to the pool
ListPool<Color32>.Release(colorList);
ListPool<Int3>.Release(edgeList);
ListPool<Vector3>.Release(vertices);
ListPool<Color32>.Release(colors);
ListPool<Vector3>.Release(normals);
ListPool<int>.Release(tris);
return mesh;
}
示例8: ToMesh
/// <summary>
/// Creates new mesh from information in draft
/// </summary>
public Mesh ToMesh()
{
var mesh = new Mesh {name = name};
mesh.SetVertices(vertices);
mesh.SetTriangles(triangles, 0);
mesh.SetNormals(normals);
mesh.SetUVs(0, uv);
mesh.SetColors(colors);
return mesh;
}
示例9: SetVertices
public void SetVertices(UIVertex[] vertices, int size)
{
Mesh mesh = new Mesh();
List<Vector3> inVertices = new List<Vector3>();
List<Color32> inColors = new List<Color32>();
List<Vector2> uvs = new List<Vector2>();
List<Vector2> list4 = new List<Vector2>();
List<Vector3> inNormals = new List<Vector3>();
List<Vector4> inTangents = new List<Vector4>();
List<int> list7 = new List<int>();
for (int i = 0; i < size; i += 4)
{
for (int j = 0; j < 4; j++)
{
inVertices.Add(vertices[i + j].position);
inColors.Add(vertices[i + j].color);
uvs.Add(vertices[i + j].uv0);
list4.Add(vertices[i + j].uv1);
inNormals.Add(vertices[i + j].normal);
inTangents.Add(vertices[i + j].tangent);
}
list7.Add(i);
list7.Add(i + 1);
list7.Add(i + 2);
list7.Add(i + 2);
list7.Add(i + 3);
list7.Add(i);
}
mesh.SetVertices(inVertices);
mesh.SetColors(inColors);
mesh.SetNormals(inNormals);
mesh.SetTangents(inTangents);
mesh.SetUVs(0, uvs);
mesh.SetUVs(1, list4);
mesh.SetIndices(list7.ToArray(), MeshTopology.Triangles, 0);
this.SetMesh(mesh);
UnityEngine.Object.DestroyImmediate(mesh);
}
示例10: SetVertices
public void SetVertices(UIVertex[] vertices, int size)
{
Mesh mesh = new Mesh();
List<Vector3> inVertices = new List<Vector3>();
List<Color32> inColors = new List<Color32>();
List<Vector2> uvs1 = new List<Vector2>();
List<Vector2> uvs2 = new List<Vector2>();
List<Vector3> inNormals = new List<Vector3>();
List<Vector4> inTangents = new List<Vector4>();
List<int> intList = new List<int>();
int num = 0;
while (num < size)
{
for (int index = 0; index < 4; ++index)
{
inVertices.Add(vertices[num + index].position);
inColors.Add(vertices[num + index].color);
uvs1.Add(vertices[num + index].uv0);
uvs2.Add(vertices[num + index].uv1);
inNormals.Add(vertices[num + index].normal);
inTangents.Add(vertices[num + index].tangent);
}
intList.Add(num);
intList.Add(num + 1);
intList.Add(num + 2);
intList.Add(num + 2);
intList.Add(num + 3);
intList.Add(num);
num += 4;
}
mesh.SetVertices(inVertices);
mesh.SetColors(inColors);
mesh.SetNormals(inNormals);
mesh.SetTangents(inTangents);
mesh.SetUVs(0, uvs1);
mesh.SetUVs(1, uvs2);
mesh.SetIndices(intList.ToArray(), MeshTopology.Triangles, 0);
this.SetMesh(mesh);
Object.DestroyImmediate((Object) mesh);
}
示例11: FillMesh
public void FillMesh(Mesh mesh)
{
mesh.Clear();
mesh.SetVertices(this.m_Positions);
mesh.SetColors(this.m_Colors);
mesh.SetUVs(0, this.m_Uv0S);
mesh.SetUVs(1, this.m_Uv1S);
mesh.SetNormals(this.m_Normals);
mesh.SetTangents(this.m_Tangents);
mesh.SetTriangles(this.m_Indicies, 0);
mesh.RecalculateBounds();
}
示例12: ProcessChunk
public void ProcessChunk(ByteData data, Mesh mesh)
{
if (lookup == null || lookup.GetLength(0) < data.Length*2 + 3)
{
lookup = new IntInt[data.Length*2 + 3, data.Length*2 + 3, data.Length*2 + 3];
}
lookupVersion++;
vertices.Clear();
normals.Clear();
triangles.Clear();
colors.Clear();
counters.Clear();
for (int x = -1; x < data.Length; x++)
{
for (int y = -1; y < data.Length; y++)
{
for (int z = -1; z < data.Length; z++)
{
int configuration = 0;
byte target = 1;
//for (int i = 0; i < 8; i++)
//{
// byte val = data[x + vertexOffset[i, 0], y + vertexOffset[i, 1], z + vertexOffset[i, 2]];
// if (val > target)
// {
// target = val;
// }
//}
int blend = 1;
var color = new Color32();
for (int i = 0; i < 8; i++)
{
byte val = data[x + vertexOffset[i, 0], y + vertexOffset[i, 1], z + vertexOffset[i, 2]];
if (val <= target)
{
configuration |= 1 << i;
}
if (val > target)
{
color = Color32.Lerp(color, colorMap[val], 1f/blend);
blend++;
}
}
if (configuration > 0 && configuration < 255)
{
ProcessCube((byte) configuration, new Vector3(x, y, z), color, x < 0 || y < 0 || z < 0);
}
}
}
}
mesh.Clear();
mesh.SetVertices(vertices);
if (SMOOTH)
{
for (int i = 0; i < vertices.Count; i++)
{
normals.Add(compositeNormals[i].Normal);
}
}
mesh.SetNormals(normals);
mesh.SetColors(colors);
mesh.SetTriangles(triangles, 0);
}
示例13: Update
//.........这里部分代码省略.........
// Ensure ordering
if (Vector3.Dot(Vector3.Cross(verts[i1] - verts[i0], verts[i2] - verts[i0]), vert0) < 0f)
{
int tmp = i1;
i1 = i3;
i3 = tmp;
}
edgeTris.AddRange(new int[] { i0, i1, i2, i0, i2, i3 });
// Normal
Vector3 normal = 0.5f * (vert0 + vert1);
normal.Normalize();
normals.AddRange(new Vector3[] { normal, normal, normal, normal });
// The UVs encode the sampling plane normals.
Vector2 uv0 = encodeNormalToUV(midPoint0.normalized);
Vector2 uv1 = encodeNormalToUV(midPoint1.normalized);
uvNormal0.AddRange(new Vector2[] { uv0, uv0, uv0, uv0 });
uvNormal1.AddRange(new Vector2[] { uv1, uv1, uv1, uv1 });
uvNormal2.AddRange(new Vector2[] { Vector2.zero, Vector2.zero, Vector2.zero, Vector2.zero });
// Blend weights are stored in the color channel
Color c0 = new Color(1f, 0f, 0f);
Color c1 = new Color(0f, 1f, 0f);
colors.AddRange(new Color[] { c0, c1, c1, c0 });
}
// Compute the bevelled vertices.
// Those have three blend weights.
List<int> vertTris = new List<int>(20 * 3);
for (int i=0; i<20; i++)
{
Vector3 normal = rawVerts[i].normalized;
// The UVs encode the sampling plane normals.
Vector2 uv0 = encodeNormalToUV(faceMidPoints[vertFaceIndices[i, 0]].normalized);
Vector2 uv1 = encodeNormalToUV(faceMidPoints[vertFaceIndices[i, 1]].normalized);
Vector2 uv2 = encodeNormalToUV(faceMidPoints[vertFaceIndices[i, 2]].normalized);
for (int j=0; j<3; j++)
{
int faceIdx = vertFaceIndices[i, j];
Vector3 faceMidPoint = faceMidPoints[faceIdx];
Vector3 beveledVertexPosition = faceMidPoint + (1f - bevel) * (rawVerts[i] - faceMidPoint);
verts.Add(beveledVertexPosition);
normals.Add(normal);
uvNormal0.Add(uv0);
uvNormal1.Add(uv1);
uvNormal2.Add(uv2);
}
// Blend weights are stored in the color channel.
colors.Add(new Color(1f, 0f, 0f));
colors.Add(new Color(0f, 1f, 0f));
colors.Add(new Color(0f, 0f, 1f));
// Indices
int i0 = verts.Count - 3;
int i1 = verts.Count - 2;
int i2 = verts.Count - 1;
// Ensure ordering
if (Vector3.Dot (Vector3.Cross(verts[i1] - verts[i0], verts[i2] - verts[i0]), rawVerts[i]) < 0)
{
int tmp = i1;
i1 = i2;
i2 = tmp;
}
vertTris.AddRange(new int[] { i0, i1, i2 });
}
// Finish setting up the mesh.
mesh.SetVertices(verts);
mesh.SetNormals(normals);
mesh.SetColors(colors);
mesh.SetUVs(0, uvNormal0);
mesh.SetUVs(1, uvNormal1);
mesh.SetUVs(2, uvNormal2);
// There are three sub-meshes: Face faces, edge faces, and vertex faces.
mesh.subMeshCount = 3;
mesh.SetTriangles(faceTris, 0);
mesh.SetTriangles(edgeTris, 1);
mesh.SetTriangles(vertTris, 2);
// If there is a mesh filter component, apply the mesh.
var meshFilter = GetComponent<MeshFilter>();
if (meshFilter)
meshFilter.sharedMesh = mesh;
}
}
示例14: Build
/// <summary>
/// Clears out the mesh, fills in the data, and recalculates normals and bounds.
/// </summary>
/// <param name="aMesh">An already existing mesh to fill out.</param>
public void Build(ref Mesh aMesh, bool aCalculateTangents)
{
// round off a few decimal points to try and get better pixel-perfect results
for(int i=0;i<mVerts.Count;i+=1) mVerts[i] = new Vector3(
(float)System.Math.Round(mVerts[i].x, 3),
(float)System.Math.Round(mVerts[i].y, 3),
(float)System.Math.Round(mVerts[i].z, 3));
aMesh.Clear();
aMesh.vertices = mVerts .ToArray();
aMesh.uv = mUVs .ToArray();
aMesh.triangles = mIndices.ToArray();
aMesh.colors = mColors .ToArray();
aMesh.normals = mNorms .ToArray();
aMesh.tangents = mTans .ToArray();
if (mUV2s != null) {
aMesh.uv2 = mUV2s.ToArray();
}
if (mNorms.Count == 0) aMesh.RecalculateNormals();
if (aCalculateTangents && mTans.Count == 0) {
RecalculateTangents(aMesh);
} else {
aMesh.tangents = null;
}
aMesh.RecalculateBounds();
for (int n = mNorms.Count; n < mVerts.Count; ++n)
{
mNorms.Add(Vector3.zero);
}
for (int n = 0; n < aMesh.vertices.Length; n += 3) {
if (n + 3 >= aMesh.vertices.Length) break;
mNorms[n] = new Vector3(1, 0, 0);
mNorms[n + 1] = new Vector3(0, 1, 0);
mNorms[n + 2] = new Vector3(0, 0, 1);
continue;
Vector3 v1 = aMesh.vertices[n];
Vector3 v2 = aMesh.vertices[n % 3 == 2 ? n - 2 : n + 1];
float dx = v2.x - v1.x;
float dy = v2.y - v1.y;
Vector2 target = new Vector3(-dy, dx, 0);
mNorms[n] = target;
float angle = Mathf.Atan2(target.y, target.x) * (180.0f / Mathf.PI) + 90;
if (angle < 70 || angle > 120) mNorms[n] = Vector3.zero;
float angle2 = Mathf.Atan2(dy, dx) * (180.0f / Mathf.PI) + 90;
if (n < 3) Debug.Log(mNorms[n] + ", " + angle + ", " + angle2);
}
aMesh.SetNormals(mNorms);
}
示例15: MakeIndexedStrip
public static Mesh MakeIndexedStrip(List<Vector3> vertices, List<short[]> indices, List<Vector3> normals = null, List<Vector2> uvs = null, List<Color32> colors = null, bool isBacksided = false)
{
Mesh mesh = new Mesh();
mesh.SetVertices(vertices);
if (normals != null)
{
mesh.SetNormals(normals);
}
if (uvs != null)
{
mesh.SetUVs(0, uvs);
}
if (colors != null && colors.Count > 0)
{
mesh.SetColors(colors);
}
mesh.subMeshCount = indices.Count;
for (int j = 0; j != indices.Count; j++)
{
List<int> _tris = new List<int>();
for (int i = 1; i < indices[j].Length - 1; i += 2)
{
_tris.Add(indices[j][i]);
_tris.Add(indices[j][i - 1]);
_tris.Add(indices[j][i + 1]);
if (i + 2 < indices[j].Length)
{
_tris.Add(indices[j][i]);
_tris.Add(indices[j][i + 1]);
_tris.Add(indices[j][i + 2]);
}
}
mesh.SetTriangles(_tris, j);
}
return mesh;
}