Posts

getattribute blender vs redshift

It seems  like Redshift does not support OSL's getattribute() yet...and Blender's alembic importer is either not able to bring in my custom attributes, or is just ignoring them (you have to use the setattribtypeinfo function to force it to read attribs as colours still). As a result it is best practice to provide inputs to your OSL shaders to receive color or other attribs baked into your points. eg shader attributeFetch(color custom=color(0),output color col=0) { col=custom; } I had previously planned on using getattribute within the actual shader to access point attributes, but quickly encountered the "not supported" error message with Redshift :) On an interesting note Blender seems to make the getattribute function work! Could be a GPU vs CPU thing (Blender's OSL support is limited to Cycles -CPU) so this code should work -   shader attributeFetch(string name="floater",color custom=color(0),output color col=0) { color temp; if(getattribute(name,temp)

OSL - simple UV things and frac fract alternative

Here is a shoddy OSL shader that follow on from some of my GLSL experiments of yesteryear. I couldn't seem to find a frac or fract  function, but fmod-ing (it's like modulo) against 1 does the same thing. I don't put any input parameters in this shader. EDIT - just saw someone else's code that use x-floor(x) for fract, which could be faster..who knows! Variables Lame an Cat are assigned to u and v, I then multiply them by different values and fmod them so they repeat 3 and 4 times. It seems like u and v will be taken automatically, which is nice. After that I do a bit of if statement colour assignment.   shader use_uv(output color resultRGB = 0) {      float lame=u;      float cat=v;      cat=fmod(v*3,1);      lame=fmod(u*4,1);      resultRGB = color(lame, v, 1);      if(lame>0.5){      resultRGB+=color(0.3,0.4,0); } if(v<0.1){      resultRGB+=color(0,0,1); } if(lame<0.5){      resultRGB+=color(0,lame,0); } }

OSL lets begin - load a texture

Below is some code for a very simple OSLshader that loads in a texture and uses the UV coordinates existing on the object. It begins with "shader image", which will create a shader named image. The variables that are listed and initialised in the ( parentheses ) are the arguments that it takes. It expects a string for the filename, float values called s and t, initialised to u & v and also a bunch of other stuff that I'm ignoring, for this post. Also, we have the output listed, which is of type color and named Cout, defaulting to black (or 0). The actual doing part of the shader, the part between the { curly brackets, or braces } is very quick - it just takes all the variables mentioned and uses them to assign the texture image to the colour output. Cout=texture(filename,s,t); CODE AS FOLLOWS- shader image     [[ string help = "Texture lookup" ]] (     string filename = ""         ,     float s = u         ,     float t = v         ,     /*float 

step and smoothstep functions, drawing a line

float f =step(0.3, uv.x); this will return 0 where uv.x is less than 0.3 and will return 1 where uv.x is greater than 0.3 graphically this would produce a black rectangle taking up 30% of the horizontal. (or a white rectangle taking up 70% of it on the other side) The smoothstep function provides a nice blend between two values. float g=smoothstep (0.2,0.8,uv.x); now if you assign this to a colour output, you'll see a nice gradient, starting black from roughly 20% to white at 80% along the horizontal. With this in mind, it is possible to plot a line using smoothsteps. float plot(vec2 uv, float line){   return  smoothstep( line-0.02, line, uv.y) -           smoothstep( line, line+0.02, uv.y); } looking at the code you might be able to guess that we're just taking the areas slightly above and below the line's function and subtracting. The 0.02 would define the thickness of said line. In the main function of our shader, we'd call plot like this -  float f=plot(uv,g); then

kodelife/glsl drawing a circle (sdf)

 Below is the main function for drawing a circle. Let's go through the important parts of the code. Firstly vec2 uv =inData.v_texcoord-0.5; we subtract 0.5 from the uv vector so that we get the 0 value in the centre. (by default 0 is at the bottom left corner) vec2 gv=fract(uv*5)-0.5; We copy the pattern across by multiplying the uv. By using fract we get the fractional values that exist within each smaller uv space. The 0.5 subtraction is to shift the 0 point back to the centre - the fract values will run from 0 to 1 otherwise. Next up, the circle float d=length(gv); float r=0.3; float m=smoothstep(r,r*0.4,d); Here we calculate the distance a pixel is from the centre (now set correctly because of the -0.5 above), by using the length function. We then define our desired circle radius as "r" & give it a value. Next, we use a smoothstep of r & a slightly smaller r, testing against the pixel's distance, to see if it is within the circle or not. This value is stor

kodelife/glsl noise

I had been cluttering up my Unity notes blog with GLSL/Kodelife stuff, so I've made this new page here... 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)