本文整理汇总了C#中pb_Object.SetUV方法的典型用法代码示例。如果您正苦于以下问题:C# pb_Object.SetUV方法的具体用法?C# pb_Object.SetUV怎么用?C# pb_Object.SetUV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pb_Object
的用法示例。
在下文中一共展示了pb_Object.SetUV方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Triangulate
static void Triangulate(pb_Object pb)
{
Vector3[] v = pb.vertices;
Vector2[] u = pb.msh.uv;
int triangleCount = pb.msh.triangles.Length;
if(triangleCount == v.Length)
{
Debug.LogWarning("We can't pull over any further!\npb_Object: " + pb.name + " is already triangulated.");
}
int vertexCount = triangleCount;
int faceCount = vertexCount / 3;
Vector3[] tri_vertices = new Vector3[vertexCount];
Vector2[] tri_uvs = new Vector2[vertexCount];
pb_Face[] tri_faces = new pb_Face[faceCount];
int n = 0, f = 0;
foreach(pb_Face face in pb.faces)
{
int[] indices = face.indices;
for(int i = 0; i < indices.Length; i+=3)
{
tri_vertices[n+0] = v[indices[i+0]];
tri_vertices[n+1] = v[indices[i+1]];
tri_vertices[n+2] = v[indices[i+2]];
tri_uvs[n+0] = u[indices[i+0]];
tri_uvs[n+1] = u[indices[i+1]];
tri_uvs[n+2] = u[indices[i+2]];
tri_faces[f++] = new pb_Face( new int[] { n+0, n+1, n+2 },
face.material,
face.uv,
face.smoothingGroup,
face.textureGroup, // textureGroup -> force to manual uv mode
face.elementGroup,
face.manualUV, // manualUV
face.color
);
n += 3;
}
}
pb.SetVertices(tri_vertices);
pb.SetUV(tri_uvs);
pb.SetFaces(tri_faces);
pb.SetSharedIndices( pb_IntArrayUtility.ExtractSharedIndices(tri_vertices) );
pb.SetSharedIndicesUV( new pb_IntArray[0] );
}
示例2: ProjectFacesAuto
/**
* Projects UVs on all passed faces, automatically updating the sharedIndicesUV table
* as required (only associates vertices that share a seam).
*/
public static void ProjectFacesAuto(pb_Object pb, pb_Face[] faces)
{
int[] ind = pb_Face.AllTrianglesDistinct(faces);
Vector3[] verts = pbUtil.ValuesWithIndices(pb.vertices, ind);
/* get average face normal */
Vector3 nrm = Vector3.zero;
foreach(pb_Face face in faces)
nrm += pb_Math.Normal(pb, face);
nrm /= (float)faces.Length;
/* project uv coordinates */
Vector2[] uvs = pb_UV_Utility.PlanarProject(verts, nrm);
/* re-assign new projected coords back into full uv array */
Vector2[] rebuiltUVs = pb.msh.uv;
for(int i = 0; i < ind.Length; i++)
rebuiltUVs[ind[i]] = uvs[i];
/* and set the msh uv array using the new coordintaes */
pb.SetUV(rebuiltUVs);
/* now go trhough and set all adjacent face groups to use matching element groups */
}
示例3: AlignEdges
/**
* move the UVs to where the edges passed meet
*/
static bool AlignEdges(pb_Object pb, pb_Face f1, pb_Face f2, pb_Edge edge1, pb_Edge edge2)
{
Vector2[] uvs = pb.uv;
pb_IntArray[] sharedIndices = pb.sharedIndices;
pb_IntArray[] sharedIndicesUV = pb.sharedIndicesUV;
/**
* Match each edge vertex to the other
*/
int[] matchX = new int[2] { edge1.x, -1 };
int[] matchY = new int[2] { edge1.y, -1 };
int siIndex = sharedIndices.IndexOf(edge1.x);
if(siIndex < 0)
return false;
if(sharedIndices[siIndex].array.Contains(edge2.x))
{
matchX[1] = edge2.x;
matchY[1] = edge2.y;
}
else
{
matchX[1] = edge2.y;
matchY[1] = edge2.x;
}
// scale face 2 to match the edge size of f1
float dist_e1 = Vector2.Distance(uvs[edge1.x], uvs[edge1.y]);
float dist_e2 = Vector2.Distance(uvs[edge2.x], uvs[edge2.y]);
float scale = dist_e1/dist_e2;
// doesn't matter what point we scale around because we'll move it in the next step anyways
foreach(int i in f2.distinctIndices)
uvs[i] = uvs[i].ScaleAroundPoint(Vector2.zero, Vector2.one * scale);
/**
* Figure out where the center of each edge is so that we can move the f2 edge to match f1's origin
*/
Vector2 f1_center = (uvs[edge1.x] + uvs[edge1.y]) / 2f;
Vector2 f2_center = (uvs[edge2.x] + uvs[edge2.y]) / 2f;
Vector2 diff = f1_center - f2_center;
/**
* Move f2 face to where it's matching edge center is on top of f1's center
*/
foreach(int i in f2.distinctIndices)
uvs[i] += diff;
/**
* Now that the edge's centers are matching, rotate f2 to match f1's angle
*/
Vector2 angle1 = uvs[matchY[0]] - uvs[matchX[0]];
Vector2 angle2 = uvs[matchY[1]] - uvs[matchX[1]];
float angle = Vector2.Angle(angle1, angle2);
if(Vector3.Cross(angle1, angle2).z < 0)
angle = 360f - angle;
foreach(int i in f2.distinctIndices)
uvs[i] = pb_Math.RotateAroundPoint(uvs[i], f1_center, angle);
float error = Mathf.Abs( Vector2.Distance(uvs[matchX[0]], uvs[matchX[1]]) ) + Mathf.Abs( Vector2.Distance(uvs[matchY[0]], uvs[matchY[1]]) );
// now check that the matched UVs are on top of one another if the error allowance is greater than some small value
if(error > .02)
{
// first try rotating 180 degrees
foreach(int i in f2.distinctIndices)
uvs[i] = pb_Math.RotateAroundPoint(uvs[i], f1_center, 180f);
float e2 = Mathf.Abs( Vector2.Distance(uvs[matchX[0]], uvs[matchX[1]]) ) + Mathf.Abs( Vector2.Distance(uvs[matchY[0]], uvs[matchY[1]]) );
if(e2 < error)
error = e2;
else
{
// flip 'em back around
foreach(int i in f2.distinctIndices)
uvs[i] = pb_Math.RotateAroundPoint(uvs[i], f1_center, 180f);
}
}
// If successfully aligned, merge the sharedIndicesUV
pbUVOps.SplitUVs(pb, f2.distinctIndices);
pb_IntArrayUtility.MergeSharedIndices(ref sharedIndicesUV, matchX);
pb_IntArrayUtility.MergeSharedIndices(ref sharedIndicesUV, matchY);
pb_IntArray.RemoveEmptyOrNull(ref sharedIndicesUV);
pb.SetSharedIndicesUV(sharedIndicesUV);
// @todo Update Element Groups here?
pb.SetUV(uvs);
//.........这里部分代码省略.........
示例4: UnwrapSphericalPB
public static void UnwrapSphericalPB(pb_Object pb, int[] indices)
{
Vector2[] uv = pb.uv;
Vector3[] v = pb.vertices;
Vector3 cen = pb.msh.bounds.center;
float radius = Vector3.Distance(pb.msh.bounds.extents, cen);
for(int i = 0; i < indices.Length; i++)
{
Vector3 p = (v[i] - cen).normalized;
uv[i].y = Mathf.Acos(p.z/radius) / Mathf.PI;
if (p.y >= 0)
uv[i].x = Mathf.Acos(p.x/(radius * Mathf.Sin(Mathf.PI*(uv[i].y)))) / (Mathf.PI * 2f);
else
uv[i].x = (Mathf.PI + Mathf.Acos(p.x/(radius * Mathf.Sin(Mathf.PI*(uv[i].y))))) / (Mathf.PI * 2f);
}
SplitUVs(pb, indices);
pb.SetUV(uv);
}
示例5: UnwrapSpherical
public static void UnwrapSpherical(pb_Object pb, int[] indices)
{
Vector2[] uv = pb.uv;
Vector3[] v = pb.vertices;
Vector3 cen = pb.msh.bounds.center;
float radius = Vector3.Distance(pb.msh.bounds.extents, cen);
for(int i = 0; i < indices.Length; i++)
{
Vector3 p = (v[i] - cen).normalized;
uv[i].x = .5f + (Mathf.Atan2(p.z, p.x) / (2f * Mathf.PI));
uv[i].y = .5f - (Mathf.Asin(p.y) / Mathf.PI);
uv[i] *= radius;
}
SplitUVs(pb, indices);
pb.SetUV(uv);
}
示例6: ProjectFacesBox
/**
* Projects UVs for each face using the closest normal on a box.
*/
public static void ProjectFacesBox(pb_Object pb, pb_Face[] faces)
{
Vector2[] uv = pb.uv;
Dictionary<ProjectionAxis, List<pb_Face>> sorted = new Dictionary<ProjectionAxis, List<pb_Face>>();
for(int i = 0; i < faces.Length; i++)
{
Vector3 nrm = pb_Math.Normal(pb, faces[i]);
ProjectionAxis axis = pb_Math.VectorToProjectionAxis(nrm);
if(sorted.ContainsKey(axis))
{
sorted[axis].Add(faces[i]);
}
else
{
sorted.Add(axis, new List<pb_Face>() { faces[i] });
}
// clean up UV stuff - no shared UV indices and remove element group
faces[i].elementGroup = -1;
}
foreach(KeyValuePair<ProjectionAxis, List<pb_Face>> kvp in sorted)
{
int[] distinct = pb_Face.AllTrianglesDistinct(kvp.Value.ToArray());
Vector2[] uvs = pb_Math.PlanarProject( pb.GetVertices(distinct), pb_Math.ProjectionAxisToVector(kvp.Key), kvp.Key );
for(int n = 0; n < distinct.Length; n++)
uv[distinct[n]] = uvs[n];
SplitUVs(pb, distinct);
}
/* and set the msh uv array using the new coordintaes */
pb.SetUV(uv);
pb.ToMesh();
pb.Refresh();
}
示例7: ProjectFacesAuto
/**
* Projects UVs on all passed faces, automatically updating the sharedIndicesUV table
* as required (only associates vertices that share a seam).
*/
public static void ProjectFacesAuto(pb_Object pb, pb_Face[] faces)
{
int[] ind = pb_Face.AllTrianglesDistinct(faces);
Vector3[] verts = pbUtil.ValuesWithIndices(pb.vertices, ind);
/* get average face normal */
Vector3 nrm = Vector3.zero;
foreach(pb_Face face in faces)
nrm += pb_Math.Normal(pb, face);
nrm /= (float)faces.Length;
/* project uv coordinates */
Vector2[] uvs = pb_Math.PlanarProject(verts, nrm);
/* re-assign new projected coords back into full uv array */
Vector2[] rebuiltUVs = pb.uv;
for(int i = 0; i < ind.Length; i++)
rebuiltUVs[ind[i]] = uvs[i];
/* and set the msh uv array using the new coordintaes */
pb.SetUV(rebuiltUVs);
pb.msh.uv = rebuiltUVs;
/* now go trhough and set all adjacent face groups to use matching element groups */
foreach(pb_Face f in faces)
{
f.elementGroup = -1;
SplitUVs(pb, f.distinctIndices);
}
// pb_IntArray[] sharedIndices = pb.sharedIndices;
pb.SewUVs(pb_Face.AllTrianglesDistinct(faces), .001f);
// foreach(pb_Face f in faces)
// {
// foreach(pb_Edge e in f.edges)
// {
// foreach(pb_Face f2 in faces)
// {
// if(f2 == f) continue;
// int index = f2.edges.IndexOf(e, sharedIndices);
// // Found an aligned edge
// if( index > -1 )
// {
// if(f.elementGroup < 0)
// {
// if(f2.elementGroup < 0)
// {
// f.elementGroup = pb.UnusedElementGroup(0);
// f2.elementGroup = f.elementGroup;
// }
// else
// {
// f.elementGroup = f2.elementGroup;
// }
// }
// else
// {
// if(f2.elementGroup < 0)
// f2.elementGroup = f.elementGroup;
// else
// {
// foreach(pb_Face iter in System.Array.FindAll(faces, element => element.elementGroup == f2.elementGroup))
// iter.elementGroup = f.elementGroup;
// }
// }
// }
// }
// }
// }
}
示例8: ApplyUVs
/**
* Sets an array to the appropriate UV channel, but don't refresh the Mesh.
*/
static void ApplyUVs(pb_Object pb, Vector2[] uvs, int channel)
{
switch(channel)
{
case 0:
pb.SetUV(uvs);
pb.msh.uv = uvs;
break;
case 1:
pb.msh.uv2 = uvs;
break;
}
}
示例9: ResetPbObjectWithMeshFilter
//.........这里部分代码省略.........
int vertexCount = m.vertexCount;
Vector3[] m_vertices = m.vertices;
Color[] m_colors = m.colors != null && m.colors.Length == vertexCount ? m.colors : new Color[vertexCount];
Vector2[] m_uvs = m.uv;
List<Vector3> verts = preserveFaces ? new List<Vector3>(m.vertices) : new List<Vector3>();
List<Color> cols = preserveFaces ? new List<Color>(m.colors) : new List<Color>();
List<Vector2> uvs = preserveFaces ? new List<Vector2>(m.uv) : new List<Vector2>();
List<pb_Face> faces = new List<pb_Face>();
MeshRenderer mr = pb.gameObject.GetComponent<MeshRenderer>();
if(mr == null) mr = pb.gameObject.AddComponent<MeshRenderer>();
Material[] sharedMaterials = mr.sharedMaterials;
int mat_length = sharedMaterials.Length;
for(int n = 0; n < m.subMeshCount; n++)
{
int[] tris = m.GetTriangles(n);
for(int i = 0; i < tris.Length; i+=3)
{
int index = -1;
if(preserveFaces)
{
for(int j = 0; j < faces.Count; j++)
{
if( faces[j].distinctIndices.Contains(tris[i+0]) ||
faces[j].distinctIndices.Contains(tris[i+1]) ||
faces[j].distinctIndices.Contains(tris[i+2]))
{
index = j;
break;
}
}
}
if(index > -1 && preserveFaces)
{
int len = faces[index].indices.Length;
int[] arr = new int[len + 3];
System.Array.Copy(faces[index].indices, 0, arr, 0, len);
arr[len+0] = tris[i+0];
arr[len+1] = tris[i+1];
arr[len+2] = tris[i+2];
faces[index].SetIndices(arr);
faces[index].RebuildCaches();
}
else
{
int[] faceTris;
if(preserveFaces)
{
faceTris = new int[3]
{
tris[i+0],
tris[i+1],
tris[i+2]
};
}
else
{
verts.Add(m_vertices[tris[i+0]]);
verts.Add(m_vertices[tris[i+1]]);
verts.Add(m_vertices[tris[i+2]]);
cols.Add(m_colors != null && m_colors.Length == vertexCount ? m_colors[tris[i+0]] : Color.white);
cols.Add(m_colors != null && m_colors.Length == vertexCount ? m_colors[tris[i+1]] : Color.white);
cols.Add(m_colors != null && m_colors.Length == vertexCount ? m_colors[tris[i+2]] : Color.white);
uvs.Add(m_uvs[tris[i+0]]);
uvs.Add(m_uvs[tris[i+1]]);
uvs.Add(m_uvs[tris[i+2]]);
faceTris = new int[3] { i+0, i+1, i+2 };
}
faces.Add(
new pb_Face(
faceTris,
sharedMaterials[n >= mat_length ? mat_length - 1 : n],
new pb_UV(),
0, // smoothing group
-1, // texture group
-1, // element group
true // manualUV
));
}
}
}
pb.SetVertices(verts.ToArray());
pb.SetUV(uvs.ToArray());
pb.SetFaces(faces.ToArray());
pb.SetSharedIndices(pb_IntArrayUtility.ExtractSharedIndices(verts.ToArray()));
pb.SetColors(cols.ToArray());
return true;
}
示例10: CombineObjects
/**
* \brief Given an array of "donors", this method returns a merged #pb_Object.
*/
public static bool CombineObjects(pb_Object[] pbs, out pb_Object combined)
{
combined = null;
if(pbs.Length < 1) return false;
List<Vector3> v = new List<Vector3>();
List<Vector2> u = new List<Vector2>();
List<Color> c = new List<Color>();
List<pb_Face> f = new List<pb_Face>();
List<pb_IntArray> s = new List<pb_IntArray>();
List<pb_IntArray> suv = new List<pb_IntArray>();
foreach(pb_Object pb in pbs)
{
int vertexCount = v.Count;
// Vertices
v.AddRange(pb.VerticesInWorldSpace());
// UVs
u.AddRange(pb.uv);
// Colors
c.AddRange(pb.colors);
// Faces
pb_Face[] faces = new pb_Face[pb.faces.Length];
for(int i = 0; i < faces.Length; i++)
{
faces[i] = new pb_Face(pb.faces[i]);
faces[i].manualUV = true;
faces[i].ShiftIndices(vertexCount);
faces[i].RebuildCaches();
}
f.AddRange(faces);
// Shared Indices
pb_IntArray[] si = pb.GetSharedIndices();
for(int i = 0; i < si.Length; i++)
{
for(int n = 0; n < si[i].Length; n++)
si[i][n] += vertexCount;
}
s.AddRange(si);
// Shared Indices UV
{
pb_IntArray[] si_uv = pb.GetSharedIndicesUV();
for(int i = 0; i < si_uv.Length; i++)
{
for(int n = 0; n < si_uv[i].Length; n++)
si_uv[i][n] += vertexCount;
}
suv.AddRange(si_uv);
}
}
GameObject go = (GameObject)GameObject.Instantiate(pbs[0].gameObject);
go.transform.position = Vector3.zero;
go.transform.localRotation = Quaternion.identity;
go.transform.localScale = Vector3.one;
// Destroy the children
foreach(Transform t in go.transform)
GameObject.DestroyImmediate(t.gameObject);
if(go.GetComponent<pb_Object>()) GameObject.DestroyImmediate(go.GetComponent<pb_Object>());
if(go.GetComponent<pb_Entity>()) GameObject.DestroyImmediate(go.GetComponent<pb_Entity>());
combined = go.AddComponent<pb_Object>();
combined.SetVertices(v.ToArray());
combined.SetUV(u.ToArray());
combined.SetColors(c.ToArray());
combined.SetFaces(f.ToArray());
combined.SetSharedIndices( s.ToArray() ?? pb_IntArrayUtility.ExtractSharedIndices(v.ToArray()) );
combined.SetSharedIndicesUV( suv.ToArray() ?? new pb_IntArray[0] {});
combined.ToMesh();
combined.GetComponent<pb_Entity>().SetEntity( pbs[0].GetComponent<pb_Entity>().entityType );
combined.CenterPivot( pbs[0].transform.position );
combined.Refresh();
// refresh donors since deleting the children of the instantiated object could cause them to lose references
foreach(pb_Object pb in pbs)
pb.Verify();
return true;
}