private BabylonMesh ConvertUnityMeshToBabylon(Mesh mesh, Transform transform, GameObject gameObject, float progress, ref UnityMetaData metaData, ref List<BabylonExport.Entities.BabylonParticleSystem> particleSystems, ref List<UnityFlareSystem> lensFlares, ref string componentTags, BabylonMesh collisionMesh = null, Collider collider = null)
{
BabylonMesh babylonMesh = new BabylonMesh();
metaData.type = "Mesh";
if (!String.IsNullOrEmpty(componentTags))
{
babylonMesh.tags = componentTags;
}
ExporterWindow.ReportProgress(progress, "Exporting mesh: " + gameObject.name);
babylonMesh.name = gameObject.name;
babylonMesh.id = GetID(transform.gameObject);
var renderer = gameObject.GetComponent<Renderer>();
if (renderer != null)
{
babylonMesh.receiveShadows = renderer.receiveShadows;
}
babylonMesh.parentId = GetParentID(transform);
babylonMesh.position = transform.localPosition.ToFloat();
babylonMesh.rotation = new float[3];
babylonMesh.rotation[0] = transform.localRotation.eulerAngles.x * (float)Math.PI / 180;
babylonMesh.rotation[1] = transform.localRotation.eulerAngles.y * (float)Math.PI / 180;
babylonMesh.rotation[2] = transform.localRotation.eulerAngles.z * (float)Math.PI / 180;
babylonMesh.scaling = transform.localScale.ToFloat();
babylonMesh.checkCollisions = false;
// Collision mesh (With detail mesh fallback)
string collisionMeshId = null;
if (collider != null)
{
if (collisionMesh != null)
{
collisionMeshId = collisionMesh.id;
collisionMesh.parentId = babylonMesh.id;
collisionMesh.visibility = collider.isTrigger ? 0.25f : 0.5f;
collisionMesh.checkCollisions = (exportationOptions.ExportCollisions && collider.isTrigger == false);
}
else
{
babylonMesh.checkCollisions = exportationOptions.ExportCollisions;
}
}
metaData.properties["collisionMeshId"] = collisionMeshId;
if (mesh != null)
{
Tools.GenerateBabylonMeshData(mesh, babylonMesh, babylonScene, transform);
int index = 0;
if (mesh.boneWeights.Length == mesh.vertexCount)
{
babylonMesh.matricesIndices = new int[mesh.vertexCount];
babylonMesh.matricesWeights = new float[mesh.vertexCount * 4];
index = 0;
foreach (BoneWeight bw in mesh.boneWeights)
{
babylonMesh.matricesIndices[index] = (bw.boneIndex3 << 24) | (bw.boneIndex2 << 16) | (bw.boneIndex1 << 8) | bw.boneIndex0;
babylonMesh.matricesWeights[index * 4 + 0] = bw.weight0;
babylonMesh.matricesWeights[index * 4 + 1] = bw.weight1;
babylonMesh.matricesWeights[index * 4 + 2] = bw.weight2;
babylonMesh.matricesWeights[index * 4 + 3] = bw.weight3;
var totalWeight = bw.weight0 + bw.weight1 + bw.weight2 + bw.weight3;
if (Mathf.Abs(totalWeight - 1.0f) > 0.01f)
{
throw new Exception("Total bone weights is not normalized for: " + mesh);
}
index++;
}
}
index = 0;
if (renderer != null && renderer.sharedMaterial != null)
{
// Validate Multi Materials
if (mesh.subMeshCount > 1)
{
BabylonMultiMaterial bMultiMat;
string multiMatName = "";
for (int i = 0; i < renderer.sharedMaterials.Length; i++)
{
multiMatName += renderer.sharedMaterials[i].name;
}
if (!multiMatDictionary.ContainsKey(multiMatName))
{
bMultiMat = new BabylonMultiMaterial
{
materials = new string[mesh.subMeshCount],
id = Guid.NewGuid().ToString(),
name = multiMatName
};
for (int i = 0; i < renderer.sharedMaterials.Length; i++)
{
var sharedMaterial = renderer.sharedMaterials[i];
BabylonMaterial babylonMaterial;
babylonMaterial = DumpMaterial(sharedMaterial, renderer.lightmapIndex, renderer.lightmapScaleOffset);
bMultiMat.materials[i] = babylonMaterial.id;
}
if (mesh.subMeshCount > 1)
{
multiMatDictionary.Add(bMultiMat.name, bMultiMat);
}
}
else
{
bMultiMat = multiMatDictionary[multiMatName];
}
babylonMesh.materialId = bMultiMat.id;
babylonMesh.subMeshes = new BabylonSubMesh[mesh.subMeshCount];
var offset = 0;
for (int materialIndex = 0; materialIndex < mesh.subMeshCount; materialIndex++)
{
var unityTriangles = mesh.GetTriangles(materialIndex);
babylonMesh.subMeshes[materialIndex] = new BabylonSubMesh
{
verticesStart = 0,
verticesCount = mesh.vertexCount,
materialIndex = materialIndex,
indexStart = offset,
indexCount = unityTriangles.Length
};
offset += unityTriangles.Length;
}
}
else
{
babylonMesh.materialId = DumpMaterial(renderer.sharedMaterial, renderer.lightmapIndex, renderer.lightmapScaleOffset).id;
}
}
babylonMesh.metadata = metaData;
babylonScene.MeshesList.Add(babylonMesh);
// Animations
ExportAnimations(transform, babylonMesh);
if (IsRotationQuaternionAnimated(babylonMesh))
{
babylonMesh.rotationQuaternion = transform.localRotation.ToFloat();
}
// Lens Flares
ParseLensFlares(gameObject, babylonMesh.id, ref lensFlares);
// Particles Systems
ParseParticleSystems(gameObject, babylonMesh.id, ref particleSystems);
// Babylon Physics
if (exportationOptions.ExportPhysics)
{
var physics = gameObject.GetComponent<BabylonPhysicsState>();
if (physics != null)
{
babylonMesh.physicsMass = physics.mass;
babylonMesh.physicsFriction = physics.friction;
babylonMesh.physicsRestitution = physics.restitution;
babylonMesh.physicsImpostor = (int)physics.imposter;
}
}
}
return babylonMesh;
}