Jump to content

Texture selection per model instance shader [shader included]


Rick
 Share

Recommended Posts

I can't attach any files for some reason as I did try to put this in the asset store under shaders and even on this post but it kept giving me an error:

 

https://dl.dropbox.c...iled-Shader.rar

 

INSTALL: Place the .shader files into your projects Shaders/Dynamic folder. It won't overwrite anything as they are new files.

 

THANKS: MASSIVE thanks to shadmar for the offsetting code and talking things out with me, and niosp for his LE2 original shader that gave the idea of using the color (he used alpha as an index but I think this required a different shader for different number of sub textures per row which this method doesn't require) for doing this as the color seems to be unique per model instance which is what we need and the reason we use the color variable to get this effect.

 

DESCRIPTION: I don't really know what to call this shader, but it allows you to combine multiple textures into 1 big texture, and then allow you to pick which texture from that you want to show on a given model instance. This is "needed" because models are by default instanced, and so changing material on 1 instance changes it on all instances. You can create unique copies, but doing so at run-time is slower than using instancing so for mobile this will give a big savings I think.To be able to have 1 model instance, but apply different textures to it, you use this shader, and then you use entity:SetColor(row, col, 1.0 / 4.0, 1) to get the subtexture you want to actually apply.

 

entity:SetColor(row, col, 1.0 / 4.0, 1) explained:

 

1) Your big texture will have rows & cols made up of your sub textures (see screenshot below)

2) When calling SetColor() the first parameter is the row, and the second is the column, the 3rd is 1 / how many texture per row fit on your big texture, and the last parameter is ignored so just put 1

 

NEGATIVES: You lose the functionality of SetColor(). This means you can't tint your model a certain color using SetColor() when using this shader. I rarely use SetColor() on models myself so this probably isn't a massive limitation.

 

RANT: Ideally, we would be able to define use variables of certain types, per model instance dynamically, that then get exposed to the shaders by the engine automatically. Not sure if that's possible, but it would allow for way more functionality and not hacking around using things like color for something else. Maybe even a couple holders for each type would help if doing this dynamically isn't possible.

 

EXTRA: Note below I only did 3 textures and left the rest blank. I will have more for my game, but did this as just an example of what I mean. In my case I'm using this for completely different textures, but you could also give a progression of your character getting beat up with blood and ripped clothes or something cool like that. smile.png

 

tiled_shader.png

  • Upvote 4
Link to comment
Share on other sites

FYI, you can also load different instances of the model and use a different material on each. See the Asset class for the constants to pass to the load function. But your solution is really good.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...