Making Glass?

How can I make a glass shader in Unity? I mean a simple one, preferably in indie (it’s for the widgetcontest and it needs to be transparent at least)…
Thanks!

Come on, no advice? [this section of the forum seems a bit unused… Maybe im mistaken?]

If you want reall glass reflections, you’ll need Unity Pro, but you can fake them with a cube map.

If you just want semi-transparent material, you don’t even have to write a new shader. You can use one of the supplied semi-transparent shaders. (The ones in the Alpha submenu in the shader property of the material you are modifying.)

The fake part seems to be the best alternative here… So I can make it work with a transparent shader with a cubemap? I’ll try that.
(does Unity pro have a ready made glass/refraction shader?)

Actually, you cannot assign a cubemap to the alpha shader. Try this shader instead

Shader "Glass" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB) Transparency (A)", 2D) = "white"
		_Reflections ("Reflection cubemap", Cube) = "skybox" { TexGen CubeReflect }
	}
	SubShader {
		Pass {
			Tags {"Queue" = "Transparent" }
			Blend SrcAlpha OneMinusSrcAlpha
			ZWrite Off
			Material {
				Diffuse [_Color]
			}
			Lighting On
			SetTexture [_MainTex] {
				combine texture * primary double, texture * primary
			}
		}
		Pass {
			Blend One One
			Material {
				Diffuse [_Color]
			}
			Lighting On
			SetTexture [_Reflections] {
				combine texture
				Matrix [_Reflection]
			}
		}
	} 
	SubShader {
		Pass {
			Tags {"Queue" = "Transparent" }
			Blend SrcAlpha OneMinusSrcAlpha
			Material {
				Diffuse [_Color]
			}
			Lighting On
			SetTexture [_MainTex] {
				combine texture * primary double, texture * primary
			}
		}
	}
}

Btw, the trick to making this one look good is to use a cubemap with few powerful highlights - don’t use the skybox, but make another one that only contains a few hoglights and a hint of the clouds…

Note: This shader actually has some issues in the 1.1 beta - I’m on that

Wow this more than I expected… I will have to get in on your shader programming, it seems really cool.

Now what kind of issues are you talking about? When I render I don’t see anything but a grey box, but that might just be my stoneage grafics card… Do you expect to fix it before the 22.august? :roll:

Thank you Nicholas, Im a little behind schedual but this is great stuff you are handing me here! I will do more tests on better Video cards and see what it yeilds…

one thing though, when I make a shader, I now have to shift to expert mode in order to attach the cubemap etc. It’s no biggie but how do you guys make the interface for the shaders?

It won’t be picked up if you change it in expert mode.

You have to define them using the properties section:

Properties { 
      _Color ("Main Color", Color) = (1,1,1,1) 
      _MainTex ("Base (RGB) Transparency (A)", 2D) = "white" 
      _Reflections ("Reflection cubemap", Cube) = "skybox" { TexGen CubeReflect } 
   }

The fact that you even can assign a cubemap in expert mode could be considered a bug… Some shaders will accept a cube map where they expect a 2D texture, others will fail in weird ways.

We have extra checking in place for the normal inspector to only allow you to assign the correct type of texture. If you can’t assign it there, there’s a reason for it…

Ok, so thats why my box turned grey. I just edit the shader source then; Im on it!

Btw: Is all reference names in Unity global? What I mean to say is; if you reference “skybox” and it’s in a subfolder way down below it will still pick it up? (if so I will have to start planning my name applying… coming from flash doesn’t exactly make it easier on me…)

No… We just have a few builtins for the global names (white, black, dot3, etc).

Atm, we don’t even support ‘skybox’, but that will change soon.

In Unity, you cannot use file names - you assign the textures by dragging dropping on to the material.

Now, I can’t really seem to figure this out properly, if I’ve got a generated cubemap- How can I apply it when there is no interface to drag it onto?

Now when I use the Shadercode, create a new shader out of it, assign it to a material; then in simple mode it calls the shader “default”, and I can’t drag any cubemaps onto it (same in Full mode)…

In expert mode there is a texture slot, so if that doesn’t work, I just don’t see how…(as you said, Unity wont take file names, and _Reflections doesn’t show up in expert mode either…)

I guess Im actually confused :o

When you write the new shader, you need to set the property type to be a ‘cube’ for it to accept cubemaps.

See the property decl in line 3

Shader "cubedemo" {
  Properties {
    _Cube ("drag texture here", CUBE) = "" { TexGen cubereflect }
  }
  subshader {
    Pass {
      SetTexture [_Cube] { combine texture }
    }
  }
}

All the reflective shaders accept cubemaps for their reflection texture as well…

The ‘Default’ shader pops up because there was a syntax error in the shader. I just tried copying the script myself, and it worked fine. Are you sure you got your selection right prior to copy-pasting?

Well I’ve done it now three times, each time there’s the same old Default showing up…

I tried exproting it, maybe you can find some errors for me?:

2511–84–$glass_480.unitypackage (1.94 KB)

Hmm… For some reason, I couldn’t unpack the package… I created one for you

2514–85–$glass_213.unitypackage (1.97 KB)

Ok, now for some reason it works, although the script looks the same…?

Are you running beta Nicholas?

Im running later than beta, but nothing has changed in that respect… All efforts are going into getting RTT working in a nice way for 1.1

Well then this is one of those weird things that happens in life then… I don’t see how you got that shader to work in your file…