Gildor's Forums

Author Topic: GLTF export - link texture to model?  (Read 746 times)
PeterBrown
Newbie
*
Posts: 5


View Profile
GLTF export - link texture to model?
« on: January 18, 2021, 13:13 »

Hello.
I am currently trying to create a "fan movie" of a game for fun, where i wanna use the assets from that game. UEviewer is already doing a great job exporting everything i need to a separated GLTF and TGA file so I can use it inside unity3d, but i need to apply all the textures to the 3d model material by hand.

I already did that for roughly 100 objects but got 500 more infront of me and i am wondering if there is an easier or automated way to link the textures to the right 3d model?

thanks!
Logged
Gildor
Administrator
Hero Member
*****
Posts: 7973



View Profile WWW
Re: GLTF export - link texture to model?
« Reply #1 on: January 18, 2021, 13:16 »

It is a planned feature, but I have no clear idea yet how to do that, so it's "on hold".
Logged
PeterBrown
Newbie
*
Posts: 5


View Profile
Re: GLTF export - link texture to model?
« Reply #2 on: January 18, 2021, 16:37 »

thanks for letting me know Smiley i wrote a small unity script that is crawling threw the textures of your project and crates a new material. it highly depends on how the assets and textures are named. so it wont work for everyone, but still. here it is:

Code:
using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class MaterialImporter : MonoBehaviour
{
    public string basecolor;
    public string normal;
    public string metallic;
    private void Reset() {
        Search();
    }
    public void Search() {

        //  Debug.Log("lets go!" + gameObject.name);
        basecolor = null;
        normal = null;
        metallic = null;
        string[] results;

        results = AssetDatabase.FindAssets(gameObject.name);
        foreach (string guid in results) {           
            // Debug.Log("testI: " + AssetDatabase.GUIDToAssetPath(guid));
        }

       
        foreach (string guidone in results) {
            if (AssetDatabase.GUIDToAssetPath(guidone).Contains("basecolor")) {
                if (basecolor == null) {
                    basecolor = AssetDatabase.GUIDToAssetPath(guidone);
                    Debug.Log("base: " + AssetDatabase.GUIDToAssetPath(guidone));
                }             
            }
            if (AssetDatabase.GUIDToAssetPath(guidone).Contains("normal")) {
               // normal = guidone;
                if (normal == null) {
                    normal = AssetDatabase.GUIDToAssetPath(guidone);
                     Debug.Log("normal: " + AssetDatabase.GUIDToAssetPath(guidone));
                }
            }
            if (AssetDatabase.GUIDToAssetPath(guidone).Contains("metallic")) {
                //metallic = guidone;
                if (metallic == null) {
                    metallic = AssetDatabase.GUIDToAssetPath(guidone);
                      Debug.Log("metallic: " + AssetDatabase.GUIDToAssetPath(guidone));
                }
            }
        }

        Material material = new Material(Shader.Find("Standard"));
        AssetDatabase.CreateAsset(material, "Assets/" + gameObject.name + ".mat");

        var rend = gameObject.GetComponent<Renderer>();

        TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(normal);
        importer.textureType = TextureImporterType.NormalMap;

        Texture2D basecl = (Texture2D)AssetDatabase.LoadAssetAtPath(basecolor, typeof(Texture2D));
        Texture2D normalmap = (Texture2D)AssetDatabase.LoadAssetAtPath(normal, typeof(Texture2D));
        Texture2D metall = (Texture2D)AssetDatabase.LoadAssetAtPath(metallic, typeof(Texture2D));
     

        Material m = (Material)AssetDatabase.LoadAssetAtPath("Assets/" + gameObject.name + ".mat", typeof(Material));
   
        m.SetTexture("_MainTex", basecl);
        m.SetTexture("_BumpMap", normalmap);
        m.SetTexture("_MetallicGlossMap", metall);

        m.SetFloat("_Glossiness", 0);
        m.SetFloat("_Metallic", 0);
        m.SetFloat("_GlossMapScale", 0.5f);
        rend.sharedMaterial = m;

        Prefab();
     
    }
    public void Prefab() {
   
            // and name it as the GameObject's name with the .Prefab format
            string localPath = "Assets/Prefabs" + gameObject.name + ".prefab";

        // Make sure the file name is unique, in case an existing Prefab has the same name.
        localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);

        // Create the new Prefab.
        PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction);

       // DestroyImmediate(gameObject);
    }
 
}


Logged
Guilty
Newbie
*
Posts: 9


View Profile
Re: GLTF export - link texture to model?
« Reply #3 on: December 20, 2023, 04:04 »

It is a planned feature, but I have no clear idea yet how to do that, so it's "on hold".

Is this still on hold? I've exported FF7 Intergrade models as gltf with animations, but they import to Blender without textures; rather, the shaders are Color Attributes.


* Screenshot 2023-12-19 160202.png (496.98 KB, 1920x1035 - viewed 63 times.)
Logged
spiritovod
Global Moderator
Hero Member
*****
Posts: 1927


View Profile
Re: GLTF export - link texture to model?
« Reply #4 on: December 21, 2023, 16:32 »

@Guilty: Applying materials is supported in ActorX importer plugin for 3ds max (for psk export), it's available here. Not sure if there are similar solution for blender, I thought textures would be built-in for gltf, but the game in question is using complex materials with shaders anyway, some of them are not even parsed by umodel at the moment (you could blame me for that actually). It's hardly possible to do anything with that aside from trying to re-create materials manually from their properties.
Logged
Jump to:  

Powered by SMF | SMF © 2006-2009, Simple Machines LLC
Leviathan design by Bloc | XHTML | CSS