Custom Texture2D Troubles (bump)
Asked Answered
S

6

0

So I am trying to programmatically generate an image, via Texture2D, for use in a shader. I do this with a custom Texture2D inherited resource that writes to an ImageTexture and outputs from that. I am able to get my custom texture to display in a TextureRect, but get weird results elsewhere.

It will not display at all if used as the Albedo of material. When plugged into the shader it was designed for, I get an inexplicable checkered pattern. When I tried to display it in a simpler shader, I found that the texture refuses to load into a Sampler2D uniform. (though it will load into a Sampler2D array uniform? See gifs.) All this leads me to believe that I am missing some critical information about how Texture2D works. Does someone with more knowledge of the innerworkings of Godot know where I can go for more information?


Thanks,

Scaler answered 6/5, 2023 at 16:19 Comment(0)
Y
0

Scaler What happens if you inherit from ImageTexture instead of Texture2D?

Yamen answered 6/5, 2023 at 17:17 Comment(0)
S
0

Yamen This does actually work when I run it! However, it now displays nonsensical output in the editor. This solution has deepened my feeling that I don't know what's going on, but a solution is a solution.

Scaler answered 6/5, 2023 at 18:24 Comment(0)
S
0

Yamen I spoke too soon actually. Although the texture displays, it only uses the default settings and will not respond to parameters changed in the editor. It makes the think the issue is to do with when and how resources are initialized.

Scaler answered 6/5, 2023 at 18:52 Comment(0)
Y
0

Scaler What do you mean by 'nonsensical output'?
If you want the object resource to rebuild itself when some property is changed in the editor during runtime, you can make a setter method for that property and explicitly re-call the constructor (or any other builder method) from there.

Yamen answered 6/5, 2023 at 18:55 Comment(0)
S
0

Yamen By "nonsensical output", a mean a purple color of no apparent origin is show.

I don't want it to change during runtime, I just want changes made to @export-ed properties to have an effect. I assumed that _init would be called after exports are set, but it appears I was wrong.

Scaler answered 6/5, 2023 at 20:17 Comment(0)
Y
0

Scaler
If you do what I described above and set the script to be a tool, the resource will be re-initialized when changing the property value in editor gui:

@tool
class_name MyTexture
extends ImageTexture

@export var value: int = 100: set = set_value

func set_value(v):
	value = v
	_init()

func _init():
	var img: Image = Image.new()
	img.set_data(1, 1, false,Image.FORMAT_RGBA8, PackedByteArray([value, 0, 0, 255]))
	img.resize(64, 64)
	set_image(img)

You'll most likely need to restart the project for this to take effect.

Yamen answered 6/5, 2023 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.