本文整理汇总了C#中UnityEngine.Mesh.UploadMeshData方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.UploadMeshData方法的具体用法?C# Mesh.UploadMeshData怎么用?C# Mesh.UploadMeshData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Mesh
的用法示例。
在下文中一共展示了Mesh.UploadMeshData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MergeMeshes
public static GameObject MergeMeshes(PaintJob[] jobs)
{
if (jobs.Length == 0)
return null;
List<CombineInstance> meshes = new List<CombineInstance>();
for (int i = 0; i < jobs.Length; ++i)
{
Mesh m = BakeDownMesh(jobs[i].meshFilter.sharedMesh, jobs[i].stream);
CombineInstance ci = new CombineInstance();
ci.mesh = m;
ci.transform = jobs[i].meshFilter.transform.localToWorldMatrix;
meshes.Add(ci);
}
Mesh mesh = new Mesh();
mesh.CombineMeshes(meshes.ToArray());
GameObject go = new GameObject("Combined Mesh");
go.AddComponent<MeshRenderer>();
var mf = go.AddComponent<MeshFilter>();
mesh.Optimize();
mesh.RecalculateBounds();
mesh.UploadMeshData(false);
mf.sharedMesh = mesh;
for (int i = 0; i < meshes.Count; ++i)
{
GameObject.DestroyImmediate(meshes[i].mesh);
}
return go;
}
示例2: Start
void Start()
{
RenderSettings.fogColor = new Color(0, 0, 0);
var vertices = new Vector3[POINT_MAX];
{
for (var fstep = 0; fstep < FORWARD_STEP; ++fstep) {
for (var tstep = 0; tstep < THETA_STEP+1; ++tstep) {
int i = fstep * (THETA_STEP+1) + tstep;
vertices[i].x = Mathf.Cos((float)tstep/(float)THETA_STEP * Mathf.PI * 2f) * RADIUS;
vertices[i].y = Mathf.Sin((float)tstep/(float)THETA_STEP * Mathf.PI * 2f) * RADIUS;
vertices[i].z = (float)fstep/(float)FORWARD_STEP * LENGTH;
}
}
}
var triangles = new int[(FORWARD_STEP-1)*THETA_STEP * 6];
{
int i = 0;
for (int y = 0; y < FORWARD_STEP-1; ++y) {
for (int x = 0; x < THETA_STEP; ++x) {
triangles[i] = (y + 0) * (THETA_STEP+1) + x + 0; ++i;
triangles[i] = (y + 1) * (THETA_STEP+1) + x + 0; ++i;
triangles[i] = (y + 0) * (THETA_STEP+1) + x + 1; ++i;
triangles[i] = (y + 1) * (THETA_STEP+1) + x + 0; ++i;
triangles[i] = (y + 1) * (THETA_STEP+1) + x + 1; ++i;
triangles[i] = (y + 0) * (THETA_STEP+1) + x + 1; ++i;
}
}
}
var uvs = new Vector2[POINT_MAX];
{
int i = 0;
for (int y = 0; y < FORWARD_STEP; ++y) {
for (int x = 0; x < THETA_STEP+1; ++x) {
uvs[i].x = (float)x/(float)THETA_STEP * REPEAT;
uvs[i].y = (float)y/(float)FORWARD_STEP * REPEAT;
++i;
}
}
}
var mesh = new Mesh ();
mesh.name = "tube_unit";
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.UploadMeshData(true /* markNoLogerReadable */);
var mf = GetComponent<MeshFilter> ();
mf.sharedMesh = mesh;
}
示例3: GetMesh
public Mesh GetMesh()
{
Mesh m = new Mesh();
m.vertices = vertices.ToArray();
m.triangles = triangles.ToArray();
m.UploadMeshData(false);
m.RecalculateNormals();
m.RecalculateBounds();
return m;
}
示例4: Awake
void Awake()
{
Debug.Log("awake");
filter = gameObject.AddComponent<MeshFilter>();
mesh = CreateGridMesh(xCount, zCount, Vector3.zero);
cld = gameObject.AddComponent<MeshCollider>();
cld.mesh = mesh;
mesh.UploadMeshData(false);
mesh.RecalculateBounds();
filter.mesh = mesh;
MeshRenderer rd = gameObject.AddComponent<MeshRenderer>();
}
示例5: GetPrimitiveMesh
protected static Mesh GetPrimitiveMesh(PrimitiveType type)
{
GameObject plane = GameObject.CreatePrimitive(type);
plane.hideFlags = HideFlags.HideAndDontSave;
Mesh srcMesh = plane.GetComponent<MeshFilter>().sharedMesh;
Mesh mesh = new Mesh();
mesh.vertices = srcMesh.vertices;
mesh.normals = srcMesh.normals;
mesh.triangles = srcMesh.triangles;
mesh.uv = srcMesh.uv;
mesh.UploadMeshData(true);
GameObject.DestroyImmediate(plane);
if(!defaultMaterial)
defaultMaterial = new Material(Shader.Find("Mobile/Diffuse"));
return mesh;
}
示例6: OnEnable
public virtual void OnEnable()
{
m_trans = GetComponent<Transform>();
if (m_materials==null || m_materials.Length==0)
{
m_materials = new Material[1] { m_material };
}
m_actual_materials = new List<List<Material>>();
while (m_actual_materials.Count < m_materials.Length)
{
m_actual_materials.Add(new List<Material>());
}
if (m_expanded_mesh == null && m_mesh != null)
{
m_expanded_mesh = BatchRendererUtil.CreateExpandedMesh(m_mesh, out m_instances_par_batch);
m_expanded_mesh.UploadMeshData(true);
}
int layer_mask = m_layer_selector.value;
for (int i = 0; i < 32; ++i )
{
if ((layer_mask & (1<<i)) != 0)
{
m_layer = i;
m_layer_selector.value = 1 << i;
break;
}
}
}
示例7: generateMeshes
private void generateMeshes() {
_cubeMesh = new Mesh();
_cubeMesh.name = "RuntimeGizmoCube";
_cubeMesh.hideFlags = HideFlags.HideAndDontSave;
List<Vector3> verts = new List<Vector3>();
List<int> indexes = new List<int>();
Vector3[] faces = new Vector3[] { Vector3.forward, Vector3.right, Vector3.up };
for (int i = 0; i < 3; i++) {
addQuad(verts, indexes, faces[(i + 0) % 3], -faces[(i + 1) % 3], faces[(i + 2) % 3]);
addQuad(verts, indexes, -faces[(i + 0) % 3], faces[(i + 1) % 3], faces[(i + 2) % 3]);
}
_cubeMesh.SetVertices(verts);
_cubeMesh.SetIndices(indexes.ToArray(), MeshTopology.Quads, 0);
_cubeMesh.RecalculateNormals();
_cubeMesh.RecalculateBounds();
_cubeMesh.UploadMeshData(true);
_wireCubeMesh = new Mesh();
_wireCubeMesh.name = "RuntimeWireCubeMesh";
_wireCubeMesh.hideFlags = HideFlags.HideAndDontSave;
verts.Clear();
indexes.Clear();
for (int dx = 1; dx >= -1; dx -= 2) {
for (int dy = 1; dy >= -1; dy -= 2) {
for (int dz = 1; dz >= -1; dz -= 2) {
verts.Add(0.5f * new Vector3(dx, dy, dz));
}
}
}
addCorner(indexes, 0, 1, 2, 4);
addCorner(indexes, 3, 1, 2, 7);
addCorner(indexes, 5, 1, 4, 7);
addCorner(indexes, 6, 2, 4, 7);
_wireCubeMesh.SetVertices(verts);
_wireCubeMesh.SetIndices(indexes.ToArray(), MeshTopology.Lines, 0);
_wireCubeMesh.RecalculateBounds();
_wireCubeMesh.UploadMeshData(true);
_wireSphereMesh = new Mesh();
_wireSphereMesh.name = "RuntimeWireSphereMesh";
_wireSphereMesh.hideFlags = HideFlags.HideAndDontSave;
verts.Clear();
indexes.Clear();
int totalVerts = CIRCLE_RESOLUTION * 3;
for (int i = 0; i < CIRCLE_RESOLUTION; i++) {
float angle = Mathf.PI * 2 * i / CIRCLE_RESOLUTION;
float dx = 0.5f * Mathf.Cos(angle);
float dy = 0.5f * Mathf.Sin(angle);
for (int j = 0; j < 3; j++) {
indexes.Add((i * 3 + j + 0) % totalVerts);
indexes.Add((i * 3 + j + 3) % totalVerts);
}
verts.Add(new Vector3(dx, dy, 0));
verts.Add(new Vector3(0, dx, dy));
verts.Add(new Vector3(dx, 0, dy));
}
_wireSphereMesh.SetVertices(verts);
_wireSphereMesh.SetIndices(indexes.ToArray(), MeshTopology.Lines, 0);
_wireSphereMesh.RecalculateBounds();
_wireSphereMesh.UploadMeshData(true);
}
示例8: RebuildDistortionMesh
private void RebuildDistortionMesh() {
distortionMesh = new Mesh();
Vector3[] vertices;
Vector2[] tex;
ComputeMeshPoints(kMeshWidth, kMeshHeight, kDistortVertices, out vertices, out tex);
int[] indices = ComputeMeshIndices(kMeshWidth, kMeshHeight, kDistortVertices);
Color[] colors = ComputeMeshColors(kMeshWidth, kMeshHeight, tex, indices, kDistortVertices);
distortionMesh.vertices = vertices;
distortionMesh.uv = tex;
distortionMesh.colors = colors;
distortionMesh.triangles = indices;
distortionMesh.Optimize();
distortionMesh.UploadMeshData(true);
}
示例9: MirrorMesh
private void MirrorMesh( MeshFilter meshFilter )
{
Mesh srcMesh = meshFilter.sharedMesh;
if( srcMesh == null )
return;
Mesh dstMesh = new Mesh();
dstMesh.vertices = MirrorArray( srcMesh.vertices );
dstMesh.triangles = MirrorTriangles( srcMesh.triangles );
dstMesh.uv = srcMesh.uv;
dstMesh.uv2 = srcMesh.uv2;
dstMesh.normals = MirrorArray( srcMesh.normals );
dstMesh.colors = srcMesh.colors;
dstMesh.tangents = srcMesh.tangents;
dstMesh.UploadMeshData( true );
meshFilter.sharedMesh = dstMesh;
}
示例10: 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;
}
示例11: GenerateMesh
//.........这里部分代码省略.........
meshPoses[meshPoses.Length - 2] = c.GetValue(0.0f, cV);
meshPoses[meshPoses.Length - 1] = c.GetValue(1.0f, cV);
meshUVs[meshUVs.Length - 2] = new Vector2(0.5f, 1.0f);
meshUVs[meshUVs.Length - 1] = new Vector2(0.5f, 0.0f);
//At each division along the curve, generate a ring of vertices.
float rotateIncrement = 360.0f / divisionsAround;
float moveIncrement = 1.0f / (float)(divisionsAlong - 1);
for (int i = 0; i < divisionsAlong; ++i)
{
float t = moveIncrement * (float)i;
var valAndDerivative = c.GetValueAndDerivative(t, cV);
valAndDerivative.Derivative.Normalize();
Vector3 perp = valAndDerivative.Perpendicular.normalized;
Quaternion rot = Quaternion.AngleAxis(rotateIncrement, valAndDerivative.Derivative);
float variance = radiusVariance.Evaluate(t),
radiusBase = radiusAlongCurve.Evaluate(t);
for (int j = 0; j < divisionsAround; ++j)
{
float jLerp = (float)j / (float)(divisionsAround - 1);
float radius = radiusAroundCurve.Evaluate(jLerp) *
radiusScale *
(radiusBase + Rand.Range(-variance, variance));
int index = j + (i * divisionsAround);
meshPoses[index] = valAndDerivative.Value + (perp * radius);
meshUVs[index] = new Vector2((float)j / (float)(divisionsAround + 1),
(float)i / (float)divisionsAlong);
perp = rot * perp;
}
}
//Next, generate indices.
int indicesPerRing = divisionsAround * 2 * 3,
indicesPerCap = divisionsAround * 3;
int[] meshTris = new int[(indicesPerRing * (divisionsAlong - 1)) +
(indicesPerCap * 2)];
//Generate indices along the curve.
int triIndex = 0;
for (int i = 1; i < divisionsAlong; ++i)
{
int prevStartVertex = (i - 1) * divisionsAround,
startVertex = i * divisionsAround;
for (int j = 0; j < divisionsAround; ++j)
{
int nextJ = (j + 1) % divisionsAround;
meshTris[triIndex] = prevStartVertex + j;
meshTris[triIndex + 1] = startVertex + nextJ;
meshTris[triIndex + 2] = startVertex + j;
meshTris[triIndex + 3] = prevStartVertex + j;
meshTris[triIndex + 4] = prevStartVertex + nextJ;
meshTris[triIndex + 5] = startVertex + nextJ;
triIndex += 6;
}
}
//Generate indices for the end caps of the curve.
int topStartVert = (divisionsAlong - 1) * divisionsAround;
for (int i = 0; i < divisionsAround; ++i)
{
int nextI = (i + 1) % divisionsAround;
meshTris[triIndex] = nextI;
meshTris[triIndex + 1] = i;
meshTris[triIndex + 2] = meshPoses.Length - 2;
meshTris[triIndex + indicesPerCap] = topStartVert + i;
meshTris[triIndex + indicesPerCap + 1] = topStartVert + nextI;
meshTris[triIndex + indicesPerCap + 2] = meshPoses.Length - 1;
triIndex += 3;
}
//Finally, generate and return the mesh object.
m.Clear();
m.vertices = meshPoses;
m.uv = meshUVs;
m.triangles = meshTris;
m.RecalculateBounds();
m.RecalculateNormals();
CalculateMeshTangents(m);
m.UploadMeshData(true);
}
示例12: CreateOrDestroyDebugVisualization
private void CreateOrDestroyDebugVisualization()
{
if (!Application.isPlaying)
{
return;
}
if (enabled && drawDebugVisualization && debugVisualizationQuad == null)
{
var mesh = new Mesh
{
vertices =
new[]
{
new Vector3(-0.5f, 0.9f, 1.0f),
new Vector3(-0.5f, 1.0f, 1.0f),
new Vector3(0.5f, 1.0f, 1.0f),
new Vector3(0.5f, 0.9f, 1.0f)
},
uv =
new[]
{
new Vector2(0.0f, 0.0f),
new Vector2(0.0f, 1.0f),
new Vector2(1.0f, 1.0f),
new Vector2(1.0f, 0.0f)
},
triangles = new[] { 0, 1, 2, 0, 2, 3 }
};
mesh.Optimize();
mesh.UploadMeshData(true);
debugVisualizationQuad = new GameObject("AdaptiveQualityDebugVisualizationQuad");
debugVisualizationQuad.transform.parent = VRTK_DeviceFinder.HeadsetTransform();
debugVisualizationQuad.transform.localPosition = Vector3.forward;
debugVisualizationQuad.transform.localRotation = Quaternion.identity;
debugVisualizationQuad.AddComponent<MeshFilter>().mesh = mesh;
debugVisualizationQuadMaterial = Resources.Load<Material>("AdaptiveQualityDebugVisualization");
debugVisualizationQuad.AddComponent<MeshRenderer>().material = debugVisualizationQuadMaterial;
}
else if ((!enabled || !drawDebugVisualization) && debugVisualizationQuad != null)
{
Destroy(debugVisualizationQuad);
debugVisualizationQuad = null;
debugVisualizationQuadMaterial = null;
}
}
示例13: GenerateMesh
private void GenerateMesh()
{
mesh = new Mesh();
Vector3[] vertices = new Vector3[list.Count * 2 + 2]; //
Vector3[] normals = new Vector3[list.Count * 2 + 2];
vertices[0] = transform.forward * 40.0f;
vertices[1] = -transform.forward * 40.0f;
normals[0] = transform.up;
normals[1] = transform.up;
Vector2[] newUV = new Vector2[vertices.Length];
Vector3 min = Vector3.Min(vertices[0], vertices[1]);
Vector3 max = Vector3.Max(vertices[0], vertices[1]);
for (int i = 0; i < list.Count; i++)
{
vertices[i * 2 + 2] = list[i].transform.localPosition + transform.forward * 40.0f;
vertices[i * 2 + 3] = list[i].transform.localPosition - transform.forward * 40.0f;
min = Vector3.Min(min, vertices[i * 2 + 2]);
max = Vector3.Max(max, vertices[i * 2 + 2]);
min = Vector3.Min(min, vertices[i * 2 + 3]);
max = Vector3.Max(max, vertices[i * 2 + 3]);
}
/*for (int i = 0; i < vertices.Length; i++)
{
Vector3 normal = Vector3.zero;
if (i > 1) normal += Vector3.Cross(vertices[i], vertices[i - 2]);
if (i < vertices.Length - 2) normal += Vector3.Cross(vertices[i], vertices[i + 2]);
Debug.Log(normal.normalized);
normals[i] = normal.normalized;
}*/
int[] triangles = new int[(vertices.Length - 2) * 6];
for (int i = 0; i < (vertices.Length - 2); i = i + 2)
{
triangles[i * 6] = i + 3;
triangles[i * 6 + 1] = i + 1;
triangles[i * 6 + 2] = i;
triangles[i * 6 + 3] = i + 2;
triangles[i * 6 + 4] = i + 3;
triangles[i * 6 + 5] = i;
}
mesh.vertices = vertices;
//mesh.normals = normals;
Vector2 last0 = Vector2.zero;
Vector2 last1 = Vector2.zero;
for (int i = 0; i < newUV.Length-1; i+=2) // / (max.z - min.z) // /( max.x - min.x) // Mathf.Sqrt(vertices[i].x * vertices[i].x + vertices[i].y * vertices[i].y)
{
float x0 = vertices[i+1].x - last0.x;
float y0 = vertices[i+1].y - last0.y;
newUV[i] = new Vector2(last0.x + Mathf.Sqrt(x0 * x0 + y0 * y0), vertices[i].z);
last0 = new Vector2(vertices[i+1].x, vertices[i+1].y);
float x1 = vertices[i].x - last1.x;
float y1 = vertices[i].y - last1.y;
newUV[i + 1] = new Vector2(last1.x + Mathf.Sqrt(x0 * x0 + y0 * y0), vertices[i+1].z);
last1 = new Vector2(vertices[i].x, vertices[i].y);
}
mesh.uv = newUV;
mesh.triangles = triangles;
mesh.RecalculateNormals();
/*bool switsch = false;
Vector3[] n = mesh.normals;
for(int i = 0; i < n.Length; i++)
{
if (switsch) n[i] = -n[i];
switsch = !switsch;
}
mesh.normals = n;
mesh.RecalculateNormals();*/
GetComponent<MeshFilter>().mesh = mesh;
mesh.UploadMeshData(true);
}
示例14: Apply
public void Apply()
{
MeshRenderer mr = GetComponent<MeshRenderer>();
MeshFilter mf = GetComponent<MeshFilter>();
if (mr != null && mf != null)
{
Mesh m = new Mesh();
m.vertices = mf.sharedMesh.vertices;
if (_colors != null && _colors.Length == mf.sharedMesh.vertexCount)
{
// workaround for unity bug; dispite docs claim, color channels must exist on the original mesh
// for the additionalVertexStream to work.
Color[] orig = new Color[mf.sharedMesh.vertexCount];
for (int i = 0; i < orig.Length; ++i)
{
orig[i] = Color.white;
}
mf.sharedMesh.colors = orig;
m.colors = _colors;
}
if (_uv0 != null && _uv0.Length == mf.sharedMesh.vertexCount) { m.uv = _uv0; }
if (_uv1 != null && _uv1.Length == mf.sharedMesh.vertexCount) { m.uv2 = _uv1; }
if (_uv2 != null && _uv2.Length == mf.sharedMesh.vertexCount) { m.uv3 = _uv2; }
if (_uv3 != null && _uv3.Length == mf.sharedMesh.vertexCount) { m.uv4 = _uv3; }
mr.additionalVertexStreams = m;
m.UploadMeshData(true);
// another Unity bug, when in editor, the paint job will just disapear sometimes. So we have to re-assign
// it every update (even though this doesn't get called each frame, it appears to loose the data during
// the editor update call, which only happens occationaly.
#if UNITY_EDITOR
stream = m;
#endif
}
}
示例15: LoadMesh
public static Mesh LoadMesh(string fullPath, string meshName)
{
var mesh = new Mesh();
using (var fileStream = File.Open(fullPath, FileMode.Open))
{
mesh.LoadOBJ(OBJLoader.LoadOBJ(fileStream));
}
mesh.Optimize();
mesh.name = meshName;
if (!mesh.name.Contains("LOD"))
{
mesh.UploadMeshData(true);
}
return mesh;
}