Posts

Showing posts with the label normal

simple outline shader, using the vertex normal

Beneath is a method of drawing outlines around our mesh - or rather, we're expanding the mesh, giving it a flat colour and then drawing the mesh as normal on top of it. Note that we have two CGPROGRAM blocks, which get executed in order. This type of outline requires the transparent tag to work properly, and this could cause problems elsewhere.. Also it only outlines the extremities of a shape - or you could say, just the alpha edges . Next post will feature a more advanced shader    Shader "Dave/Unlit/simpleOutline" {     Properties     {         _MainTex("Texture",2D) = "white"{}         _OutlineColor("Outline Colour",color)=(0,0,0,1)         _Outline("Outline Width",Range(-0.1,0.1))=0.005     }     SubShader{         Tags{"Queue"="Transparent"}         ZWrite off ...

using vertex shaders with surface shaders - "extrusion"/displace by normal?

Here's a nice & simple vert-surface shader that affects the appearance of the object's vertices. If you click the object in the editor view, you'll see the object outline remains normal - it is just the rendered appearance. The magic happens in the vert function, where you add the normal of the vertex, multiplied by the extrude amount to the vertex's original position.    Shader "Dave/Unlit/Extrude"{     Properties{         _MainTex("MainTex",2D) = "white"{}         _Amount ("extrude",Range(-1,1))=0.01     }         SubShader{         CGPROGRAM         #pragma surface surf Lambert vertex:vert         struct Input {             float2 uv_MainTex;         };        ...

CG Surface shader continued - normal maps, world reflection

Below  is code for a reflection map that takes the normal map into account - eg , it obeys all the bumpy bits. Notice - the reflection map is taken in as a CUBE type, in all caps & uses a samplerCUBE type in the subshader variable definition. The Normal map is taken in as a 2D, just like a standard texture map - but in the Properties, we initialise it to "bump".  We use the normal map by calling the UnpackNormal function - this converts the rgb values of the normal map (we get those by using tex2D) into the xyz vectors that the normals need. We're sticking the reflection map into the output Emission for now . World Reflection is actually based on the normal of the object - the polynormals are projected outwards and they hit a cubemap - the colour of the cubemap at this point is then returned as the world reflection - HOWEVER - because we are modifying the normals using our bump/normal map this is gonna get weird. Unity will actually freak out - UNLESS - we add the I...

casting rays, layer masks, filter objects

 An edit if you will.. When casting rays with masks/filters, remember the ray will keep going until it hits something...so if you have some junk under your floor geo, it's likely the ray will hit it. So be sure to have a function that checks the hit object somewhere..   Some code -                  LayerMask mask = LayerMask.GetMask("spawnObject");                          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //cast ray             RaycastHit hit; //create hit event variable             if (Physics.Raycast(ray, out hit,50f,mask)) //if ray hits object             {           ...