Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving & reloading precedurally generated textures #16678

Closed
supagu opened this issue Feb 14, 2018 · 12 comments
Closed

Saving & reloading precedurally generated textures #16678

supagu opened this issue Feb 14, 2018 · 12 comments

Comments

@supagu
Copy link
Contributor

supagu commented Feb 14, 2018

Godot 3.0
commit 187c40d
Linux Manjaro

I have a height map, which my game will then take and generate a normal map from. This normal map is then saved to disk so that next time the user plays the map, the normal map does not need to be generated again.

I can save out the normal map as a png just fine, but when it tries to load the png the next time I hit the error:

ERROR: _load_data: Condition ' header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T' ' is true. returned: ERR_FILE_CORRUPT
At: scene/resources/texture.cpp:430.

I suspect this has to do with godot using its own intermediate texture format.

@ghost ghost added this to the 3.1 milestone Feb 14, 2018
@ghost
Copy link

ghost commented Feb 14, 2018

What are the functions you use? Image.load(path) and ImageTexture.create_from_image(Image)?
Or ImageTexture.load(path)? or something else?

@supagu
Copy link
Contributor Author

supagu commented Feb 14, 2018

Ref< StreamTexture > n;
n.instance();
Error error = n->load(path);
if (error != OK)
return error;

@ghost
Copy link

ghost commented Feb 14, 2018

IIRC, load uses ResourceLoader, which can only load imported resources. Try loading it as Image and set it to ImageTexture.

Edit: might be related to #14879

Also just saw that you use StreamTexture. Try using ResourceSaver::save("... .tres") to save it instead of png.

@supagu
Copy link
Contributor Author

supagu commented Feb 14, 2018

The technique you suggested works

@supagu supagu closed this as completed Feb 14, 2018
@akien-mga akien-mga added archived and removed bug labels Feb 14, 2018
@supagu
Copy link
Contributor Author

supagu commented Feb 14, 2018

This is now causing me another problem, The Image and ImageTexture I create is being saved into my scene making it bloat out to like 200mb!

Seems I need to emulate what EditorFileSystem::get_singleton()->reimport_files(reimports); is doing
it casues the file to be processed into a stex file in the cache and can then be loaded using ResourceLoader::load,

but i can only do this in the editor

doing it this way stops some image being saved into my scene.

@supagu supagu reopened this Feb 14, 2018
@vnen
Copy link
Member

vnen commented Feb 14, 2018

But if it's generated, why are you adding it to the scene? You can create an ImageTexture, save it with ResourceSaver somewhere (so it's not part of your scene), then load it when needed,

@supagu
Copy link
Contributor Author

supagu commented Feb 14, 2018

I generate a normal map from a height map then save it. Then I load it and apply it to the terrain material so there map maker can see the normals.
Somehow in this chain of events and ImageTexture is saved into the map unless I use a StreamTexture

@supagu
Copy link
Contributor Author

supagu commented Feb 15, 2018

Right after a good sleep I have this almost working using the following steps:

  1. create a temporary ImageTexture fill it with the normal map image and call ResourceSaver::save, then ImageTexture can be released.

1a. if in the editor, I tell the editor to re-import the texture so I can load it ASAP:
if (EditorFileSystem::get_singleton()) { // cause the image to be "imported" Vector<String> reimports; reimports.push_back(path); EditorFileSystem::get_singleton()->reimport_files(reimports); }

  1. Image can then be loaded with:
    Ref n = ResourceLoader::load(path);

There is a 'gotcha' here in that because I am applying the procedural texture in the editor, it is being saved into the scene. So If I want to ship the map without this texture (as it is generated procedurally) then I would need to work around this last issue. The map won't load with a missing texture.

Is there a way I can force the level to load with missing dependencies as the editor somehow does?

@akien-mga
Copy link
Member

What's the status on this issue? If there's still a problem, could you provide a minimal reproduction project?

@akien-mga akien-mga removed this from the 3.2 milestone Nov 14, 2019
@supagu
Copy link
Contributor Author

supagu commented Nov 14, 2019

Sorry I have no idea if this is possible yet. I have just changed things so the texture is generated each time I load onto a level which is slow but works for me. Which is a better solution in my case.

The ideal solution is to be able to cache the generated texture on the end users machine by the game but at this stage editor code is compiled out AFAIK (ie ability to save out assets).

@Xrayez
Copy link
Contributor

Xrayez commented Sep 29, 2020

In theory you can bypass this by implementing your own custom EditorImportPlugin to import your normal maps alongside original textures, or find a way to override StreamTexture import, which might not be as trivial to do in comparison, these kind of limitations of ImageTexture vs StreamTexture are also expressed in #24222, I'm not sure what would be the recommended solution for this, but implementing custom editor plugins seems to be the most "straightforward" way to do this, of course that's up to a tutorial and the problem is more or less specific to the project.

What Godot could do at the core is possibly exposing some StreamTexture internal methods which are used to import textures in the same way, but that's probably up to a proposal anyway, for which you can open an issue at GIP repository.

@RedMser
Copy link
Contributor

RedMser commented May 31, 2024

I think this issue should be closed. It had no activity for years and to me looks unactionable / better fit for a proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants