예제 #1
0
        // 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);
        }
예제 #2
0
        public new static FbxFileTexture Create(FbxObject pContainer, string pName)
        {
            global::System.IntPtr cPtr = FbxWrapperNativePINVOKE.FbxFileTexture_Create__SWIG_1(FbxObject.getCPtr(pContainer), pName);
            FbxFileTexture        ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxFileTexture(cPtr, false);

            return(ret);
        }
예제 #3
0
        public new static FbxFileTexture Create(FbxManager pManager, string pName)
        {
            global::System.IntPtr cPtr = fbx_wrapperPINVOKE.FbxFileTexture_Create__SWIG_0(FbxManager.getCPtr(pManager), pName);
            FbxFileTexture        ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxFileTexture(cPtr, false);

            return(ret);
        }
예제 #4
0
 public bool Equals(FbxFileTexture other)
 {
     if (object.ReferenceEquals(other, null))
     {
         return(false);
     }
     return(this.swigCPtr.Handle.Equals(other.swigCPtr.Handle));
 }
예제 #5
0
        public bool eq(FbxFileTexture pTexture)
        {
            bool ret = FbxWrapperNativePINVOKE.FbxFileTexture_eq(swigCPtr, FbxFileTexture.getCPtr(pTexture));

            if (FbxWrapperNativePINVOKE.SWIGPendingException.Pending)
            {
                throw FbxWrapperNativePINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
예제 #6
0
            /// <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);
            }
예제 #7
0
        public new static FbxFileTexture Create(FbxObject pContainer, string pName)
        {
            global::System.IntPtr cPtr = NativeMethods.FbxFileTexture_Create__SWIG_1(FbxObject.getCPtr(pContainer), pName);
            FbxFileTexture        ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxFileTexture(cPtr, false);

            if (NativeMethods.SWIGPendingException.Pending)
            {
                throw NativeMethods.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
예제 #8
0
        public new FbxFileTexture GetSrcObject_FileTexture()
        {
            global::System.IntPtr cPtr = NativeMethods.FbxPropertyDouble3_GetSrcObject_FileTexture(swigCPtr);
            FbxFileTexture        ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxFileTexture(cPtr, false);

            if (NativeMethods.SWIGPendingException.Pending)
            {
                throw NativeMethods.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FbxFileTexture obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }