Posts

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=N21(id);    float br=N21(id+vec2(1,0));    float b=m

Unity's "new" input system and Keijiro's Minis midi stuff

Image
Many of you might be used to using the old Klak/MidiJack libraries for midi input into Unity, however they're getting a little long in the tooth now & Unity has changed up its input system to supposedly be more flexible & modern. The new midi input library is called Minis, I'm pretty sure it's short for Midi New Input System.. but that doesn't really matter :D There isn't a whole lot of easy-to-understand documentation on how to use it in scripts, so I thought I'd share what I'd figured out through digging around Keijiro's example files. I'm kind of assuming you know how to use the Input Action menu - as it would seem you don't even need it, for the method I'm about to show you... In any case if you do setup some midi inputs there (or indeed any other kinds of inputs - and it seems like you can only setup simple single note button presses there? - you would use code resembling this (following) to trigger things functions as you would

scripting custom render texture creation, assignment, shaders

I got a little fed up of dragging the (wrong) custom render texture swatches onto materials in the inspector (slightly down to my crap naming conventions & Unity's project window only showing the first 8 letters of each name- can this be changed?!), so I decided to figure out a C# script that could make this easier. The entire script is way below - I'm just going to highlight a few important lines.   Shader shader = Shader.Find("custom/createdTextureShader"); This finds a shader that you've created externally. You could have a public shader variable at the top instead, so you could drop stuff into the Inspector.   _init_mat = new Material(init_shader);   Here we make a new material, using a specified shader type.  _init_mat.SetTexture("_initTex", init_texture); Then we give it a texture file. _initTex is a property I've specified for the init_shader. Typically there is a _MainTex by default. I just wanted to see if you could specify arbitrary p

reflecting around a vector

 The above image hopefully explains the line below marked as "the reflecting bit". In the code below, in Kodelife branded code, I'm assigning the mouse.x to define the angle value, multiplying it by PI to get a full range of rotation (not sure why it's not 2 PI here, will update when I figure it out) Firstly, toward the bottom of the code, we're defining the distance from a line that goes from 0, to 0.5 on the x axis. We then use the smoothstep function on the distance variable to draw a thin line. We actually rotate the line by reflecting it across a vector n, whose direction we define by the angle. The dot product dot(uv,n) gives us the distance from the n line. and the direction we want to move in is n. so multiplying n by the distance and by 2 will get us toward the line and across.  a slight maths aside. dot(uv,n) *n is the actual projection of uv onto n. the above image is dealing with a normalised vector, where n would be 1 and isn't included in the eq