Tag Archives: Tutorial

Fresnel shader in Unity(With source)

FresnelShader

I was just messing around with shaders in Unity while working on a game. I’ve never really tried making a shader before because I feel the math is intimidating, but it is actually pretty simple once you get into it. I’ve always liked the fresnel effect; it looks cool for microbiological illustrations and it’s nice for highlighting stuff.

The way it works is that you take the dot product of the direction vector of the camera and the normal of the current pixel being rendered on the model(See picture below). The dot product will go from 1 to 0(assuming that we’re dealing with unit vectors(we are)) the closer these vectors are to being perpendicular and the closer these vectors are to being perpendicular the closer the pixel is to the edge of the model being rendered in relation to the camera. You then take the inverse of the dot product, i.e. 1 – (cameraDirection ยท normal), and you multiply it with the color you want. The color then gets more intense the closer you get to the edge of the model, as you can see in the picture above. If you add a bump map to this, the surface normals get distorted and the fresnel effect distorts with it, which looks really cool as you can see in the bottom two spheres on the right, in the picture above.

Normals

The white lines are the normals of the sphere; the outward direction of the face.

Anyway, feel free to use it in your own projects as much as you want, link in the comments if you wanna show off something you’ve made with it. Here’s the source:

Shader "Custom/FresnelShader" {
	Properties {
	 	_Shininess ("Shininess", Range (0.01, 3)) = 1

	 	_MyColor ("Shine Color", Color) = (1,1,1,1) 

		_MainTex ("Base (RGB)", 2D) = "white" {}

		_Bump ("Bump", 2D) = "bump" {}

	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		sampler2D _Bump;
		float _Shininess;
		fixed4 _MyColor; 

		struct Input {
			float2 uv_MainTex;
			float2 uv_Bump;
			float3 viewDir;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Normal = UnpackNormal(tex2D(_Bump, IN.uv_Bump));
			half factor = dot(normalize(IN.viewDir),o.Normal);
			o.Albedo = c.rgb+_MyColor*(_Shininess-factor*_Shininess);
			o.Emission.rgb = _MyColor*(_Shininess-factor*_Shininess);
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Home Theater PC for free

HTPC

What you need, if you want the HTPC(Home Theater PC) for free:

  • A TV with VGA or HDMI input(Possibly audio input jack or speaker system).
  • A wireless router.
  • An old PC(Possibly laptop, if you’re wiling to plug it in every time you want to watch stuff).
  • A smartphone(Only necessary for easily portable remote control).
  • A USB with about 1 GB free space or a writable DVD.
  • Willingness to “just fucking Google it”.

Continue reading

Subdivision of icosahedrons

In this post I will explain how to build, subdivide and texture an icosahedron. Look at the ending of the post for eye candy.

So I’ve been following the development of Notch’s new game, 0x10^c, which is looking really exciting. When I saw a screenshot(Can’t find it now) I was reminded of something I read about using icosahedrons to make planets in 3D with minimal distortion when mapping to a texture.

I find this way of attacking the problem of spheres fascinating. Basically you start off with a 20 sided die and split the faces by adding new vertices on a sphere encapsulating the icosahedrons, so in each iteration you get closer to a perfect sphere. You split the faces by adding a point in the middle of each edge in a triangle, you then construct four new triangles using these and the original vertices.

Icosahedron Subdivision

Continue reading