首先,我创建2个立方体并组合成1个网格。我想在每个中心缩放2个立方体。
我该怎么做?
我的代码:
private MeshFilter m_MeshFilter;
private MeshRenderer m_MeshRenderer;
private Mesh m_Mesh;
private float m_Length = 1f;
private float m_Width = 1f;
private float m_Height = 1f;
我创建空的游戏对象并添加代码。
private void Awake () {
m_MeshFilter = this.GetComponent<MeshFilter> ();
m_MeshRenderer = this.GetComponent<MeshRenderer> ();
m_Mesh = m_MeshFilter.mesh;
m_Mesh.Clear ();
m_Cloud = new Mesh[2, 1];
}
private void Start() {
var mesh1 = DrawCube (0, 0, 0, m_Length, m_Width, m_Height);
var mesh2 = DrawCube (1, 1, 2, m_Length, m_Width, m_Height);
// Combine 2 cube mesh
CombineInstance[] combine = new CombineInstance[2];
combine [0].mesh = mesh1;
combine [0].transform = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3.one);
combine [1].mesh = mesh2;
combine [1].transform = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3.one);
m_MeshFilter.mesh = new Mesh ();
m_MeshFilter.mesh.CombineMeshes (combine);
}
绘制具有x,y,宽度,高度返回网格的立方体
private Mesh DrawCube(int i, int x, int y, float length, float width, float height) {
#region Mesh
var cubeMesh = new Mesh ();
var center = 0.5f;
#endregion
#region Vertices
Vector3 p0 = new Vector3( -length * center + x, -width * center, height * center + y );
Vector3 p1 = new Vector3( length * center + x, -width * center, height * center + y );
Vector3 p2 = new Vector3( length * center + x, -width * center, -height * center + y );
Vector3 p3 = new Vector3( -length * center + x, -width * center, -height * center + y );
Vector3 p4 = new Vector3( -length * center + x, width * center, height * center + y );
Vector3 p5 = new Vector3( length * center + x, width * center, height * center + y );
Vector3 p6 = new Vector3( length * center + x, width * center, -height * center + y );
Vector3 p7 = new Vector3( -length * center + x, width * center, -height * center + y );
Vector3[] vertices = new Vector3[]
{
// Bottom
p0, p1, p2, p3,
// Left
p7, p4, p0, p3,
// Front
p4, p5, p1, p0,
// Back
p6, p7, p3, p2,
// Right
p5, p6, p2, p1,
// Top
p7, p6, p5, p4
};
#endregion
#region Normales
Vector3 up = Vector3.up;
Vector3 down = Vector3.down;
Vector3 front = Vector3.forward;
Vector3 back = Vector3.back;
Vector3 left = Vector3.left;
Vector3 right = Vector3.right;
Vector3[] normales = new Vector3[]
{
// Bottom
down, down, down, down,
// Left
left, left, left, left,
// Front
front, front, front, front,
// Back
back, back, back, back,
// Right
right, right, right, right,
// Top
up, up, up, up
};
#endregion
#region UVs
Vector2 _00 = new Vector2( 0f, 0f );
Vector2 _10 = new Vector2( 1f, 0f );
Vector2 _01 = new Vector2( 0f, 1f );
Vector2 _11 = new Vector2( 1f, 1f );
Vector2[] uvs = new Vector2[]
{
// Bottom
_11, _01, _00, _10,
// Left
_11, _01, _00, _10,
// Front
_11, _01, _00, _10,
// Back
_11, _01, _00, _10,
// Right
_11, _01, _00, _10,
// Top
_11, _01, _00, _10,
};
#endregion
#region Triangles
int[] triangles = new int[]
{
// Bottom
3, 1, 0,
3, 2, 1,
// Left
3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
// Front
3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
// Back
3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
// Right
3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
// Top
3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
};
#endregion
cubeMesh.vertices = vertices;
cubeMesh.normals = normales;
cubeMesh.uv = uvs;
cubeMesh.triangles = triangles;
cubeMesh.RecalculateBounds();
cubeMesh.Optimize();
return cubeMesh;
}
答案 0 :(得分:0)
是的,最后,我的问题似乎以简单的方式解决了。
首先我找到每个立方体的中心并计算方向然后按比例缩放:D
很有趣,无论如何,谢谢......
private Vector3 CalcuateCenterMesh (Vector3[] vectors) {
var sumVec = Vector3.zero;
for (int i = 0; i < vectors.Length; i++) {
sumVec += vectors [i];
}
return sumVec / vectors.Length;
}
并且规模:
private void ScaleMesh (int index, float scale) {
var mesh = m_MeshFilter.sharedMesh;
var vertices = mesh.vertices;
var min = index * 24;
var max = (index + 1) * 24;
for (int i = min; i < max; i++) {
var direction = vertices[i] - m_MeshCenters[index];
var directionNormal = direction.normalized;
vertices [i] = directionNormal * scale + m_MeshCenters[index];
}
m_MeshFilter.sharedMesh.vertices = vertices;
m_MeshFilter.sharedMesh.RecalculateBounds ();
}
但如果你有更好的解决方案请给我:D
我想创建像游戏一样的云:&#34; Ogre Attacks和Giant Raids!强大的弩炮塔 - 王国和城堡&#34;