protected override FbxScene CreateScene(FbxManager manager) { FbxScene scene = base.CreateScene(manager); // mesh shared by all instances FbxMesh sharedMesh = FbxMesh.Create(scene, m_meshName); FbxSurfaceMaterial sharedMaterial = FbxSurfacePhong.Create(scene, m_materialName); // add mesh to all nodes Queue <FbxNode> nodes = new Queue <FbxNode>(); for (int i = 0; i < scene.GetRootNode().GetChildCount(); i++) { nodes.Enqueue(scene.GetRootNode().GetChild(i)); } while (nodes.Count > 0) { FbxNode node = nodes.Dequeue(); node.SetNodeAttribute(sharedMesh); node.AddMaterial(sharedMaterial); for (int i = 0; i < node.GetChildCount(); i++) { nodes.Enqueue(node.GetChild(i)); } } return(scene); }
/// <summary> /// Export (and map) a Unity PBS material to FBX classic material /// </summary> public FbxSurfaceMaterial ExportMaterial(Material unityMaterial, FbxScene fbxScene) { if (Verbose) { Debug.Log(string.Format("exporting material {0}", unityMaterial.name)); } var materialName = unityMaterial ? unityMaterial.name : "DefaultMaterial"; if (MaterialMap.ContainsKey(materialName)) { return(MaterialMap [materialName]); } // We'll export either Phong or Lambert. Phong if it calls // itself specular, Lambert otherwise. var shader = unityMaterial ? unityMaterial.shader : null; bool specular = shader && shader.name.ToLower().Contains("specular"); var fbxMaterial = specular ? FbxSurfacePhong.Create(fbxScene, materialName) : FbxSurfaceLambert.Create(fbxScene, materialName); // Copy the flat colours over from Unity standard materials to FBX. fbxMaterial.Diffuse.Set(GetMaterialColor(unityMaterial, "_Color")); fbxMaterial.Emissive.Set(GetMaterialColor(unityMaterial, "_EmissionColor")); fbxMaterial.Ambient.Set(new FbxDouble3()); fbxMaterial.BumpFactor.Set(unityMaterial ? unityMaterial.GetFloat("_BumpScale") : 0); if (specular) { (fbxMaterial as FbxSurfacePhong).Specular.Set(GetMaterialColor(unityMaterial, "_SpecColor")); } // Export the textures from Unity standard materials to FBX. ExportTexture(unityMaterial, "_MainTex", fbxMaterial, FbxSurfaceMaterial.sDiffuse); ExportTexture(unityMaterial, "_EmissionMap", fbxMaterial, "emissive"); ExportTexture(unityMaterial, "_BumpMap", fbxMaterial, FbxSurfaceMaterial.sNormalMap); if (specular) { ExportTexture(unityMaterial, "_SpecGlosMap", fbxMaterial, FbxSurfaceMaterial.sSpecular); } MaterialMap.Add(materialName, fbxMaterial); return(fbxMaterial); }
protected override FbxScene CreateScene(FbxManager manager) { FbxScene scene = base.CreateScene(manager); // Set the UVs FbxMesh cubeMesh = scene.GetRootNode().GetChild(0).GetMesh(); FbxLayer fbxLayer = cubeMesh.GetLayer(0 /* default layer */); if (fbxLayer == null) { cubeMesh.CreateLayer(); fbxLayer = cubeMesh.GetLayer(0 /* default layer */); } using (var fbxLayerElement = FbxLayerElementUV.Create(cubeMesh, "UVSet")) { fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByPolygonVertex); fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eIndexToDirect); // set texture coordinates per vertex FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray(); for (int n = 0; n < 8; n++) { fbxElementArray.Add(new FbxVector2(n % 2, 1)); // TODO: switch to correct values } // For each face index, point to a texture uv FbxLayerElementArray fbxIndexArray = fbxLayerElement.GetIndexArray(); fbxIndexArray.SetCount(24); for (int vertIndex = 0; vertIndex < 24; vertIndex++) { fbxIndexArray.SetAt(vertIndex, vertIndex % 8); // TODO: switch to correct values } fbxLayer.SetUVs(fbxLayerElement, FbxLayerElement.EType.eTextureDiffuse); } // Create the material var fbxMaterial = FbxSurfacePhong.Create(scene, m_materialName); fbxMaterial.Diffuse.Set(new FbxColor(1, 1, 1)); fbxMaterial.Emissive.Set(new FbxColor(0.5, 0.1, 0.2)); fbxMaterial.Ambient.Set(new FbxDouble3(0.3, 0.4, 0)); fbxMaterial.BumpFactor.Set(0.6); fbxMaterial.Specular.Set(new FbxDouble3(0.8, 0.7, 0.9)); // Create and add the texture var fbxMaterialProperty = fbxMaterial.FindProperty(FbxSurfaceMaterial.sDiffuse); Assert.IsNotNull(fbxMaterialProperty); Assert.IsTrue(fbxMaterialProperty.IsValid()); var fbxTexture = FbxFileTexture.Create(fbxMaterial, FbxSurfaceMaterial.sDiffuse + "_Texture"); fbxTexture.SetFileName("/path/to/some/texture.jpg"); fbxTexture.SetTextureUse(FbxTexture.ETextureUse.eStandard); fbxTexture.SetMappingType(FbxTexture.EMappingType.eUV); fbxTexture.ConnectDstProperty(fbxMaterialProperty); scene.GetRootNode().GetChild(0).AddMaterial(fbxMaterial); return(scene); }