hey guys club here and today we will be covering something that has been much requested from me for the past i think year and a half uh but i really haven't done it because there really wasn't a consistent method up until 1. 18 came out and then it was a matter of me going through the pain of updating it but if you are here for the title it is a shader selection meth method which essentially lets you toggle or select different shaders based on input from a data pack so this is one of the examples i've put together which will be for download at the github but essentially the example here uses the multi-channel capability it has two channels uh the shader selector and we have one which is flashlight on or off and the other which is the battery level in the top left corner of the screen so when i turn the flashlight on the battery level will go down i can turn the flashlight off at any time it'll stop making the battery level go down and then eventually when the battery level hits zero my flashlight turns off now there are a couple things to discuss in terms of efficiency and capabilities and potential issues with this pack so let's just dig into all of the details starting from the basics so if you want to make a pack where you can select different shaders you need to have some kind of input from the game but what kind of input can you have well according to some wikis and you know you can test around yourself by going into each of these things known as core shaders and setting their colors to whatever like red just to see what's on the core shader you can kind of deduce what is rendered on each core shader and what kind of things that you'll be able to detect the common one to use is a glowing effect with different colors but that's kind of limited in what you can do because there's a limited number of colors so there are a lot of different options for going about this but the one that i chose as probably the most non-invasive method uh and easiest to customize is the kind of item drop now obviously you can experiment and figure out which one you like best but the way that items work is there are some items in the game that are rendered on a different buffer than regular items and they have transparency so they're handled by a different core shader than a block of dirt and those items are stained glass and some other transparent items so what you can do is you can create a 3d model and then the 3d model can just be a big cube okay so why didn't i release this pack before well this is what it has to do with look at this cube notice something weird well before they added the ability to mess with core shaders you essentially had to work with a shaded version of the cube so part of the problem that i had before was the frag color was not getting a constant value so when you have a 3d model in the game it's going to have some natural darkening you see this item on the ground see how each edge of the item has a different kind of shade because of the shadowing well that's kind of like inherent in the game but that is not inherent when you're messing with the core shader so since we have core shaders i am basically able to look for an item that has an alpha of 1 and an r of 1 and then set its alpha to a just always be one okay so i can basically interrupt this stuff because by default what the game does is it applies some fog logic to get that shadowing it creates some color modulation and some fog so i can basically bypass that color modulation in that fog for the specific items that i've generated as 3d models okay so then they will be 100 consistent what they are to the program shader so inside the program shader uh it's similar to what i've shown before so when we go to post transparency this uh another thing to note is that this only works with fabulous you have to have fabulous ones to do this shader selector but i mainly just chose that because i want the depth buffer uh but anyways so this i will go into how it works in a second but you run the transparency shader as it is and you make one little modification if it is a red 1. 0 item that means that it's our special item and we set the alpha to zero so that basically makes these guys invisible on the ground so you can seamlessly hide it besides the shadow the only thing that's not hidden is the shadow so without that you literally can't see it okay so that's how i basically hide our input selection so how do we read what the input selection is and remember what the input selection is well that's using some program shaders okay so this is kind of complicated how it works but essentially we have a 1 by 1 pixel buffer and a 1 by 1 pixel buffer copy okay so at the very beginning of our pass we copy uh whatever is on the in the input buffer onto the copy buffer okay then we scan the image looking for a pixel to put on the buffer okay and then we also pass our copied previous buffer so then we can compare inside here the previous versus the new thing you're looking at okay so let's go into scan inside scan you can see here that it loads what the previous color detected was and that's what it loads onto the output color by default but if there's the special conditions such as the item is close to them within one block or whatever 1.
0 units is and the color is exactly red then we're going to change what that item color is based on this logic and this is how i do the multi-channel so if blue is 1. 0 then update green well if sorry if green is 1. 0 then update blue if blue is 1.
0 then update green so this kind of lets you pick one channel or the other and we'll go over how that works uh in code in minecraft so how you you use the pack uh so this kind of just updates the color on this buffer right here and then down here at the bottom we run the draw text debug which i did not make this uh i will have a link to where this came from but it's very old but i've updated it slightly so that it works with the latest version but this basically just renders this text in the top left for debugging purposes and uh we'll go into what you need to do like you just need to delete these two uh passes in order to get rid of it for your pack if you're done debugging uh so then we run to the eval so the scan and the eval are separate steps the scan happens before the transparency step and then the eval happens after so eval is kind of like what you do based on what the value is and there you just pass the uh copy sampler okay so you just caught you just got past the sam the buffer essentially the thing that you wrote to uh so then after you pass that then you pass it to just a final blit copy which is a utility copy it's a special copy and it just kind of you know copies whatever was there so first we make a copy of what the previous sensed color input value is then we make a copy of that then if we see something change then we update the original and then we make logic based on the original and finally we output the logic processed frame so then the next thing to look at is eval so eval is where the logic happens essentially so this is where we kind of make our shader happen so this is going to be where you actually do stuff so inside this file you can see that i essentially check the green channel and i check the blue channel and then i also added overlay logic for overlays but in this case there is no overlay um but it's nice to have this blending here which is the same as my previous video on shaders so what we do is we set the color of the pixel you're currently at to whatever color it's supposed to be okay and then we check the uh selected color screen value if the green value is uh greater than zero if it's some in betw if it's some value that is not 255 essentially then we're going to add that to the screen's color and if the blue value is some value that's not 255 add that to the blues value so how do we get this working in game so we basically just have to give ourselves white stained glass that is custom model data of one three digits and three digits okay the three digits the first three are the g and the second three are the b okay so if i want to give myself something that has a b a g of 0 6 9 i just do that and you can see in the top left corner red is 255 green is 69 blue is 255 okay and that'll just add the green then if i want to set the blue channel separately i can do one two three and 255 on the green and that'll keep the green at 69 and say one two three now you can't do both because that would mean i have to create i think a couple million 3d model files so it will just take whatever it'll typically just take blue president green precedent so just make sure that if you want to set two channels you have to use two separate commands now this is where we discuss kind of the like how accurate this thing is so we have the give command which works pretty good for it but it's not perfect and it makes a little pop sound so you're better off using um [Music] something like a summon command inside the player's face so that's where the example pack goes into so let me dig into how i added the shaders to my code and kind of got it working all together so now we're basically going to go over how i built the example here so essentially all i did was i go into this post and i added an extra uh aux target which is the flashlight sampler this is just the texture used for the flashlight overlay if you wanted to add an overlay so i'll show you how i did that so i basically add this line of code which basically looks for a file named flashlight inside the effect folder so you have to go into the minecraft textures effect and add a file called flashlight okay so this flashlight file is one that i've built or you know i put together specifically to work with the logic of the shader so it's essentially the file size here which is uh 408 width and 1205 height so that has to be the same as this so if i go into edits image size you'll see it's the same okay but notice how there are one two three four one two three four five different basically sheets here that we're going to be using okay so we import that texture using this kind of aux target and make sure you have commas for your list and you end without a comma so then inside the eval so that's literally all you have to do so you just you mess with what extra info you need in the aux target if you do need extra info then you go into program poi eval. json you add that here so i added the flashlight sampler here and then i also added this fov uniform which is used in the flashlights logic so the this kind of stuff you have to kind of know how to use glsl and what you're trying to do here so then inside eval. fsh i also have an eval vsh which doesn't really do anything and you can make it do stuff if you want to but anyways eval fsh has an added function for linearized depth which basically calculates the distance i added the sampler up here flashlight sampler and i added the uniform here fov so then inside here i changed the logic for the green and the blue channel which is what you'll probably want to do as well so i check if green is 254 out of 255 then we turn on the flashlight uh if green is not that then it will just default to whatever the pixel is supposed to be which is if green is 255 then it will not have a flashlight on so for the flashlight this is the exact exact same code i used for my flashlight tutorial on how to do uh depth on like the depth tutorial how depth works and you can see that i'll put it in the description i'll link a couple different of these tutorials in the description that go a little more in depth on how this works but essentially we just figure out how far away the pixel we're trying to draw is and then increase its brightness based on the distance from the center of the screen as well as how far away it is okay and then we add that we set the frag color to that okay notice how i set the frag color if you're doing something that's setting the frag color you're gonna have to do this uh you're gonna have to mess around with the logic of whether you add to frag color or set the frag color because it's kind of complicated to get that working so then inside the blue channel then this is where the logic works for the batteries levels so if the battery level is by default is zero but this is inverse so zero means that you have full battery and four means you have nothing so then you check if the blue channel is 254 then set it to one else if it's 253 set it to two else if it's 252 set it's three else if it's 251 set it to four and the reason i did elsif's instead of a switch is just because i have these kind of like fractionals that i like to have here and uh i could have done a switch but i just went with an if else because it is a little bit easier for people to understand so then we have overlay so this is where you apply the overlay so based on battery i essentially just pick the y-coordinate based on the battery so the x-coordinate is the x-coordinate of the pixel and then the y coordinate is based on the battery so if you're on the battery level of a full battery then your y coordinate is just the y coordinate that you already had but if you're at the no battery then it's going to be at the top corner of the bottom so it just kind of transforms it essentially and then finally this kind of mixes whatever our overlay was with the rgb okay so that's how we set up the shader pack and then i obviously have f3t and everything is running as expected and i do slash give 254 that turns the light on 255 that turns the light off just like that okay and then i do 254 that makes the battery 1 253 2 251 and boom just like that okay so now we have to get into how i set up the data pack so in the shader selection example it has a function folder and that's really all it uses so this one i kind of you have to you have to mess around with what works best for you so i i just detect if the player right clicks a warped fungus on a stick and has some battery level then you toggle the flashlight so in the toggle of the flashlight it increments a score and then sets the score to zero so these two commands essentially just implement the toggling i could have done times negative one same difference but then it loads it runs shader load so shadow load basically runs logic based on this ssf.