123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.Max;
- using BabylonExport.Entities;
- using MaxSharp;
- using System.Runtime.InteropServices;
- namespace Max2Babylon
- {
- partial class BabylonExporter
- {
- private int bonesCount;
- private void ExportMesh(Node meshNode, BabylonScene babylonScene)
- {
- if (meshNode._Node.GetBoolProperty("babylonjs_noexport"))
- {
- return;
- }
- if (!ExportHiddenObjects && !meshNode.Visible)
- {
- return;
- }
- var babylonMesh = new BabylonMesh();
- int vx1, vx2, vx3;
- 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;
- babylonMesh.showBoundingBox = meshNode._Node.GetBoolProperty("babylonjs_showboundingbox");
- babylonMesh.showSubMeshesBoundingBox = meshNode._Node.GetBoolProperty("babylonjs_showsubmeshesboundingbox");
- // Collisions
- babylonMesh.checkCollisions = meshNode._Node.GetBoolProperty("babylonjs_checkcollisions");
- // Skin
- var skin = GetSkinModifier(meshNode._Node);
- if (skin != null)
- {
- babylonMesh.skeletonId = skins.IndexOf(skin);
- bonesCount = skin.NumBones;
- }
- // 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);
- if (exportQuaternionsInsteadOfEulers)
- {
- babylonMesh.rotationQuaternion = parts.Q.ToArray();
- }
- else
- {
- 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.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.pivotMatrix = pivotMatrix.ToArray();
- // Mesh
- var objectState = meshNode._Node.EvalWorldState(0, false);
- var triObject = objectState.Obj.GetMesh();
- var mesh = triObject != null ? triObject.Mesh : null;
- var computedMesh = meshNode.GetMesh();
- RaiseMessage(meshNode.Name, 1);
- if (mesh != null)
- {
- mesh.BuildNormals();
- if (mesh.NumFaces < 1)
- {
- RaiseError(string.Format("Mesh {0} has no face", babylonMesh.name), 2);
- }
- if (mesh.NumVerts < 3)
- {
- RaiseError(string.Format("Mesh {0} has not enough vertices", babylonMesh.name), 2);
- }
- if (mesh.NumVerts >= 65536)
- {
- RaiseError(string.Format("Mesh {0} has too many vertices (more than 65535)", babylonMesh.name), 2);
- }
- // 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;
- var optimizeVertices = meshNode._Node.GetBoolProperty("babylonjs_optimizevertices");
- // Skin
- IISkinContextData skinContext = null;
- if (skin != null)
- {
- skinContext = skin.GetContextInterface(meshNode._Node);
- }
- // Compute normals
- VNormal[] vnorms = null;
- List<GlobalVertex>[] verticesAlreadyExported = null;
- if (!optimizeVertices)
- {
- vnorms = Tools.ComputeNormals(mesh);
- }
- else
- {
- verticesAlreadyExported = new List<GlobalVertex>[mesh.NumVerts];
- }
- for (var face = 0; face < mesh.NumFaces; face++)
- {
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx1, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx2, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
- indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx3, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
- matIDs.Add(mesh.Faces[face].MatID % multiMatsCount);
- CheckCancelled();
- }
- if (vertices.Count >= 65536)
- {
- RaiseError(string.Format("Mesh {0} has too many vertices: {1} (limit is 65535)", babylonMesh.name, vertices.Count), 2);
- if (!optimizeVertices)
- {
- RaiseError("You can try to optimize your object using [Try to optimize vertices] option", 2);
- }
- }
- RaiseMessage(string.Format("{0} vertices, {1} faces", vertices.Count, indices.Count / 3), 2);
- // 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();
- }
- if (skin != null)
- {
- babylonMesh.matricesWeights = vertices.SelectMany(v => v.Weights.ToArray()).ToArray();
- babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).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;
- }
- }
- }
- if (indexCount != 0)
- {
- subMesh.indexCount = indexCount;
- subMesh.verticesStart = minVertexIndex;
- subMesh.verticesCount = maxVertexIndex - minVertexIndex + 1;
- indexStart += indexCount;
- subMeshes.Add(subMesh);
- }
- CheckCancelled();
- }
- babylonMesh.subMeshes = subMeshes.ToArray();
- // Buffers - Indices
- babylonMesh.indices = sortedIndices.ToArray();
- triObject.Dispose();
- }
- // Animations
- var animations = new List<BabylonAnimation>();
- if (!ExportVector3Controller(meshNode._Node.TMController.PositionController, "position", animations))
- {
- ExportVector3Animation("position", animations, key =>
- {
- var worldMatrix = meshNode.GetWorldMatrix(key, meshNode.HasParent());
- return worldMatrix.Trans.ToArraySwitched();
- });
- }
- if (!ExportQuaternionController(meshNode._Node.TMController.RotationController, "rotationQuaternion", animations))
- {
- ExportQuaternionAnimation("rotationQuaternion", animations, key =>
- {
- var worldMatrix = meshNode.GetWorldMatrix(key, meshNode.HasParent());
- var affineParts = Loader.Global.AffineParts.Create();
- Loader.Global.DecompAffine(worldMatrix, affineParts);
- return affineParts.Q.ToArray();
- });
- }
- if (!ExportVector3Controller(meshNode._Node.TMController.ScaleController, "scaling", animations))
- {
- ExportVector3Animation("scaling", animations, key =>
- {
- var worldMatrix = meshNode.GetWorldMatrix(key, meshNode.HasParent());
- var affineParts = Loader.Global.AffineParts.Create();
- Loader.Global.DecompAffine(worldMatrix, affineParts);
- return affineParts.K.ToArraySwitched();
- });
- }
- if (!ExportFloatController(meshNode._Node.VisController, "visibility", animations))
- {
- ExportFloatAnimation("visibility", animations, key => new[] { meshNode._Node.GetVisibility(key, Interval.Forever._IInterval) });
- }
- babylonMesh.animations = animations.ToArray();
- if (meshNode._Node.GetBoolProperty("babylonjs_autoanimate"))
- {
- babylonMesh.autoAnimate = true;
- babylonMesh.autoAnimateFrom = (int)meshNode._Node.GetFloatProperty("babylonjs_autoanimate_from");
- babylonMesh.autoAnimateTo = (int)meshNode._Node.GetFloatProperty("babylonjs_autoanimate_to");
- babylonMesh.autoAnimateLoop = meshNode._Node.GetBoolProperty("babylonjs_autoanimateloop");
- }
- babylonScene.MeshesList.Add(babylonMesh);
- }
- int CreateGlobalVertex(IMesh mesh, Mesh computedMesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, VNormal[] vnorms, List<GlobalVertex>[] verticesAlreadyExported, IISkinContextData skinContextData)
- {
- var faceObject = mesh.Faces[face];
- var vertexIndex = (int)faceObject.V[facePart];
- var vertex = new GlobalVertex
- {
- BaseIndex = vertexIndex,
- Position = mesh.Verts[vertexIndex],
- Normal = (vnorms != null) ? vnorms[vertexIndex].GetNormal(faceObject.SmGroup) : 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);
- }
- if (skinContextData != null)
- {
- float weight0 = 0;
- float weight1 = 0;
- float weight2 = 0;
- int bone0 = bonesCount;
- int bone1 = bonesCount;
- int bone2 = bonesCount;
- int bone3 = bonesCount;
- int nbBones = skinContextData.GetNumAssignedBones(vertexIndex);
- if (nbBones > 0)
- {
- bone0 = skinContextData.GetAssignedBone(vertexIndex, 0);
- weight0 = skinContextData.GetBoneWeight(vertexIndex, 0);
- }
- if (nbBones > 1)
- {
- bone1 = skinContextData.GetAssignedBone(vertexIndex, 1);
- weight1 = skinContextData.GetBoneWeight(vertexIndex, 1);
- }
- if (nbBones > 2)
- {
- bone2 = skinContextData.GetAssignedBone(vertexIndex, 2);
- weight2 = skinContextData.GetBoneWeight(vertexIndex, 2);
- }
- if (nbBones > 3)
- {
- bone3 = skinContextData.GetAssignedBone(vertexIndex, 3);
- }
- if (nbBones == 0)
- {
- weight0 = 1.0f;
- bone0 = bonesCount;
- }
- if (nbBones > 4)
- {
- RaiseError("Too many bones influences per vertex: " + nbBones + ". Babylon.js only support 4 bones influences per vertex.", 2);
- }
- vertex.Weights = Loader.Global.Point4.Create(weight0, weight1, weight2, 1.0 - weight0 - weight1 - weight2);
- vertex.BonesIndices = (bone3 << 24) | (bone2 << 16) | (bone1 << 8) | bone0;
- }
- if (verticesAlreadyExported != null)
- {
- if (verticesAlreadyExported[vertexIndex] != null)
- {
- var index = verticesAlreadyExported[vertexIndex].IndexOf(vertex);
- if (index > -1)
- {
- return verticesAlreadyExported[vertexIndex][index].CurrentIndex;
- }
- }
- else
- {
- verticesAlreadyExported[vertexIndex] = new List<GlobalVertex>();
- }
- vertex.CurrentIndex = vertices.Count;
- verticesAlreadyExported[vertexIndex].Add(vertex);
- }
- vertices.Add(vertex);
- return vertices.Count - 1;
- }
- }
- }
|