Posts

Showing posts with the label basic

simple CG surface shader breakdown

 A simple shader in Unity has a certain structure.. It starts with the main block: shader("folder/name") { //everything in here } We have the word "shader" that tells Unity we're writing a shader. The "folder" part will create a folder in the shader drop down menu of the Unity Material. The "name" will be the name of your shader that sits in that folder. Next comes the Properties - these are the exposed properties that the user will see in the Unity inspector. These sit inside the shader block, as does everything else we're going to add. shader("folder/name") { Properties {     _myColor("My Color",color)=(1,1,1,1)     _myTex("My Texture",2D)="White"{}     _myFloat("My Float Value",float)=1.3 } }  So this is our first sub block of the shader. It seems like a lot of coders like to put the underscore in front of their defined _names. I guess this is good for identifying user-created variable ...

Compute Shader, absolute minimum

 First make a compute shader - right click in the project window-  create shader, compute shader The default code will look as follows // Each #kernel tells which function to compile; you can have many kernels #pragma kernel CSMain // Create a RenderTexture with enableRandomWrite flag and set it // with cs.SetTexture RWTexture2D<float4> Result; [numthreads(8,8,1)] void CSMain (uint3 id : SV_DispatchThreadID) {     // TODO: insert actual code here!     Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0); }   To keep things simple, lets just replace that last Result... line with    Result[id.xy]=float4(1,1,0,0);  This will make our shader produce a yellow colour. Note the #pragma kernel is called CSMain. This is basically the function name we'll be calling from the C# script.   To go with the compute shader, we need a C# script that assigns the shader to our geometry. Let's use a Qu...