Proper cubemap lighting shader

Heylo,

I was working on converting static directionally lit objects into using cubemap based lighting, as suggested at Unite. I’ve got the cubemap just fine, but I’m having trouble with the shader. This is what I have so far:

Shader "CubeLight/Basic" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_CubeLight ("Light Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
	}

	SubShader {
		Pass {
			Name "BASE"
			SetTexture [_MainTex] {
				constantColor [_Color]
				Combine texture * constant
			} 
			SetTexture [_CubeLight] {
				combine texture * previous DOUBLE, previous
			}
		}
	} 
	
	Fallback "VertexLit"
}

It sort of works, but the lighting seems to change with the camera view. I haven’t found the right combo yet, so I’m wondering if anyone has an idea.

Thanks in advance,
-Jon

I’m sorry, can you provide images of cubemap lighting and possibly explain what it is and the advantages of it? I have never heard of this…

The idea is that you bake any number of lights into one cubemap, so your shader just samples that dependent on the normal instead of performing any number of lighting calculations, which are particularly expensive on Intel integrated cards because they lack hardware lighting&transform. A texture lookup is inexpensive to do.

It works if the lighting isn’t spatially localized (spot or point lights) and static.

Cheers,
-Jon

Well, the presentations are up, now. :slight_smile: Yay

This one has the cubemap lighting:
http://unity3d.com/support/resources/unite-presentations/shading-and-shadows

Cheers,
-Jon

Playing with Aras’ demo and wizard just now, I see that this technique only works with directional lights. Can (or should) this technique be applied to interior scenes lit with many point lights?

Nah - it works only for directional lights / reflections

What you can do is to have many cubemaps set up for different parts of the level, but unless you’re moving fast (think driving game), you’re gonna get some pops when you switch cubemaps

Thanks, Nick.

Have a Merry Christmas and a happy New Year! I’ll be thinking of you guys as I bask in the California and Florida sunshine over the next couple of weeks. :lol:

–Rick

What advantage does this have over baking lightmaps? Is this to speed up directional lighting when doing per-pixel calculations for the lighting (eg if you have normal mapped surfaces) to reduce the number of passes needed for calculating all the lights on Intel hardware? In the end, is this speedup worth it to apply to other, non-Intel hardware to reduce the lighting passes?

You can move stuff about. The cubemaps essentially stored incoming light from a given direction, so moving and rotation is a lot simpler.

We used it in GC:Palestine - for outside games like that, we typically baked 15-20 directional lights into it. Gave a really nice, soft lighting - at pretty much the same cost as 1-2 directional lights

Ok this makes more sense now. Could you use a series of these over time to do things like change the lighting to simulate time of day changes?

Thanks for the explanation.

Yup - although you would probably need many cubemaps for it to appear smooth… Instead, you’d probably make a shader that allowed you to fade between them - this could also help with the motion problems I described above.

In GC:Palestine, we had 4-6 lighting environments, and then we only switched lighting during cutscenes - so we’d fade the screen to black, then fade up again in a new “mood”

The nice thing about cubemaps are that pretty much any graphic card supports them - and supports them well.

Are there any engine scalability issues with using a cubemap for lighting? Meaning are there any potential old target computer configurations where cubemaps might not be supported, and then lighting effect would be lost? I mean on an old computer the engine might turn off the cubemap for a reflection with little effect, but turning off a cubemap used for lighting would make everything go darker or ambient.

Thanks,
Ricko

Cubemaps are supported since very fist GeForce, Radeon cards, and Intel 865 (or 845, don’t remember). So pretty much all cards since 2000.

Basically, if they can’t render a cubemap, their cards are so slow, you’ll need to reauthor your assets anyways…

for GC:Palestine, (which targets public schools) we basically declared anything that old for DOA - and removed all lighting whatsoever. If they could get it to run at all, they were very lucky

Thanks for the responses guys. That is what I needed to know.

Ricko

Just make a fallback unlit subshader in case they don’t do cubemapping.

Shader "Cube Lighting/Diffuse" {
Properties {
	_MainTex ("Main Texture", 2D) = "white" {}
	_DiffuseCube ("Diff cube", Cube) = "" { TexGen CubeNormal }
}
SubShader {
	Pass {
		SetTexture [_MainTex] { combine texture }
		SetTexture [_DiffuseCube] { matrix [_ViewToWorld] combine previous * texture DOUBLE, previous }
	}
}
	SubShader {
		Pass {
			SetTexture [_MainTex] {
				Combine texture
			} 
		}
	}
}

But yeah, the card that can’t do cubemapping will be darn slow anyways. At least it’ll look like something not-black, though.

hmm. one year old post.

I tried this shader in the post above but the lighting still seems to change with the camera view.

Is it a must to have Unity Pro for this to work?

Now one year later are there any new ways to use a cubemap for diffuse illumination?

Looked at setting this up, and was running into the same thing, it seems that the _ViewToWorld keyword is set in the “CubeMapUpdater.js” script from the unite07 Shading and Shadows project

// Attach this script to a camera to make cubemap lighting be updated
// for this camera.

@script RequireComponent(Camera)

function OnPreRender()
{
	var mat = camera.cameraToWorldMatrix;
	mat.SetColumn( 3, Vector4(0,0,0,1) );
	Shader.SetGlobalMatrix( "_ViewToWorld", mat );
}

What i would like to know however, is what i could do to enable shadows for this