private void EvaluateNormals(IndexedMesh indexedMesh) { foreach (var bufferAndPrimitives in indexedMesh.GroupPrimitives()) { var bufferView = bufferAndPrimitives.BufferView; var existingNormals = bufferView.GetStream(StreamKey.Normal); if (existingNormals != null) { continue; } var positions = bufferView.GetStreamReader <Vector3>(StreamKey.Position); var normals = new ArrayMeshStream <Vector3>(positions.Count, StreamConverterFactory.Default); foreach (var primitive in bufferAndPrimitives) { primitive.SetIndexStream(StreamKey.Normal, primitive.GetIndexReader(StreamKey.Position).ToList()); } { foreach (var face in indexedMesh.Primitives.SelectMany(_ => _.GetFaces(StreamKey.Position))) { var a = positions[face.Item1]; var b = positions[face.Item2]; var c = positions[face.Item3]; var n = Vector3.Cross(b - a, c - a); normals[face.Item1] += n; normals[face.Item2] += n; normals[face.Item3] += n; } for (var index = 0; index < normals.Count; index++) { var normal = normals[index]; if (normal != Vector3.Zero) { normals[index] = Vector3.Normalize(normal); } } bufferView.SetStream(StreamKey.Normal, normals); } foreach (var primitive in bufferAndPrimitives) { primitive.SetIndexStream(StreamKey.Normal, primitive.GetIndexReader(StreamKey.Position).ToList()); } } }
private IEnumerable <IMesh> PaintSeparateStreamMesh(IndexedMesh mesh) { foreach (var bufferAndPrimitives in mesh.GroupPrimitives()) { var bufferView = bufferAndPrimitives.BufferView; if (bufferView.GetStream(StreamKey.Color) != null) { continue; } var streamConverterFactory = bufferView.GetStream(StreamKey.Position).ConverterFactory; bufferView.SetStream(StreamKey.Color, new ArrayMeshStream <Vector4>(new[] { _color }, streamConverterFactory)); foreach (var primitiveAndIndex in bufferAndPrimitives.Primitives) { var submesh = primitiveAndIndex.Primitive; var positionIndices = submesh.GetIndexReader(StreamKey.Position); var indices = new int[positionIndices.Count]; submesh.SetIndexStream(StreamKey.Color, indices); } } yield return(mesh); }