godot icon indicating copy to clipboard operation
godot copied to clipboard

Can I use part of a sprite as the light texture?

Open CrayonChimp opened this issue 1 year ago • 9 comments

Tested versions

4.3rc2

System information

MacOS Monterey 12.7.5

Issue description

Sorry, I know this isn't the right place, but I've asked around and no one has given me a definitive answer.

image When I try to add light textures to my light nodes, I find that light textures are a bit different from Sprite2D textures that can be cut from large sprites. Currently, I create separate files for each light texture, but this is a bit inconvenient because I have too many light textures and if I create a file for each texture, there will be a lot of files. My question is, am I doing something wrong or is it really only possible to generate separate texture files for each light node?

Steps to reproduce

N/A

Minimal reproduction project (MRP)

N/A

CrayonChimp avatar Aug 14 '24 04:08 CrayonChimp

What about AtlasTexture?

Summersay415 avatar Aug 14 '24 04:08 Summersay415

What about AtlasTexture?

I tried this, but for some reason unknown to me the lights don't show up when I use an AtlasTexture.

CrayonChimp avatar Aug 14 '24 08:08 CrayonChimp

This is a limitation that will be enforced, see:

  • https://github.com/godotengine/godot/pull/88349

Keeping this open to track this specific case and so it can be documented

AThousandShips avatar Aug 14 '24 09:08 AThousandShips

This is a limitation that will be enforced, see:

Keeping this open to track this specific case and so it can be documented

So currently I can't clip part of a sprite to a Light2D texture, right?

CrayonChimp avatar Aug 14 '24 10:08 CrayonChimp

So currently I can't clip part of a sprite to a Light2D texture, right?

Correct. It probably won't change in future either.

A workaround could be to create an adapter resource like

@tool
class MyAdapter
extends Texture2D

static var _cache: Dictionary # store textures using weakref()

@export var source: AtlasTexture
@export var some_way_to_select_subimage

var _tex: Texture2D

func _draw(...):
  if not _tex:
   _tex = _create_or_load_from_cache(
      source, some_way_to_select_subimage)
  _tex.draw(...)

# and the other required Texture2D virtual methods
...

huwpascoe avatar Aug 14 '24 13:08 huwpascoe

So currently I can't clip part of a sprite to a Light2D texture, right?

Correct. It probably won't change in future either.

A workaround could be to create an adapter resource like

@tool
class MyAdapter
extends Texture2D

static var _cache: Dictionary # store textures using weakref()

@export var source: AtlasTexture
@export var some_way_to_select_subimage

var _tex: Texture2D

func _draw(...):
  if not _tex:
   _tex = _create_or_load_from_cache(
      source, some_way_to_select_subimage)
  _tex.draw(...)

# and the other required Texture2D virtual methods
...

Then I think my project is ready for like a thousand png files.

CrayonChimp avatar Aug 14 '24 13:08 CrayonChimp

Then I think my project is ready for like a thousand png files.

In terms of memory usage, it'll be the same as using a single large PNG file anyway. File size on disk will also nearly be the same - the only difference is that lossless compression will be a bit less efficient, and there will be more PNG headers. Either way, I expect the difference to be pretty small (less than a MB compared to a single large atlas).

Calinou avatar Aug 15 '24 00:08 Calinou

In terms of memory usage, it'll be the same as using a single large PNG file anyway. File size on disk will also nearly be the same - the only difference is that lossless compression will be a bit less efficient, and there will be more PNG headers. Either way, I expect the difference to be pretty small (less than a MB compared to a single large atlas).

I'm not really worried about the file size, it's just that having too many files makes it hard to organize the project. Too many small files sometimes make it hard to find the files for my light node.

CrayonChimp avatar Aug 15 '24 01:08 CrayonChimp

image

I really want light textures to use partial sprites. Take this stall for example, the lantern can illuminate the player, but the shop sign can't, they have different item_cull_mask, so I have to create two files. If I want to add more complex lighting to this stall, maybe I need more light files to achieve a small stall. And my game will have many such stalls, so it is foreseeable that the file system will cause a mess. I'm really curious why light texture can't use partial sprites like Sprite2D, is there any technical difficulty?

CrayonChimp avatar Aug 17 '24 17:08 CrayonChimp

Discussion opened: https://github.com/godotengine/godot-proposals/discussions/10487

I'm really curious why light texture can't use partial sprites like Sprite2D, is there any technical difficulty?

Yes, atlas support needs dedicated support in each node. This can add a lot of maintenace burden, so it was only implemented in places where it's really needed (e.g. to save on memory).

Calinou avatar Aug 18 '24 15:08 Calinou