protected override void ConvertGeometry(IGeometry iGeometry, ref Matrix4x4 nodeWorldTransform) { if (iGeometry.Format != GeometryFormat.Basic && iGeometry.Format != GeometryFormat.BasicDX) { throw new InvalidOperationException(); } var geometry = ( Geometry )iGeometry; var materialIndexMap = new int[Math.Max(1, geometry.Materials.Length)]; if (geometry.HasMaterials) { for (var i = 0; i < geometry.Materials.Length; i++) { // Convert material var material = geometry.Materials[i]; if (!mConvertedMaterialCache.TryGetValue(material, out var aiMaterialIndex)) { var textureName = material.UseTexture ? FormatTextureName(material.TextureId) : null; var aiMaterial = CreateMaterial(material.Diffuse, material.Specular, Color.Gray, textureName, material.ClampU, material.ClampV, material.FlipU, material.FlipV, material.UseAlpha); aiMaterialIndex = Scene.MaterialCount; Scene.Materials.Add(aiMaterial); mConvertedMaterialCache[material] = aiMaterialIndex; } materialIndexMap[i] = aiMaterialIndex; } } else { materialIndexMap[0] = GetNoMaterialMaterialIndex(); } foreach (var mesh in geometry.Meshes) { var aiMesh = new Assimp.Mesh(); // Convert mesh var hasNormals = geometry.HasNormals || mesh.HasNormals; var triangleIndices = mesh.ToTriangles(); var vertices = new List <Vertex>(); for (var i = 0; i < triangleIndices.Length; i += 3) { var aiFace = new Assimp.Face(); for (int j = 0; j < 3; j++) { var index = triangleIndices[i + j]; var vertex = new Vertex { Position = geometry.VertexPositions[index.VertexIndex] }; if (hasNormals) { vertex.Normal = geometry.HasNormals ? geometry.VertexNormals[index.VertexIndex] : index.Normal; } if (mesh.HasColors) { vertex.Color = index.Color; } if (mesh.HasUVs) { vertex.UV = UVCodec.Decode255(index.UV); } var vertexIndex = vertices.IndexOf(vertex); if (vertexIndex == -1) { vertexIndex = vertices.Count; vertices.Add(vertex); } aiFace.Indices.Add(vertexIndex); } aiMesh.Faces.Add(aiFace); } aiMesh.Vertices.AddRange(vertices.Select(x => ToAssimp(x.Position))); if (hasNormals) { aiMesh.Normals.AddRange(vertices.Select(x => ToAssimp(x.Normal))); } if (mesh.HasColors) { aiMesh.VertexColorChannels[0].AddRange(vertices.Select(x => ToAssimp(x.Color))); } if (mesh.HasUVs) { aiMesh.TextureCoordinateChannels[0].AddRange(vertices.Select(x => ToAssimp(x.UV))); } // Set mesh material index aiMesh.MaterialIndex = materialIndexMap[Math.Min(mesh.MaterialId, materialIndexMap.Length - 1)]; // Add mesh to scene Scene.Meshes.Add(aiMesh); } }
private static Node ConvertNode(Assimp.Node aiNode, List <Assimp.Mesh> aiMeshes, List <MaterialBuildInfo> materialBuildInfos) { Node ConvertHierarchyNodeRecursively(Assimp.Node curAiNode, ref Node previousSibling, Node parent, ref Assimp.Matrix4x4 parentNodeWorldTransform) { var nodeWorldTransform = curAiNode.Transform * parentNodeWorldTransform; var nodeInverseWorldTransform = nodeWorldTransform; nodeInverseWorldTransform.Inverse(); curAiNode.Transform.Decompose(out var scale, out var rotation, out var translation); // Create node var node = new Node(AssimpHelper.FromAssimp(translation), AngleVector.FromQuaternion(AssimpHelper.FromAssimp(rotation)), AssimpHelper.FromAssimp(scale), parent); if (curAiNode.HasMeshes) { var geometry = new Geometry(); // Convert meshes var vertexPositions = new List <Assimp.Vector3D>(); var vertexNormals = new List <Assimp.Vector3D>(); var vertexUVs = new List <Assimp.Vector3D>(); var vertexColors = new List <Assimp.Color4D>(); var lastRenderState = new MeshRenderState(); foreach (var aiMeshIndex in curAiNode.MeshIndices) { var aiMesh = aiMeshes[aiMeshIndex]; var material = materialBuildInfos[aiMesh.MaterialIndex]; var mesh = new Mesh(); var renderState = new MeshRenderState(); renderState.IndexFlags = IndexAttributeFlags.HasPosition | IndexAttributeFlags.Position16BitIndex; var useColors = false; var hasColors = aiMesh.HasVertexColors(0); var hasUVs = aiMesh.HasTextureCoords(0); var hasNormals = aiMesh.HasNormals; if (hasColors || !hasNormals) { renderState.IndexFlags |= IndexAttributeFlags.HasColor | IndexAttributeFlags.Color16BitIndex; useColors = true; } else { renderState.IndexFlags |= IndexAttributeFlags.HasNormal | IndexAttributeFlags.Normal16BitIndex; } if (hasUVs) { renderState.IndexFlags |= IndexAttributeFlags.HasUV | IndexAttributeFlags.UV16BitIndex; } // Convert faces var triangleIndices = new Index[aiMesh.FaceCount * 3]; for (var i = 0; i < aiMesh.Faces.Count; i++) { var aiFace = aiMesh.Faces[i]; Debug.Assert(aiFace.IndexCount == 3); for (var j = 0; j < aiFace.Indices.Count; j++) { int aiFaceIndex = aiFace.Indices[j]; var position = aiMesh.Vertices[aiFaceIndex]; var positionIndex = vertexPositions.IndexOf(position); if (positionIndex == -1) { positionIndex = vertexPositions.Count; vertexPositions.Add(position); } var normalIndex = 0; var colorIndex = 0; var uvIndex = 0; if (useColors) { var color = hasColors ? aiMesh.VertexColorChannels[0][aiFaceIndex] : new Assimp.Color4D(); colorIndex = vertexColors.IndexOf(color); if (colorIndex == -1) { colorIndex = vertexColors.Count; vertexColors.Add(color); } } else { var normal = aiMesh.Normals[aiFaceIndex]; normalIndex = vertexNormals.IndexOf(normal); if (normalIndex == -1) { normalIndex = vertexNormals.Count; vertexNormals.Add(normal); } } if (hasUVs) { var uv = aiMesh.TextureCoordinateChannels[0][aiFaceIndex]; uvIndex = vertexUVs.IndexOf(uv); if (uvIndex == -1) { uvIndex = vertexUVs.Count; vertexUVs.Add(uv); } } triangleIndices[(i * 3) + j] = new Index { PositionIndex = ( ushort )positionIndex, NormalIndex = ( ushort )normalIndex, ColorIndex = ( ushort )colorIndex, UVIndex = ( ushort )uvIndex }; } } // Build display list var displayList = new GXDisplayList(GXPrimitive.Triangles, triangleIndices); mesh.DisplayLists.Add(displayList); // Set up render params var indexFlagsParam = new IndexAttributeFlagsParam(renderState.IndexFlags); mesh.Parameters.Add(indexFlagsParam); if (useColors) { mesh.Parameters.Add(new LightingParams(LightingParams.Preset.Colors)); } else { mesh.Parameters.Add(new LightingParams(LightingParams.Preset.Normals)); } mesh.Parameters.Add(new TextureParams(( ushort )(material.TextureId))); mesh.Parameters.Add(new MipMapParams()); geometry.OpaqueMeshes.Add(mesh); } // Build vertex buffers if (vertexPositions.Count > 0) { geometry.VertexBuffers.Add(new VertexPositionBuffer(vertexPositions.Select(x => { Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeInverseWorldTransform); return(AssimpHelper.FromAssimp(x)); }).ToArray())); } if (vertexNormals.Count > 0) { nodeInverseWorldTransform.Transpose(); geometry.VertexBuffers.Add(new VertexNormalBuffer(vertexNormals.Select(x => { Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeInverseWorldTransform); return(AssimpHelper.FromAssimp(x)); }).ToArray())); } if (vertexColors.Count > 0) { geometry.VertexBuffers.Add(new VertexColorBuffer(vertexColors.Select(AssimpHelper.FromAssimp).ToArray())); } if (vertexUVs.Count > 0) { geometry.VertexBuffers.Add(new VertexUVBuffer(vertexUVs.Select(x => UVCodec.Encode1023(AssimpHelper .FromAssimpAsVector2(x))) .ToArray())); } } // Set sibling (next) reference of previous if (previousSibling != null) { previousSibling.Sibling = node; } previousSibling = node; if (curAiNode.HasChildren) { Node childPreviousSibling = null; foreach (var aiChildNode in curAiNode.Children) { var childNode = ConvertHierarchyNodeRecursively(aiChildNode, ref childPreviousSibling, node, ref nodeWorldTransform); // Make sure to set the 'first child' reference if we haven't already if (node.Child == null) { node.Child = childNode; } } } return(node); } // Dummy! Node dummy = null; var identity = Assimp.Matrix4x4.Identity; return(ConvertHierarchyNodeRecursively(aiNode, ref dummy, null, ref identity)); }
private void ConvertMeshes(List <Mesh> meshes, VertexPositionBuffer positionBuffer, VertexNormalBuffer normalBuffer, VertexColorBuffer colorBuffer, VertexUVBuffer uvBuffer) { var state = new MeshRenderState(); for (var i = 0; i < meshes.Count; i++) { var mesh = meshes[i]; if (mesh.Parameters != null && mesh.Parameters.Count > 0) { ProcessMeshParameters(mesh.Parameters, ref state); } var stateCopy = state; stateCopy.TextureId = 0; stateCopy.TileMode = 0; //stateCopy.IndexFlags = 0; stateCopy.AmbientColor = new Color(); sUniqueStates.Add(stateCopy); var aiMesh = new Assimp.Mesh(); var vertexCache = new List <Vertex>(); Debug.Assert(state.IndexFlags.HasFlag(IndexAttributeFlags.HasPosition) ? positionBuffer != null : true); Debug.Assert(state.IndexFlags.HasFlag(IndexAttributeFlags.HasNormal) ? normalBuffer != null : true); Debug.Assert(state.IndexFlags.HasFlag(IndexAttributeFlags.HasColor) ? colorBuffer != null : true); Debug.Assert(state.IndexFlags.HasFlag(IndexAttributeFlags.HasUV) ? uvBuffer != null : true); // Extract all vertices used by the triangles, and build a new vertex list // with each vertex attribute clumped together var aiFace = new Assimp.Face(); foreach (var index in mesh.DisplayLists.SelectMany(x => x.ToTriangles())) { var vertex = new Vertex(); if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasPosition)) { vertex.Position = positionBuffer.Elements[index.PositionIndex]; } if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasNormal)) { vertex.Normal = normalBuffer.Elements[index.NormalIndex]; } if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasColor)) { vertex.Color = colorBuffer.Elements[index.ColorIndex]; } if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasUV)) { vertex.UV = uvBuffer.Elements[index.UVIndex]; } // Find index of this vertex in the list in case it already exists var vertexIndex = vertexCache.IndexOf(vertex); if (vertexIndex == -1) { vertexIndex = vertexCache.Count; vertexCache.Add(vertex); } aiFace.Indices.Add(vertexIndex); if (aiFace.IndexCount == 3) { // Done with this face, move on to the next one aiMesh.Faces.Add(aiFace); aiFace = new Assimp.Face(); } } // Convert vertices aiMesh.Vertices.AddRange(vertexCache.Select(x => ToAssimp(x.Position))); if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasNormal)) { aiMesh.Normals.AddRange(vertexCache.Select(x => ToAssimp(x.Normal))); } if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasUV)) { aiMesh.TextureCoordinateChannels[0].AddRange(vertexCache.Select(x => ToAssimp(UVCodec.Decode1023(x.UV)))); } if (state.IndexFlags.HasFlag(IndexAttributeFlags.HasColor)) { aiMesh.VertexColorChannels[0].AddRange(vertexCache.Select(x => ToAssimp(x.Color))); } // Convert material if (!mConvertedMaterialCache.TryGetValue(state, out var aiMaterialIndex)) { // Not in cache, so create a new one and add it aiMaterialIndex = Scene.MaterialCount; Scene.Materials.Add(CreateMaterial(Color.Gray, Color.Gray, Color.Gray, FormatTextureName(state.TextureId), false, false, state.TileMode.HasFlag(TileMode.MirrorU), state.TileMode.HasFlag(TileMode.MirrorV), state.BlendAlphaFlags.HasFlag(BlendAlphaFlags.UseAlpha))); mConvertedMaterialCache[state] = aiMaterialIndex; } aiMesh.MaterialIndex = aiMaterialIndex; // Add mesh to scene. Scene.Meshes.Add(aiMesh); } }
private static IGeometry ConvertDisplayGeometry(Assimp.Node curAiNode, Assimp.Matrix4x4 nodeWorldTransform, List <Assimp.Mesh> aiMeshes, List <MaterialBuildInfo> materialBuildInfos) { var nodeInverseWorldTransform = nodeWorldTransform; nodeInverseWorldTransform.Inverse(); var nodeInverseTransposeWorldTransform = nodeInverseWorldTransform; nodeInverseTransposeWorldTransform.Transpose(); var geometry = new Geometry(); // Convert meshes var vertexPositions = new List <Assimp.Vector3D>(); var vertexNormals = new List <Assimp.Vector3D>(); var vertexUVs = new List <Assimp.Vector3D>(); var vertexColors = new List <Assimp.Color4D>(); var lastRenderState = new MeshRenderState(); foreach (var aiMeshIndex in curAiNode.MeshIndices) { var aiMesh = aiMeshes[aiMeshIndex]; var material = materialBuildInfos[aiMesh.MaterialIndex]; var mesh = new Mesh(); var renderState = new MeshRenderState(); renderState.IndexFlags = IndexAttributeFlags.HasPosition; var useColors = false; var hasColors = aiMesh.HasVertexColors(0); var hasUVs = aiMesh.HasTextureCoords(0); var hasNormals = aiMesh.HasNormals; if (hasColors || !hasNormals) { renderState.IndexFlags |= IndexAttributeFlags.HasColor; useColors = true; } else { renderState.IndexFlags |= IndexAttributeFlags.HasNormal; } if (hasUVs) { renderState.IndexFlags |= IndexAttributeFlags.HasUV; } // Convert faces var triangleIndices = new Index[aiMesh.FaceCount * 3]; for (var i = 0; i < aiMesh.Faces.Count; i++) { var aiFace = aiMesh.Faces[i]; for (var j = 0; j < 3; j++) { var triangleIndicesIndex = (i * 3) + 2 - j; if (j >= aiFace.IndexCount) { triangleIndices[triangleIndicesIndex] = triangleIndices[triangleIndicesIndex + 1]; continue; } int aiFaceIndex = aiFace.Indices[j]; var position = aiMesh.Vertices[aiFaceIndex]; var positionIndex = vertexPositions.AddUnique(position); if (positionIndex > byte.MaxValue) { renderState.IndexFlags |= IndexAttributeFlags.Position16BitIndex; } var normalIndex = 0; var colorIndex = 0; var uvIndex = 0; if (useColors) { var color = hasColors ? aiMesh.VertexColorChannels[0][aiFaceIndex] : new Assimp.Color4D(); colorIndex = vertexColors.AddUnique(color); if (colorIndex > byte.MaxValue) { renderState.IndexFlags |= IndexAttributeFlags.Color16BitIndex; } } else { var normal = aiMesh.Normals[aiFaceIndex]; normalIndex = vertexNormals.AddUnique(normal); if (normalIndex > byte.MaxValue) { renderState.IndexFlags |= IndexAttributeFlags.Normal16BitIndex; } } if (hasUVs) { var uv = aiMesh.TextureCoordinateChannels[0][aiFaceIndex]; uvIndex = vertexUVs.AddUnique(uv); if (uvIndex > byte.MaxValue) { renderState.IndexFlags |= IndexAttributeFlags.UV16BitIndex; } } triangleIndices[triangleIndicesIndex] = new Index { PositionIndex = ( ushort )positionIndex, NormalIndex = ( ushort )normalIndex, ColorIndex = ( ushort )colorIndex, UVIndex = ( ushort )uvIndex }; } } // Build display list var displayList = new GXDisplayList(GXPrimitive.Triangles, triangleIndices); mesh.DisplayLists.Add(displayList); // Set up render params if (renderState.IndexFlags != lastRenderState.IndexFlags) { mesh.Parameters.Add(new IndexAttributeFlagsParam(renderState.IndexFlags)); } // Set up render lighting params { if (useColors) { renderState.LightingValue1 = 0x0b11; } else { renderState.LightingValue2 = 0x0011; } renderState.LightingValue2 = 1; if (renderState.LightingValue1 != lastRenderState.LightingValue1 || renderState.LightingValue2 != lastRenderState.LightingValue2) { mesh.Parameters.Add(new LightingParams() { Value1 = renderState.LightingValue1, Value2 = renderState.LightingValue2 }); } } // Set up render texture params { renderState.TextureId = ( ushort )material.TextureId; renderState.TileMode = TileMode.WrapU | TileMode.WrapV; if (renderState.TextureId != lastRenderState.TextureId || renderState.TileMode != lastRenderState.TileMode) { mesh.Parameters.Add(new TextureParams(renderState.TextureId, renderState.TileMode)); } } // Set up render mipmap params { renderState.MipMapValue1 = 0x104a; renderState.MipMapValue2 = 0; if (renderState.MipMapValue1 != lastRenderState.MipMapValue1 || renderState.MipMapValue2 != lastRenderState.MipMapValue2) { mesh.Parameters.Add(new MipMapParams { Value1 = renderState.MipMapValue1, Value2 = renderState.MipMapValue2 }); } } //if ( material.UseAlpha ) //{ // mesh.Parameters.Add( new BlendAlphaParam() { Flags = BlendAlphaFlags.UseAlpha } ); // geometry.TranslucentMeshes.Add( mesh ); //} //else //{ // geometry.OpaqueMeshes.Add( mesh ); //} geometry.OpaqueMeshes.Add(mesh); lastRenderState = renderState; } // Build vertex buffers if (vertexPositions.Count > 0) { Debug.Assert(vertexPositions.Count <= ushort.MaxValue); var localVertexPositions = vertexPositions.Select(x => { Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeWorldTransform); return(AssimpHelper.FromAssimp(x)); }).ToArray(); geometry.VertexBuffers.Add(new VertexPositionBuffer(localVertexPositions)); geometry.Bounds = BoundingSphere.Calculate(localVertexPositions); } if (vertexNormals.Count > 0) { Debug.Assert(vertexNormals.Count <= ushort.MaxValue); geometry.VertexBuffers.Add(new VertexNormalBuffer(vertexNormals.Select(x => { Assimp.Unmanaged.AssimpLibrary.Instance.TransformVecByMatrix4(ref x, ref nodeInverseTransposeWorldTransform); return(AssimpHelper.FromAssimp(x)); }).ToArray())); } if (vertexColors.Count > 0) { Debug.Assert(vertexColors.Count <= ushort.MaxValue); geometry.VertexBuffers.Add(new VertexColorBuffer(vertexColors.Select(AssimpHelper.FromAssimp).ToArray())); } if (vertexUVs.Count > 0) { Debug.Assert(vertexUVs.Count <= ushort.MaxValue); geometry.VertexBuffers.Add(new VertexUVBuffer(vertexUVs.Select(x => UVCodec.Encode255(AssimpHelper .FromAssimpAsVector2(x))) .ToArray())); } return(geometry); }
public StripIndexUVH(ushort index, Vector2 uv) { Index = index; UV = UVCodec.Encode1023(uv); }
public StripIndexUVN(ushort index, Vector2 uv) { Index = index; UV = UVCodec.Encode255(uv); }