Change the texture shape via script ?

Hey there !

I’m making an Editor script in order to tranfsorm my textures into Cubemap materials, I’m ok with it excepted for the texture shape that I need to change from 2D to Cube.
Anyone having the attribute or function to use for it ?

Thanks in advance :slight_smile:

I don’t even know what you’re trying to say when you say “texture shape”…?

My bad, should have illustrated it. Here it is :

(these are the default image properties)
I just want to change this parameter from 2D to Cube.
Still, thanks for caring.

If you have an existing Texture2D asset and want to convert it to a cubemap, there’s no built in support for that. You would have to write pretty much all of that functionality on your own (determine if you have a 360 panorama or 6 faces, and either apply the appropriate transforms or copy the appropriate regions into a new CubeMap asset).

If you have an image file on disk, you can modify the texture importer settings for that texture:

My hack, assuming you don’t need this at runtime, is to save out a Texture2D to disk and reimport using the texture importer rather than reinvent the wheel.

2 Likes

I don’t need to script a conversion to a Cubemap material (if it’s what you were talking about), I already have this part of the code (creating the material and applying a Cube texture), and it’s always a Cubemap, never a 6-faces.

The only action I still have to do now is what would be manually : click the Texture’s “Texture Shape” attribute, select “Cube” and press “Apply”. The rest will follow easily.

I’ll still watch at your suggestion, but it looks at me like it’s going a bit too far for my problem :face_with_spiral_eyes:

EDIT : Haven’t resolved yet, but the TextureImporter actually looks great… Its textureShape attribute is exactly what I were looking at, trying to find a way with that.

It works !

For anyone that could have the same need, here it is :

// Getting textures
var textures = Selection.GetFiltered(typeof(Texture), SelectionMode.Assets).Cast<Texture>();
foreach (var tex in textures)
{
        //  Texture path
        string path = AssetDatabase.GetAssetPath(tex);
              

        // If the texture dimensions is not "Cube", we convert it
        TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
        if (tex.dimension != UnityEngine.Rendering.TextureDimension.Cube)
        {
            importer.textureShape = TextureImporterShape.TextureCube;
            importer.SaveAndReimport();
        }
}

Thanks a lot bgolus, whatever your avatar is representing :slight_smile:

2 Likes

Turbo Snail from the Neverending Story, how could someone not know that? :open_mouth:

It looks stoned, but in the meantime it’s kinda cool…

It’s a racing snail.

Hello there! Is there a way to get the path from WWW.texture? I’m trying to change the shape from a online image which is Texture2D to texture shape “CUBE”?

Hi, what if the texture is captured in real time from device, will then be possible to convert?

So, “yes”, you can construct a cube map in real time. “No” you can’t change the “shape” since that’s purely a texture importer thing and that doesn’t do anything at runtime.