How to Scale Texture in Unity using Shader Graph

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video we're going to see how you can scale textures using Shader graph in unity so let's jump right into it let's start off by trying to understand what we are trying to do here I'll add a quad to the scene next let's create a new material and add it to the quad and then add a texture to this quad while I can rescale the quad I can't really scale the texture itself so we are going to try and solve this using Shader graph let's start by creating a new Shader graph we go to create Shader graph urp and then let Shader graph let's call it scale Shader and double click to open the Shader graph window right off the B we are going to create a float parameter for scale and drag it in we also need another parameter for our texture we bring it in and connect it to a sample texture 2D node we need this because our graph doesn't take textures directly but instead takes the RGB pixel value in the base color this sample texture 2D node among other things helps you convert your texture to rgba values let's save this and see how it looks to try it out we are going to create a new material and in the Shader dropdown select scale Shader we can drop drop our texture in the base texture slot and add the material to our quad as of now it looks the same as our previous material and this scale parameter does absolutely nothing let's fix this to scale our texture we are going to need a tiling and offset node we can connect the output of this to the UV input in our sample texture 2D what thises does is it defines how many times our texture is going to be tiled or repeated if we save this and go back we can see that we can zoom in and zoom out our texture but it's not very intuitive yet so let's go back and fix that what we need is for our scale parameter to correspond to the scale of the texture and not the tiling logically if our texture tiles twice the scale will be half and if the scale is twice it's going to tile only half a time we can Define this inverse relationship by dividing one by our scale power parameter let's rearrange our nodes a bit you see this pink output is due to division by zero error we have right now we can quickly fix this by selecting scale parameter and going into graph inspector and changing the default value to one let's save this and go to scene View and check our progress scaling make makes way more sense now but we still have a problem most of the times we don't want to scale our texture from the bottom left point which is the point of origin in our UV map what we usually want to do is scale our texture Center outwards we can do that by fixing the offset for our sample texture 2D let's create a new Vector to parameter and connect it to the offset input of our tiling and offset node if we save this and go back and let's change our scale we can now Center our texture manually but the problem still persists every time I change the scale here I have to fix the offset again what we need to do is calculate the offset automatically based on our scale let's delete the offset first now we we are going to take the output of our divide node which is the tiling value and divide it by min-2 and add 0.5 to it what this does is if our tiling is 0.5 which corresponds to a scale of two then it moves the offset to 0.251 X and 0.251 Y which is the center of the texture now we can save and go back to check it out and now whenever we scale our texture it scales Center outwards one last thing we want to add is the ability to change the Anchor Point manually for this we go back to our Shader graph let's make some space here and bring in our offset node let's add it to the output of our ad node using another add node and then pass it to the offset input of the tiling and offset node we save again and go back and now we have the ability to Define custom offset points for our texture to scale from with the default being the Cento that's all for this video thanks for watching consider liking the video If it helped you out subscribe to the channel and we'll see you next time
Info
Channel: Vikings Devlogs
Views: 2,142
Rating: undefined out of 5
Keywords: vikings, vikings devlogs, devlogs, unity, unity 3d, shaders in unity, shader graph, how to scale texture, scale texture in unity, how to scale texture in unity, how to resize texture in unity, scale texture using shader graph, unity tutorial, game dev tutorial, shader graph in unity tutorial, unity shader graph, how to resize texture, tiling texture in unity
Id: _Kjg5YMHCF0
Channel Id: undefined
Length: 5min 23sec (323 seconds)
Published: Sat Dec 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.