Posts

Showing posts with the label vertex

sin vertex displacement shader

 The following vertex-surface shader moves vertices up and down based on some sine waves and the time variable. Combining lots of waves is a cool way to generate funky displacements. Things to note - we calculate a new normal in the vert function to prevent weird shading issues.  Shader "Dave/Unlit/waves" {     Properties     {         _MainTex ("Texture", 2D) = "white" {}         _Tint("Colour Tint",color)=(1,1,1,1)         _Freq("Frequency",Range(0,5))=3         _Speed("Speed",Range(0,100))=10         _Amp("Amplitude",Range(0,1))=0.5     }     SubShader     {        CGPROGRAM     #pragma surface surf Lambert vertex:vert        struct Input {         ...

Fragment/Vertex shader Casting and Receiving shadows

 There're two parts to this - lets focus on the first Pass block -  The first block allows the shader to receive shadows. The previous ones were all flat. The LightMode tag sets it to Forward lighting, which changes it from the default Deferred lighting that Unity uses.  We have some extra #pragma stuff that I don't really understand - apparently it's there so we can have complete control over the lighting? Some extra #includes are present as well...more functions to do with lighting. In our appdata struct we're using the NORMAL now. In the v2f we call the "position"  variable "pos", because later on a TRANSFER_SHADOW function requires that specific name within the struct. It won't recognise "vertex". There's also a SHADOW_COORDS variable, to tell the shadow where to go. In our vert function we convert the vertex to screenspace, calculate its worldspace normal, then get the dot product between the normal & the light's positi...

fragment vertex/vertex fragment shaders in unity

Fragment Vertex shaders are a different beast to surface shaders. We are able to access vertex data and control pixels in much finer detail. Below is a simple frag/vert shader. The framework is similar to the surface shaders we've been looking at, only the usual CGPROGRAM block now sits within a Pass block, that lives in the SubShader. I haven't included a Properties block, but that remains unchanged. The first thing is we now have #pragmas for the frag and vert functions and also #include the UnityCG.cginc file, that contains lots of handy shader functions There are other files we can include when we are working with lighting and shadows. Next up in the framework is the "appdata" struct - it essentially has all the vertex data that our 3d model holds. Here we've go the vertex variable as a float4 type, and we use the POSITION attribute. The "vertex" name can be arbitrary. You can also list other data, such as normals, color (multiple) and UVs & they...

Compute Shaders, particle system with quads, using Frag/Vert shader to draw

This is more or less the same as the last wall of code. However this time, we are using the Graphics draw to draw a quad, which consists of Four points, Two triangles. We need to adjust our buffer and create a new struct for this, as well as initialise our arrays in a specific way to ensure the quads are facing the correct direction.   As mentioned, our new Struct - Vertex stores a position, UV value and life. We're going to use the Compute Shader to calculate the postion of each particle-  but then draw a quad at that position. We set the specific values for the vertices in the compute shader too. (the UVs don't change for each quad, so we set that value in the C# script)     //Triangle 1 - bot left, top left, top right     vertexBuffer[index].position.x = p.position.x - halfSize;     vertexBuffer[index].position.y = p.position.y - halfSize;     vertexBuffer[index].position.z = p.position.z;     vertex...