private bool RenderSubMesh(SoftMesh mesh, SoftSubMesh subMesh, Matrix4x4 objToWorld, RenderPassMode passMode) { if (subMesh == null || passMode == null) { return(false); } var indexes = subMesh.Indexes; var vertexs = mesh.Vertexs; var colors = mesh.Colors; var uv1s = mesh.UV1s; bool ret = false; bool isColorEmpty = colors == null || colors.Count <= 0; bool isUV1Empty = uv1s == null || uv1s.Count <= 0; Color c1 = Color.white; Color c2 = Color.white; Color c3 = Color.white; Vector4 uv1_1 = Vector4.zero; Vector4 uv1_2 = Vector4.zero; Vector4 uv1_3 = Vector4.zero; if (vertexs != null && (isColorEmpty || vertexs.Count == colors.Count) && indexes != null && indexes.Count > 0) { int triangleCnt = ((int)indexes.Count / 3); for (int i = 0; i < triangleCnt; ++i) { int idx = i * 3; int index = indexes[idx]; Vector3 p1 = vertexs[index]; if (!isColorEmpty) { c1 = colors[index]; } if (!isUV1Empty) { uv1_1 = uv1s[index]; } index = indexes[idx + 1]; Vector3 p2 = vertexs[index]; if (!isColorEmpty) { c2 = colors[index]; } if (!isUV1Empty) { uv1_2 = uv1s[index]; } index = indexes[idx + 2]; Vector3 p3 = vertexs[index]; if (!isColorEmpty) { c3 = colors[index]; } if (!isUV1Empty) { uv1_3 = uv1s[index]; } Triangle tri = new Triangle(p1, p2, p3); // 三角形转到世界坐标系 tri.MulMatrix(objToWorld); // 过CullMode 【注意】根据渲染管线VertexShader中可以任意改变三角形,所以要放到VS后面才行,也就是到MVP坐标系里判断 // 不在这里做摄影机剔除,移到VS后面 // if (SoftMath.IsCulled(this, passMode.Cull, tri)) { // continue; // } //---- TriangleVertex triV = new TriangleVertex(tri, c1, c2, c3, passMode.mainTex); if (!isUV1Empty) { triV.uv1_1 = uv1_1; triV.uv1_2 = uv1_2; triV.uv1_3 = uv1_3; } // 进入VertexShader了, 做顶点变换等 m_TrianglesMgr.AddTriangle(triV); ret = true; } } return(ret); }
public void BuildFromMesh(Mesh mesh) { Clear(); if (mesh != null) { Vector3 minVec = Vector3.zero; Vector3 maxVec = Vector3.zero; bool isInitMinMax = false; List <Vector3> vertexs = new List <Vector3>(); mesh.GetVertices(vertexs); if (vertexs.Count > 0) { m_VertexBuffer = new VertexBuffer(); m_VertexBuffer.Capacity = vertexs.Count; for (int i = 0; i < vertexs.Count; ++i) { Vector3 v = vertexs[i]; m_VertexBuffer.Add(v); if (!isInitMinMax) { minVec = v; maxVec = v; isInitMinMax = true; } else { if (minVec.x > v.x) { minVec.x = v.x; } if (minVec.y > v.y) { minVec.y = v.y; } if (minVec.z > v.z) { minVec.z = v.z; } if (maxVec.x < v.x) { maxVec.x = v.x; } if (maxVec.y < v.y) { maxVec.y = v.y; } if (maxVec.z < v.z) { maxVec.z = v.z; } } } } if (isInitMinMax) { m_BoundSpere.position = (maxVec + minVec) / 2.0f; m_BoundSpere.radius = (maxVec - minVec).magnitude / 2.0f; } else { m_BoundSpere.position = Vector3.zero; m_BoundSpere.radius = 0f; } List <Color> colors = new List <Color>(); mesh.GetColors(colors); if (colors.Count > 0) { m_ColorBuffer = new VertexColorBuffer(); m_ColorBuffer.Capacity = colors.Count; for (int i = 0; i < colors.Count; ++i) { m_ColorBuffer.Add(colors[i]); } } // UV 1坐标 List <Vector4> uvs = new List <Vector4>(); mesh.GetUVs(0, uvs); if (uvs.Count > 0) { if (m_UV1Buffer == null) { m_UV1Buffer = new UVBuffer(); } m_UV1Buffer.Capacity = uvs.Count; for (int i = 0; i < uvs.Count; ++i) { m_UV1Buffer.Add(uvs[i]); } } for (int i = 0; i < mesh.subMeshCount; ++i) { if (m_SubList == null) { m_SubList = new List <SoftSubMesh>(); } var triangles = mesh.GetTriangles(i); if (triangles != null && triangles.Length > 0) { SoftSubMesh subMesh = new SoftSubMesh(mesh, triangles); m_SubList.Add(subMesh); } } } }