Posts

Showing posts with the label uv

kodelife/GLSL simple noise code

Below is some code that generates some smooth looking noise. It isn't very complex, but it's probably good enough to get you going. Using code from The Art of Code youtube channel. Let's break down the important functions and see how they feed into one another. float N21(vec2 p){     return fract(sin(p.x*1000.0+p.y*6832.0)*5224.0); } This function takes a vector 2 "p", which is actually going to be our uv coordinate & multiplies each component(x and y) by two arbitrary large numbers , before getting the sine value. This small value is then multiplied by another large number, and then only the fractional part of that value is returned. On it's own, this will return nice static tv/white noise kinda values. You might call it in the main function like this - float c= N21(uv*4.0)   Next up, is  float smoothNoise(vec2 uv){     vec2 lv=fract(uv);    lv=lv*lv*(3.0-2.0*lv);    vec2 id=floor(uv);        float bl...

GLSL plotting a line, 2 ways.

Firstly, to plot a "function"..  float plot(vec2 uv, float pct){     return step(pct-0.01,uv.y)-         step(pct+0.01,uv.y); } void main(void) {     vec2 uv = v_texcoord;     vec3 col=vec3(0);          float y = smoothstep(0.,1.,uv.x);     y=(sin(uv.x*6.28)+1)*0.5;     float pct=plot(uv,y);     col+=pct*vec3(0.0,1.0,0.0);          gl_FragColor = vec4(col,         1.0); }   In the main function, I've made a float value "y", which I intialise with a boring smoothstep function between the values 0 and 1. However we're not plotting that. I immediately overwrite y with what is essentially a sin(x) function. The numbers added and multiplied are just there to fit one cycle into the UV space of 0-1. (6.28 is a bad approximation of 2*PI - ie 360 degrees, +1 is to shift...

shader scroll UVs

This nice short & sweet shader scrolls the texture using time & some float values, in the x/y or u/v directions. All we're doing is manipulating the u and v values. Unity provides us with the time variable "_Time". According to the docs, the time is in Seconds & is scaled by the game's time multiplier. Beneath the shader are some docs time variables and info.    Shader "Dave/Unlit/UVSCROLL"{     Properties{         _MainTex("Texture",2D) = "white"{}         _ScrollX("Scroll X",Range(-5,5))=1         _ScrollY("Scroll Y",Range(-5,5))=1              }         SubShader{         CGPROGRAM         #pragma surface surf Lambert         sampler2D _MainTex;         float _ScrollX...

Custom toon shader lighting model thing

Here's a custom lighting model that uses a texture (ideally it will be a horizontal greyscale gradient) to create toonshading style shading bands.. You know the kind... Diffuse is calculated as in Lamber - dot product of surface normal and light direction the h value here is not the halfway vector - it is a float that takes the diffuse value, halves it and adds 0.5 to get a value between 0 and 1. Remember if the vector is facing away from the other in a dot product, it will return a value of minus one. -1 x 0.5 +0.5 will be 0. This h value is then assigned to a float2  "rh", which acts as a UV value for the variable "ramp". We now have a greyscale value from the texture. The final colour is calculated by multiplying the albedo by light colour and the ramp value. I guess you could also multiply it by the attenuation if you were after a non-flat shader too    Shader "Dave/ToonRamp"{     Properties{         _Color ("Color",color)=...

radial gradient in shader graph

  Here's a radial gradient in Unity... I guess it made sense to make it like this to someone. Once. An expansion to it, with some colours