I’ve been working on getting dynamic ambient lighting working within Unity. Based off Valve’s 6-colour pre-baked ambient lighting (detailed here, pg 5, ch 8.4.1), but it grabs the 6 colours dynamically.
There’s probably lots more you could do to optimise it further (e.g. use replacement shaders when rendering the cubemap that do simpler lighting calcs). But you could do that yourself as required.
I’d also advise against using it on anything other than your main character, as it’s likely too expensive to run on multiple objects.
cubemap camera script
Create a new camera and turn off the GUI, Flare and Audio components.
Set up it’s Culling Layers to not render non-essential things like particles or incidental detail. Also move your character to it’s own layer and set the camera not to render it (we don’t want bits of the character rendered into the cubemap).
Attach this javascript to it and set the target to be your character and set up the offset from your character’s position so that it’s in the centre of your character (i.e. a 2m tall character wants to be offset 0, 1, 0 so that the camera renders from the characters centre.
下面为摄像机脚本:
@script ExecuteInEditMode
public var target : Transform;
public var cubemapSize : int = 128;
public var oneFacePerFrame : boolean = true;
public var offset : Vector3 = Vector3.zero;
private var cam : Camera;
private var rtex : RenderTexture;
function Start () {
cam = camera;
cam.enabled = false;
// render all six faces at startup
UpdateCubemap( 63 );
transform.rotation = Quaternion.identity;
}
function LateUpdate () {
if ( oneFacePerFrame ) {
var faceToRender = Time.frameCount % 6;
var faceMask = 1 << faceToRender;
UpdateCubemap ( faceMask );
} else {
UpdateCubemap ( 63 ); // all six faces
}
}
function UpdateCubemap ( faceMask : int ) {
if ( !rtex ) {
rtex = new RenderTexture ( cubemapSize, cubemapSize, 16 );
rtex.isPowerOfTwo = true;
rtex.isCubemap = true;
rtex.useMipMap = true;
rtex.hideFlags = HideFlags.HideAndDontSave;
rtex.SetGlobalShaderProperty ( "
_
WorldCube" );
}
transform.position = target.position + offset;
cam.RenderToCubemap ( rtex, faceMask );
}
function OnDisable () {
DestroyImmediate ( rtex );
}
dynamic ambient shader
环境光shader
This is the shader that generates and applies the ambient lighting from the cubemap rendered by the camera above.
Create a new shader, paste this code into it and save it. We’ll integrate it into our shaders next.
integrating the ambient shader into surface shaders
将环境光shader与物体表面的shader发生互相影响
Now, we can use the above shader wherever we want it via the UsePass command, and blending everything else on top.
The key here is to ensure your surface shader’s blend mode is set to additive (One One) otherwise it’ll just write clean over the lovely ambient light that’s been applied.So, before your surface shader’s CGPROGRAM block, add the lines;
添加shader代码:
UsePass "DynamicAmbient/DYNAMICAMBIENT"
Blend One One
We’ve also got to ensure that our surface shader doesn’t use the ambient light value that’s set in the editor, otherwise it’ll add the two together and defeat the purpose. So when you define the surface shader to use, ensure you add the noambient argument. e.g;
#pragma surf BlinnPhong noambient
Your new surface shader with dynamic ambient lighting should look something like this;