All posts by Ronnie

Screen Space Texture Shader: Part 1

Redditor u/pigrockets asked a few days ago on r/Unity3D “What’s do you call this kind of static pattern shader?”

I call it the “Screen space Texture Shader” and here is how to make one in Unity.

First of all: What do we need to make this?

We need some way to calculate the screen position of every point on your model so that you know what pixel of your screen space texture/static pattern goes where. In Unity you can get this position calculated for free!

And as you can see from the video you want the shader to have an outline to get that cartoony look! This we will accomplish with a vertex program, where we want to scale our model a little bigger depending on the width we want on our outline, and then we color this model without any lighting in a fragment program. This “pass” will render underneath the static pattern.

For this simple shader example we won’t make a custom lighting model, which we would do if we wanted a more cartoony look.

Create your custom shader

CreateSurfaceShader.JPG

First you create your shader Create>Shader>Standard Surface Shader, and name it “ScreenSpaceTextureShader” or whatever you wan’t to call it! Open up the shader and you will see that you’ve got a lot of free code for your new shader:

Okey! That’s a nice start! On the top we have
“Shader “Custom/ScreenSpaceTextureShader” {” This names our shader “ScreenSpaceTextureShader” and puts it in the Custom shader folder.

Then we have our properties to set up our materials from the inspector. You can actually skip the properties and set up your materials entirely from script, but that is another story.

Then there’s the Subshader. This is where the magic happens, the “Tags” defines certain parameters for the shader, mainly render order. You can set the LOD for your shader also.
In the CGPROGRAM you have your shader code.

“#pragma surface surf Standard fullforwardshadows” says that we will have a surface-function called “surf” using the Standard lighting model. And finally we want fullforwardshadows, which basically means we get shadows cast from all light sources in forward rendering.

“#pragma target 3.0” means we are targeting shader model 3.0, this gives a little nicer lighting i guess, but limits the compatibility of the shader, so you can remove this if you want it to work “everywhere”.

Next up is our definitions _MainTex is a sampler2D, _Glossiness is half, _Metallic is half and _Color is fixed4. float, half, and fixed is basically the same thing with different precisions, float being the most precise, then half which you should use alot for mobile optimization, and fixed which is the least precise.
Then we have our Input structure.

This sets up what we want to pass to our surface-function. Like our models UV info. Here we have uv_MainTex which is the first UV set of the rendered mesh.

Finally we have the surface-funtion:

“void” – we are not returning anything from this function. “surf” – like we said in our “#pragma surface surf” our function is called “surf”. “Input IN” – structure type Input, with variable name “IN” is passed to our surface-function. “inout SurfaceOutputStandard o” – what light model output structure we are using.
tex2D(_MainTex, IN.uv_MainTex) read the pixelcolor of our main texture in the xy position from the uv_map, this is multiplied with our _Color and we have our Albedo Color. Metallic and Smoothness is set directly in the inspector.

Now try making a material with this “Standard” Shader! Create a new material. I’ve called it “ScreenSpaceMaterial” and select our new custom shader like so:

CreateMaterial

The material should have an inspector view with these properties:

Inspector

As you can see we have material Color, Albedo Texture, Smoothness and Metallic, but we need more properties for our Screen Space Texture Shader. We need a “screen space texture”, “outline color”, and a way to set our “outline width”.

So we add some new properties to our shader. _ScreenTex, _OutlineColor and _Outline.

Our _ScreenTex defaults to all white, _Outline color defaults to black (0, 0, 0, 1), and our _Outline needs to be within the range 0.0 to 0.03 and defaults to 0.005. Save this and your material inspector will look like this.

Inspector2.JPG

The Screen space Texture implementation is quite simple compared to the outline so we’ll start there. We need to sample our _ScreenTex in the surface-function so add “sampler2D _ScreenTex;” like we have for our _MainTex. And we need to know what the screen position is in our surface-function so add “float4 screenPos” to the Input-structure. Like this:

When we add screenPos to our Input like this, with exactly that name Unity gives us this “for free”. We can also have worldPos, viewDir and others that is done by Unity. But we only need screenPos for this.

Now we want to get the screen position in normalized values and sample the texture. To get the normalized screenpos we do:
half2 screenUV = IN.screenPos.xy / IN.screenPos.w
then we sample the texture with this coordinate:
fixed4 sstc = tex2D(_ScreenTex, screenUV);
then we multiply this color with the color we already have and get this surface-function:

That is all you need for the screen space texture part. Save the shader and try it out! Add a screen space texture to your material and you should get a result like this:

In the next part we will wrap up the shader, by adding the outline! So long!

Arduino <3 Unity

I’ve recently bought myself an Arduino Uno R3 kit and I’ve built myself some simple projects to get a feeling of what is possible with this stuff! Having little to none experience with microcontrollers I must say the Arduino is quite simple to get into, and the community is awesome with ALOT of tutorials and questions answered online! Much like the Unity community! One of my first projects was to try to get the arduino to communicate with Unity! My plan was to make a gyro-accelerometer for use with my computer! 

For this I paired the Arduino with a  Wifi-Shield(ESP8266) and a GY-521(MPU6050) 6DOF gyro/accelerometer. I set the esp8266 to be a client, hooked up to my computer as the server. I used a free library for handeling the gyroscope which converts the gyro/accel raw data to quaternion rotation and sends it as a string on the serial line. The ESP8266 transmits the string over TCP to the server. 

In Unity i set up a TCPListener on a seperate Thread that continuesly reads the strings from the Arduino and converts string back to quaternion rotation.

In operation it looks like this:  

 The setup is not very accurate so not very useful, but fun project to get working! 

Now I’ve started working on something else! Bigger, better and more beautiful! 

Poly particles

I’m not sure this is a good idea or what, but I’ve made another shader, that takes a particle system and creates a low poly version of the particles.. Looks like this:

Screenshots: 

   
    
 
Screenshots are nice and all, but video says more than a thousand screenshots..

I guess it will probably be quite heavy, since it needs extra camera that renders a displacement texture.. But it does look good or what? 

Universal poly water

So I’ve rebuilt my low poly water shader from scratch! The reason for the rebuild is that the shader I had, and have shared on here was quite limited platform wise, only worked in forward rendering and on DX11 capable hardware, which felt veeery limiting.. So fast forward to today, and I’ve got my New Universal Shader running on everything! With reflections working on supported platforms, and also I now detect where water is shallow to get the foamy look along the shore..

Edit: This is what it can look like in action:

Screenshots from iPhone: 


  

It looks awesome on the iPhone, but no reflections on my iPhone 5 (Edit: Reflections are working!)..  Which only supports Open GL ES2.0 i think.. Will test on 6S soon. On tvOS it does not look that nice,  but I think that has something to do with the way lighting is done on that platform, not sure! (Edit: It now looks really good! Quality settings was wrong!)

Uppsala temple Low poly

I wanted to do some viking age inspired low poly buildings and stumbled upon the story of Uppsala temple. A heden temple that might have been in Uppsala. A religious center where every ninth year, nine days of sacrificing took place, and every day a total of 3 men and animals where sacrificed to the gods and hanged in the tree of sacrifice and the grove outside the temple. The temple was surrounded by a golden chain that could be seen from afar. The temple was burned and the tree of sacrifice cut down by King Inge Stenkilsson the elder, and a christian church was built in it’s place. The story of the temple was written down by Adam of Bremen. But due to the lack of sources, the existance of the temple is uncertain.

I made the model based on an old drawing I found on wikipedia. This one:

 With a little sprinkle of imagination I’ve ended up with this: 

Cycles render with a little color-correction:  

Low poly models

When I’m not making games or shaders I enjoy building 3D models in Blender. I’m  quite addicted to the low poly, flat shaded style so it’s all that kind of models. Of course the fact that low poly is simpler and faster to make is important for me since I don’t have time for anything else.. Here are some in progress renders I’ve done lately..

Nisse and varg..  The santa-like nisse is rigged as well, I’ve used the character in a Unity test project..   

   
Uppsala temple (Viking age)       

  

Custom shaders and shadows

Since I’m quite new to this shader business I’m bumping into problems here and there, and this time I’m really struggling with solving it.

I’ve made this shader that I’ve gotten to cast shadows with a custom ShadowCaster pass but I’m still not able to get it to collect shadows in any way. I’ve tried getting the fallback to handle the ShadowCollector pass, I’ve tried diffent ways to write the shadowcollector pass, and I’ve tried setting the Queue to geometry and RenderType to Opaque instead of Transparent..

Shader coding in Unity is hard, because there isn’t much info to be found anywhere about how things work and how it should be done. Does anyone out there have any tips on how I can get this to work?

See the code below or download the shader here.

Low poly water shader:evolved

Hi, folks! I’ve continued working on my water shader! I’ve added transparency and today I added impact waves to the shader, which I’m quite happy with managing to do! It’s limited to 10 simultaneous “impact waves” at the same time at the moment. But adding more is not a problem. The collision points and impact force is sent to shader from script. And waves are calculated in shader.
Have a look at what it looks like in action: