123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using Autodesk.Max;
- using BabylonExport.Entities;
- using MaxSharp;
- namespace Max2Babylon
- {
- partial class BabylonExporter
- {
- private BabylonMesh ExportMesh(Node meshNode, BabylonScene babylonScene)
- {
- var babylonMesh = new BabylonMesh();
- int vx1, vx2, vx3;
- RaiseMessage(meshNode.Name, true);
- babylonMesh.name = meshNode.Name;
- babylonMesh.id = meshNode.GetGuid().ToString();
- if (meshNode.HasParent())
- {
- babylonMesh.parentId = meshNode.Parent.GetGuid().ToString();
- }
- // Misc.
- babylonMesh.isVisible = meshNode._Node.Renderable == 1;
- babylonMesh.pickable = meshNode._Node.GetBoolProperty("babylonjs_checkpickable");
- babylonMesh.receiveShadows = meshNode._Node.RcvShadows == 1;
- // Collisions
- babylonMesh.checkCollisions = meshNode._Node.GetBoolProperty("babylonjs_checkcollisions");
- // Position / rotation / scaling
- var wm = meshNode.GetWorldMatrix(0, meshNode.HasParent());
- babylonMesh.position = wm.Trans.ToArraySwitched();
- var parts = Loader.Global.AffineParts.Create();
- Loader.Global.DecompAffine(wm, parts);
- //var rotate = new float[3];
- //IntPtr xPtr = Marshal.AllocHGlobal(sizeof(float));
- //IntPtr yPtr = Marshal.AllocHGlobal(sizeof(float));
- //IntPtr zPtr = Marshal.AllocHGlobal(sizeof(float));
- //parts.Q.GetEuler(xPtr, yPtr, zPtr);
- //Marshal.Copy(xPtr, rotate, 0, 1);
- //Marshal.Copy(yPtr, rotate, 1, 1);
- //Marshal.Copy(zPtr, rotate, 2, 1);
- //var temp = -rotate[1];
- //rotate[0] = rotate[0] * parts.F;
- //rotate[1] = -rotate[2] * parts.F;
- //rotate[2] = temp * parts.F;
- //babylonMesh.rotation = rotate;
- babylonMesh.rotationQuaternion = parts.Q.ToArray();
- babylonMesh.scaling = parts.K.ToArraySwitched();
- if (wm.Parity)
- {
- vx1 = 2;
- vx2 = 1;
- vx3 = 0;
- }
- else
- {
- vx1 = 0;
- vx2 = 1;
- vx3 = 2;
- }
- // Pivot
- //var pivotMatrix = Matrix3.Identity._IMatrix3;
- //pivotMatrix.PreTranslate(meshNode._Node.ObjOffsetPos);
- //Loader.Global.PreRotateMatrix(pivotMatrix, meshNode._Node.ObjOffsetRot);
- //Loader.Global.ApplyScaling(pivotMatrix, meshNode._Node.ObjOffsetScale);
- //babylonMesh.localMatrix = pivotMatrix.ToArray();
- // Mesh
- var objectState = meshNode._Node.EvalWorldState(0, false);
- var mesh = objectState.Obj.GetMesh();
- var computedMesh = meshNode.GetMesh();
- if (mesh != null)
- {
- mesh.BuildNormals();
- if (mesh.NumFaces < 1)
- {
- RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name));
- }
- if (mesh.NumVerts < 3)
- {
- RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name));
- }
- if (mesh.NumVerts >= 65536)
- {
- RaiseError(string.Format("Mesh {0} has too many vertices (more than 65535)", babylonMesh.name));
- }
- // Material
- var mtl = meshNode.Material;
- var multiMatsCount = 1;
- if (mtl != null)
- {
- babylonMesh.materialId = mtl.GetGuid().ToString();
- if (!referencedMaterials.Contains(mtl))
- {
- referencedMaterials.Add(mtl);
- }
- multiMatsCount = Math.Max(mtl.NumSubMaterials, 1);
- }
- babylonMesh.visibility = meshNode._Node.GetVisibility(0, Interval.Forever._IInterval);
- var vertices = new List<GlobalVertex>();
- var indices = new List<int>();
- var matIDs = new List<int>();
- var hasUV = mesh.NumTVerts > 0;
- var hasUV2 = mesh.GetNumMapVerts(2) > 0;
- for (var face = 0; face < mesh.NumFaces; face++)
- {
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx1, vertices, hasUV, hasUV2));
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx2, vertices, hasUV, hasUV2));
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx3, vertices, hasUV, hasUV2));
- matIDs.Add(mesh.Faces[face].MatID % multiMatsCount);
- }
- // Buffers
- babylonMesh.positions = vertices.SelectMany(v => v.Position.ToArraySwitched()).ToArray();
- babylonMesh.normals = vertices.SelectMany(v => v.Normal.ToArraySwitched()).ToArray();
- if (hasUV)
- {
- babylonMesh.uvs = vertices.SelectMany(v => v.UV.ToArray()).ToArray();
- }
- if (hasUV2)
- {
- babylonMesh.uvs2 = vertices.SelectMany(v => v.UV2.ToArray()).ToArray();
- }
- // Submeshes
- var sortedIndices = new List<int>();
- var subMeshes = new List<BabylonSubMesh>();
- var indexStart = 0;
- for (var index = 0; index < multiMatsCount; index++)
- {
- var subMesh = new BabylonSubMesh();
- var indexCount = 0;
- var minVertexIndex = int.MaxValue;
- var maxVertexIndex = int.MinValue;
- subMesh.indexStart = indexStart;
- subMesh.materialIndex = index;
-
- for (var face = 0; face < matIDs.Count; face++)
- {
- if (matIDs[face] == index)
- {
- var a = indices[3*face];
- var b = indices[3*face + 1];
- var c = indices[3*face + 2];
- sortedIndices.Add(a);
- sortedIndices.Add(b);
- sortedIndices.Add(c);
- indexCount += 3;
- if (a < minVertexIndex)
- {
- minVertexIndex = a;
- }
-
- if (b < minVertexIndex)
- {
- minVertexIndex = b;
- }
- if (c < minVertexIndex)
- {
- minVertexIndex = c;
- }
- if (a > maxVertexIndex)
- {
- maxVertexIndex = a;
- }
- if (b > maxVertexIndex)
- {
- maxVertexIndex = b;
- }
- if (c > maxVertexIndex)
- {
- maxVertexIndex = c;
- }
- }
- }
- subMesh.indexCount = indexCount;
- subMesh.verticesStart = minVertexIndex;
- subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
- indexStart += indexCount;
- subMeshes.Add(subMesh);
- }
- babylonMesh.subMeshes = subMeshes.ToArray();
- // Buffers - Indices
- babylonMesh.indices = sortedIndices.ToArray();
- }
- babylonScene.MeshesList.Add(babylonMesh);
- return babylonMesh;
- }
- int CreateGlobalVertex(IMesh mesh, Mesh computedMesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2)
- {
- var vertexIndex = (int)mesh.Faces[face].V[facePart];
- var vertex = new GlobalVertex
- {
- Position = mesh.Verts[vertexIndex],
- Normal = computedMesh.vnormals[vertexIndex]._IPoint3
- };
- if (hasUV)
- {
- var tvertexIndex = (int)mesh.TvFace[face].T[facePart];
- vertex.UV = Loader.Global.Point2.Create(mesh.TVerts[tvertexIndex].X, mesh.TVerts[tvertexIndex].Y);
- }
- if (hasUV2)
- {
- var tvertexIndex = (int)mesh.MapFaces(2)[face].T[facePart];
- vertex.UV2 = Loader.Global.Point2.Create(mesh.MapVerts(2)[tvertexIndex].X, mesh.MapVerts(2)[tvertexIndex].Y);
- }
- var index = vertices.IndexOf(vertex);
- if (index > -1)
- {
- return index;
- }
- vertices.Add(vertex);
- return vertices.Count - 1;
- }
- }
- }
|