// Pass: // parameterName - // used for the FbxTexture name. Can be something arbitrary since as far as I know, // the texture node's name is unused by importers. private static void AddTextureToMaterial( FbxExportGlobals G, ExportFileReference fileRef, FbxSurfaceLambert fbxMaterial, string parameterName) { Debug.Assert(File.Exists(fileRef.m_originalLocation)); var destPath = Path.Combine(G.m_outputDir, fileRef.m_uri); if (!File.Exists(destPath)) { if (!FileUtils.InitializeDirectoryWithUserError(Path.GetDirectoryName(destPath))) { return; } File.Copy(fileRef.m_originalLocation, destPath); } // It's kind of weird that the parameter name is used for the texture node's name, // but as far as I can tell nobody cares about that name, so whatever. FbxFileTexture fbxTexture = FbxFileTexture.Create(G.m_scene, parameterName); fbxTexture.SetFileName(destPath); fbxTexture.SetTextureUse(FbxTexture.ETextureUse.eStandard); fbxTexture.SetMappingType(FbxTexture.EMappingType.eUV); fbxTexture.SetMaterialUse(FbxFileTexture.EMaterialUse.eModelMaterial); fbxTexture.UVSet.Set(new FbxString("uv0")); // It's also weird that we only ever assign to the Diffuse slot. // Shouldn't we be looking at the parameter name and assigning to Diffuse, Normal, etc // based on what we see? // TODO: check fbxMaterial.Diffuse.ConnectSrcObject(fbxTexture); fbxMaterial.TransparentColor.ConnectSrcObject(fbxTexture); }
/// <summary> /// Export an Unity Texture /// </summary> public void ExportTexture(Material unityMaterial, string unityPropName, FbxSurfaceMaterial fbxMaterial, string fbxPropName) { if (!unityMaterial) { return; } // Get the texture on this property, if any. if (!unityMaterial.HasProperty(unityPropName)) { return; } var unityTexture = unityMaterial.GetTexture(unityPropName); if (!unityTexture) { return; } // Find its filename var textureSourceFullPath = AssetDatabase.GetAssetPath(unityTexture); if (textureSourceFullPath == "") { return; } // get absolute filepath to texture textureSourceFullPath = Path.GetFullPath(textureSourceFullPath); if (Verbose) { Debug.Log(string.Format("{2}.{1} setting texture path {0}", textureSourceFullPath, fbxPropName, fbxMaterial.GetName())); } // Find the corresponding property on the fbx material. var fbxMaterialProperty = fbxMaterial.FindProperty(fbxPropName); if (fbxMaterialProperty == null || !fbxMaterialProperty.IsValid()) { Debug.Log("property not found"); return; } // Find or create an fbx texture and link it up to the fbx material. if (!TextureMap.ContainsKey(textureSourceFullPath)) { var fbxTexture = FbxFileTexture.Create(fbxMaterial, fbxPropName + "_Texture"); fbxTexture.SetFileName(textureSourceFullPath); fbxTexture.SetTextureUse(FbxTexture.ETextureUse.eStandard); fbxTexture.SetMappingType(FbxTexture.EMappingType.eUV); TextureMap.Add(textureSourceFullPath, fbxTexture); } TextureMap[textureSourceFullPath].ConnectDstProperty(fbxMaterialProperty); }
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); }
internal static FbxSurfaceMaterial CreateFbxMaterial( FbxExportGlobals G, string meshNamespace, IExportableMaterial exportableMaterial, HashSet <string> createdMaterialNames) { string materialName; if (exportableMaterial is BrushDescriptor) { // Toolkit uses this guid (in "N" format) to look up a BrushDescriptor. // See Toolkit's ModelImportSettings.GetDescriptorForStroke materialName = $"{exportableMaterial.UniqueName:N}_{meshNamespace}_{exportableMaterial.DurableName}"; } else if (exportableMaterial is DynamicExportableMaterial dem) { // Comes from {fbx,obj,gltf,...} import from {Poly, Media Library} // This is a customized version of a BrushDescriptor -- almost certainly // Pbr{Blend,Opaque}{Double,Single}Sided with maybe an added texture and // some of its params customized. // TBT will merge the material created by Unity for this FbxMaterial with // the premade material it has for the parent guid. materialName = $"{dem.Parent.m_Guid:N}_{meshNamespace}_{dem.DurableName}"; } else { Debug.LogWarning($"Unknown class {exportableMaterial.GetType().Name}"); materialName = $"{meshNamespace}_{exportableMaterial.DurableName}"; } // If only ExportFbx were a non-static class we could merge it with FbxExportGlobals materialName = ExportUtils.CreateUniqueName(materialName, createdMaterialNames); FbxSurfaceLambert material = FbxSurfaceLambert.Create(G.m_scene, materialName); material.Ambient.Set(new FbxDouble3(0, 0, 0)); material.Diffuse.Set(new FbxDouble3(1.0, 1.0, 1.0)); if (exportableMaterial.EmissiveFactor > 0) { material.EmissiveFactor.Set(exportableMaterial.EmissiveFactor); material.Emissive.Set(new FbxDouble3(1.0, 1.0, 1.0)); } if (exportableMaterial.BlendMode != ExportableMaterialBlendMode.None) { var blendMode = FbxProperty.Create(material, Globals.FbxStringDT, "BlendMode"); switch (exportableMaterial.BlendMode) { case ExportableMaterialBlendMode.AlphaMask: blendMode.SetString(new FbxString("AlphaMask")); material.TransparencyFactor.Set(0.2); break; case ExportableMaterialBlendMode.AdditiveBlend: blendMode.SetString(new FbxString("AdditiveBlend")); break; } } // Export the texture if (exportableMaterial.HasExportTexture()) { // This is not perfectly unique, but it is good enough for fbx export // better would be to use <durable>_<guid> but that's ugly, and nobody uses // the textures anyway, so... let's leave well enough alone for now. string albedoTextureName = exportableMaterial.DurableName; var fullTextureDir = Path.Combine(G.m_outputDir, kRelativeTextureDir); if (!Directory.Exists(fullTextureDir)) { if (!FileUtils.InitializeDirectoryWithUserError(fullTextureDir)) { throw new IOException("Cannot write textures"); } } string src = exportableMaterial.GetExportTextureFilename(); var textureFileName = albedoTextureName + ".png"; var textureFilePath = Path.Combine(fullTextureDir, textureFileName); FileInfo srcInfo = new FileInfo(src); if (srcInfo.Exists && !new FileInfo(textureFilePath).Exists) { srcInfo.CopyTo(textureFilePath); } FbxFileTexture texture = FbxFileTexture.Create(G.m_scene, albedoTextureName + "_texture"); texture.SetFileName(textureFilePath); texture.SetTextureUse(FbxTexture.ETextureUse.eStandard); texture.SetMappingType(FbxTexture.EMappingType.eUV); texture.SetMaterialUse(FbxFileTexture.EMaterialUse.eModelMaterial); texture.UVSet.Set(new FbxString("uv0")); material.Diffuse.ConnectSrcObject(texture); material.TransparentColor.ConnectSrcObject(texture); } else { foreach (var kvp in exportableMaterial.TextureUris) { string parameterName = kvp.Key; string textureUri = kvp.Value; if (ExportFileReference.IsHttp(textureUri)) { // fbx can't deal with http references to textures continue; } ExportFileReference fileRef = ExportFileReference.GetOrCreateSafeLocal( G.m_disambiguationContext, textureUri, exportableMaterial.UriBase, $"{meshNamespace}_{Path.GetFileName(textureUri)}"); AddTextureToMaterial(G, fileRef, material, parameterName); } } return(material); }