MentalRay 一点点“环境”的乐趣




原教学出自Master Zap的博客,原始链接如下:

作者:Master Zap (瑞典)


翻译:CWWS (


The Joy of a little “Ambience”…


As usual, when the same question pops up in multiple places, I tend to turn this into a blog post. The question I was asked recently was how to use ambient light in mental ray (specifically, in mental ray in 3ds Max), because people were confused about the lack of an “ambient” slot in, say, the mia_material (Arch & Design) shader.

通常来说,当相同的问题在不同的地方都被问到,我就会把它放到博客里解答。最近我经常被问到的一个问题就是,如何使用mental ray里面的ambient light(环境灯光)(特别是在3DMAX的Mental ray里面如何使用),因为有些材质缺少”ambient”(环境色)的选项,人们觉得不好理解,比如说,MR材质中的

(Arch & Design建筑和设计) shader.

I will try to explain this here.


THEORY: “Ambient Light” and “Occlusion” – a primer

理论:“Ambient Light”和“Occlusion”-入门

Back in the day…


Traditional computer graphics, with no indirect lighting of any kind, would by default look like this; here’s a scene lit with a single shadow-casting directional light:



I.e. shadows are pitch-black, with no realism whatsoever. You can’t even tell that the yellowish rectangular thing at the bottom is a table that has legs!


So a couple of tricks were introduced. One ugly, unrealistic hack was “shadow density”, i.e. the ability to say “when light from this light is in shadow, only x% of it actually disappear”. So you could set things like “Shadow Color” and “Shadow Density”, which you all understand is completely absurd and totally contrary to any form of sanity and logic.


An opaque object either blocks light, or it doesn’t – it doesn’t just randomly go “Oh I block 47% of the light”. That can’t happen (outside of actually transparent objects).


RULE#1: Never, ever, no matter how “old-school” your rendering techniques are, use “shadow color” or “shadow density” settings. Ever. Trust me on this.

规则#1:无论你的渲染技巧是多老、多旧、多么过时的,记住,永远,永远不要在渲染中使用和调节”shadow color” (阴影颜色)或者”shadow density”(阴影密度)这样的参数,决不!你一定要相信我说的这一点!!

Enter “Ambient” light


“But”, these early CG people said, “outdoors shadows from the sun is slightly blue, shouldn’t I set a shadow density and color to get my blue shadows”?




The reason “shadows are blue” is because they are filled in by blue light from the sky.


Now, our early CG pioneers understood this, of course, so rather than the horrendeos hack of “shadow color”, they introduced a nearly-as-horrendous hack: Ambient Light.

现在,我们早期的CG先锋们理解这一点了,当然,比可怕的“阴影颜色”更近一步,他们开发了一个差不多可怕的技术,就是:Ambient Light.(环境灯光)

But the problem was, this “Ambient Light” was ever-present and uniform, and yielded a totally unrealistic and unsatisfactory result when used on it’s own, something like this:

但是问题随之而来,这里的“Ambient Light”(环境灯光)是永远存在,并且完全均匀一致的,当使用这个设置的时候,产生出了完全不真实和无法让人满意的结果,看上去就像下面这张图片一样:


That looks about as horrible as the original: Sure, you can see something in the shadows – but it’s all totally uniformly lit. The “legs” of our table can now be seen… but as totally uniformly colored blobs, with nothing to reveal their shape.


Are these round legs, or are they flat oval things, or what are those? Does the legs touch the floor, or are they hovering above it? The purple teapot looks almost flying, because the shadow behind the green teapot is just a flat color.


The problem here is that light is hitting every point uniformly, with no regard to the position or angle of the surfaces. But if we are trying so simulate “blueish light from the sky”, then a point that is exposed to a lot of the sky will receive more light, than a point that is behind a bunch of other objects that are blocking (i.e. “occluding”) the skylight.


Enter “Occlusion”


Luckily, some bright people at ILM back in 2002 when they were working on “Pearl Harbor” invented (using mental ray, I should add) something called “Ambient Occlusion”. Basically, they wrote a shader that figure out how “occluded” a certain point was, i.e. how much stuff was “blocking light from arriving” at that point. This “Occlusion” on it’s own looks like this:

幸运的是,回到2002年,一些在ILM(工业光魔)工作的聪明家伙,在为电影“珍珠港”工作时,(我得指出,当时用的是mental ray渲染器,)他们开发了一种叫做“Ambient Occlusion”(环境光遮挡)的技术,基本上,他们编写了一种Shader(Mental Ray材质的通称),它能分辨出每一个特定的点被“遮挡”住光线的程度,也就是说,有“多少光线被遮挡了从而无法照射到”那个点,这种“遮挡”效果如果单独显示,看起来就像是下面这张图片一样:


Combining “Ambient” and “Occusion”

结合”Ambient”(环境光) 和”Occusion”(遮挡)

Now, if you apply this to the Ambient term, you get this very much nicer image:



See how beautifully the details resolve; the legs of the table now correclty reads as “contacting” the floor; the shape of the legs can be perceived. The area between the teapots is properly darkened, and the teapots have nice contact shadows.


The above is how it should be done!


Doing it the WRONG way


However, unfortunatly, many people have read and misunderstood the original ILM documents on Ambient Occlusion, and apply the Occlusion pass across the entire rendering. This is W R O N G!!

然而,不幸的是,很多人读过ILM关于Ambient Occlusion(环境光遮挡)的文档,却错误地理解了它,他们在渲染的整个过程之中设置了一个Occlusion(遮挡)渲染层,这是错误的!!

I.e. people make “plain” render (including ambient light and everything), make an occlusion pass, and just multiply the two in post!

也就是说,这些人制作了一个我们之前说明过的“平坦”的渲染(包括ambient light(环境光)设置和其它的一切),又制作了一个Occlusion(遮挡)渲染层, 然后仅仅在后期里面把它们以multiply(相乘)的模式叠加!

The result is that you get a deceptively-sort-of-okay-but-somwhat-wrong looking result like this:



Notice how this has a “dirty” feel. This is because the occlusion pass (pictured above) was applied on top of the entire image, affecing the “ambient” light as well as the directional light.

注意这种“脏脏”的感觉多么烦人,这是因为Occlusion pass(遮挡渲染层)被放置在图像的上端,作用于整个图像,它不但影响直射的光线,同时连”ambient” light(环境光)也影响了。

But this makes no sense; the “occlusion” of the directional light is already taken into account – that’s what the shadow of the light is. Ambient occlusion isn’t called “ambient occlusion” for no reason; it’s supposed to be applied to the “ambient” term, i.e. the “omnidirectional surrounding light”, not to any direct lights.

但是这么做是不合理的,因为直射光线产生的“遮挡”效果已经在渲染得时候被包括进去了,就是灯光产生的阴影部分,Ambient occlusion(环境光遮挡)并不意味着“ambient occlusion(环境遮挡)”,它应该被加在“环境”部分,也就是说,“各个方向周围环绕的光线”,而不是任何的直射光线。

But in the above image you will see darkening on floor in front of the objects, making it appear “dirty”. Similarly, there is a bad looking “dirt-shadow” of the teapots spout on the front of the teapots. And so on.


SO: Globally multiplying occlusion on top of the beauty pass (including reflections, direct light, etc) is WRONG. Don’t do it.


But isn’t this all fake? Why are we doing it?


Now… all we are doing with our “ambient” light and the “occlusion” is simulating “omnipresent light” (i.e. light from the environment) as well as “bounce light” (from other objects). Lets compare the result with actually calculating omnipresent light and bounce light; i.e. use Final Gathering and calculate it FOR REAL:

现在……我们对”ambient” light(环境光)和”occlusion”(遮挡)所做的一切都是在模拟“无所不在的灯光环境下”(比如说,自然环境下),体现从别的物体上反射回来的光线的效果。(记住这里是模拟的)。现在让我们比较一下,如果使用真实的计算方法来达到实际情况下自然环境和反射光线的得到的结果的差异,也就是说,我们使用Final Gathering功能来真实计算这样的情况。


The above image uses FG and a 3ds Max “Skylight” (i.e. light from the environment in XSI or Maya) to introduce the same kind of light we tried to “fake” with the “ambient” light – but correctly. And with true indirect bounces!

上面这张图使用了Final Gathering功能以及3ds Max的 “Skylight”(天空光线类型灯光),(类似于在XSI 或 Maya中环境下的灯光),为了得到相同的灯光环境,我们模拟了”ambient” light环境光的设置,但是很正确地,得到了真实的漫反射效果。

So this results begs the question; when we so easily can get the correct result, why would we still want to go the “fake” route?


There are a couple of answers:


• FG is an interpolated technique. This means that the indirect lighting is sub-sampled (calculated less than for every pixel) and the values between those are interpolated. However, if you are doing an animation, and the result between two frames are slightly different (for whatever reason), this may – hypothetically – cause not one pixel to change, but a large area of pixels to change (i.e. all pixels influenced by the interpolation of that FG point).

• FG是一个以内差值替换计算的技术,这意味着非直线的漫反射光线被二次采样,(根据其少于每个像素的值计算),然后从中选出基于两个数中间的值被选为插值,然而,如果你是在制作一部动画,那么前后两帧之间的结果是明显不同的(无论以何种理由计算),这可能——假设——不仅仅导致一个像素的改变,而是一整块区域像素的改变(也就是说,所有的像素都受那个FG采样点的插值的影响)。

The result, in a final animation, will be visible flicker. Visible, because it is “macroscopic”, i.e. larger than a pixel, and perceived as “scintillation” or “flicker”, which is visually unappealing.

• 而如上所述的原理的结果,就是在一个最终渲染完的动画里面,会发生画面闪烁的现象,非常明显,因为这是肉眼可以察觉的。换句话说,这种闪烁的范围会大于一个像素,你看上去的感觉就是动画在闪烁,这在视觉上是不可接受的。

Contrast this with using the occlusion technique: It is calculated for every pixel (every sample, even), and hence, any noise in this calculation is of sub-pixel size. I.e. a difference from one frame or another will only affect at most one pixel, and mostly just a fragment of a pixel.

• 将这个技术与occlusion“遮挡”渲染层技术相比,后者是基于每一个像素来计算,(每一个样本,平均的),因此,计算出来的noise(噪波)只影响对应的子像素,换言之,不同帧之间的区别只会影响最多一个像素,而大多数只会影响像素的一小部分。

The result is that this perceived more as “film grain”, and is much less visually objectionable.


• We may actually want to combine both “real” and “fake”; this is a method I often advocate.


You use FG at a low density and with high interpolation, and you apply your occlusion on top of this indirect light, rather than some homemade, arbitrarily invented “ambient” light. This relieves you of the responsibility of having to figure out the proper intensity and color of this mythical “ambient light” – it is calculated for you.

你采用FG时,设置很低的密度,并用很高的插值,同时你把你的occlusion(遮挡)渲染层放到这个漫反射灯光渲染的上面,不像一些家居设置,你可以任意地创造”ambient” light(环境光),这可以让你免除必须计算出:虚构的的环境光设置的正确密度和颜色数值的麻烦,它取决于你自己。

And of course, this combo method is a built in as a feature in Arch&Design/mia_material…


PRACTICE: How do we actually do this?


Since the original questions was in a 3ds Max context, I will answer (for now) in a 3ds Max context, but also mention the methods for XSI and Maya in the text.

由于最初的问题是在3ds Max区问出来的,所以(现在),我会在3ds Max里面回答这个问题,不过我也会用文字提示同样的解决方法在XSI 和 Maya里如何操作。

There are three basic methods to apply ambient light:


1. Use the built in AO of Arch&Design/mia_material and utilize it’s “Ambient” parameter

2. Use a light set to “Ambient Only”

3. Use a light with the Ambient/Reflective Occlusion shader as light shader.



2,使用一盏灯光并设置为”Ambient Only” (“只用于环境光”)


3,使用一盏灯光,并采用Ambient/Reflective Occlusion(环境光/遮挡)材质作为灯光的材质。

Method #1: Arch&Design (mia_material) ambience


This method allows you either per-material or global ambient light levels, and you can easily modify the ambient occlusion radius from the rollout:

这个方法既可以让你设置全局的环境光,也可以单独设置每一个材质球,另外你还可以非常容易地从下拉框调节ambient occlusion radius(环境光遮挡半径)。

• Open the material in the material editor

• Go to the “Special Effects” rollout.

• Turn on the “Ambient Occlusion” and…

• 从材质编辑器打开材质球。

• 来到”Special Effects”(特殊效果)下拉框

• 打开”Ambient Occlusion”(环境光遮挡)选项,并且……

o …EITHER put in a given ambient light color in the material…

o ……在材质球指定一个环境光颜色…….

o …OR: switch to the “global” mode, which will use the “ambient” color and intensity from the Environment dialog box:

o ……或者:转换到”global”(全局)模式,它使用Environment(环境)对话框里面的”ambient”(环境)颜色和强度来定义材质球的相关参数。


In XSI and Maya this means enabling the built in AO of mia_material, and setting the “ao_ambient” parameter to the color of your desired ambient light.


在XSI 和 Maya里面,这意味着打开mia_material材质的内置AO选项开关,并设置”ao_ambient”参数,选择你希望得到的环境光颜色。

Method #2: “Ambient Only” light


(AFAIK, this specific method only works in 3ds Max. Correct me if I am wrong.)

(特别提示,这个特殊的方法只在3ds Max下有效,如果我说错了,请纠正我。)

• Create an Omni

• 创建一盏Omni灯光。

• In it’s Advanced rollout, set the light to “Ambient Only”

• 在它的Advanced (高级选项)卷展栏,设置灯光为”Ambient Only”(只用于环境光)


This gives you plain (flat) ambient light with no occlusion.

这个选择将给你我们前面说过的平面化的ambient light(环境光),它是不包含occlusion(遮挡)效果的

To get the occlusion effect by adding the Ambient/Reflective Occlusion shader as the “Projector Map” of the light.

然后把Ambient/Reflective Occlusion(环境光/遮挡)材质作为”Projector Map”(投射贴图)赋予灯光,来获得occlusion(遮挡)效果。

BUT – There is a big BUT – For this to work, you MUST have the light with it’s transformation reset, i.e. it must be placed at 0,0,0 in the top viewport, or the ambient occlusion shader will be confused.

但是——这是一个大大的“但是”,要想这个方法生效,你必须重置这盏灯光的transformation位置,它必须在top(顶)视图被放置为0,0,0的坐标上,不然的话,ambient occlusion(环境光遮挡)效果将会混乱的。

Doing this manually is tricky; so it can be done much easier with a script:


myLight = Omnilight()

myLight.AmbientOnly = true

myLight.projectorMap = Ambient_Reflective_Occlusion__3dsmax()

To run this script:


• Go to MaxScript menu

• choose “New Script”

• paste the above snippet in

• hit CTRL-E

• 找到MaxScript菜单

• 选择”New Script”(新建一个表达式)

• 把上面提供的代码片断拷贝粘帖进去

• 按快捷键CTRL-E

You will now have a set up light, where you can modify it’s intensity and color etc. w. the normal 3ds max light features. The only thing you need to look out for is if you wan to set the distance for the ambient occlusion rays, you must

现在你将会得到一盏已经为你设置好的灯光,当然你还可以调节他的强度,颜色等通常的3ds max灯光参数,唯一你需要搜寻的是,如果你想设置ambient occlusion rays(环境光遮挡光线)的距离,你必须:

• take the Ambient/Reflective Occlusion shader that is in the projector map

• drag-and-drop it into the material editor

• choose “Instance”

• …and modify the settings from the material editor

• 在projector map(投射贴图)选项选择Ambient/Reflective Occlusion(环境光/反射遮挡)材质。

• 把它拖拽到材质编辑器里面

• 在弹出的对话框选择”Instance”(实例)选项

• 然后在材质编辑器里面调节相关设置

Method #3: “Ambient Only” light w. occlusion shader as light shader

方法#3:使用occlusion shader(遮挡材质)作为灯光材质,并且“只用于环境光”的灯光。

There is actually a Method#3 which is similar to Method #2, except rather than placing the Ambient/Reflective Occlusion shader in the “Projection Map” slot, you use that shader as the light shader of the light itself.

方法#3实际上和方法#2有些类似,不过这里我们不把Ambient/Reflective Occlusion(环境光/反射遮挡)材质放在”Projection Map”(投射贴图)材质槽,而是把它作为灯光自身的材质。

A quirk of this methods is that now the shader completely replaces all the settings of the light; all controls for color, intensity, etc. stop working, and you need to do all changes to the light intensity and color by modifying the color of the “bright” slot of the occlusion shader itself (by putting it in material editor, as above).

这个方法有趣的地方是,你赋予灯光的材质将会完全替代所有灯光本身的设置,所有对于灯光本身参数,例如:颜色,强度,等等的控制都停止工作了,然后你需要设置occlusion shader 遮挡材质自身的”bright”(亮度)参数来控制灯光的强度和颜色,(通过把材质放到材质编辑器里调节,像我们上一个方法做的那样。)

This method has the benefit of not requiring the strange “light has to be in 0,0,0″ requirement, plus that it is a method that works as well in Maya and XSI. To do this in XSI or Maya:

这个方法的好处是,不需要再面对类似于“把灯光位置设置为0,0,0”这样奇怪的要求,另外,这个方法也适合在Maya 和 XSI中使用,想要在Maya 和 XSI中使用,做如下操作。

• Create an area light

• Set it’s area light type to “User” and 1 sample

• Use the mib_amb_occlusion shader as the light shader of the light

• 创建一个area light(区域光)

• 设置灯光类型为”User”,sample为一

• 使用mib_amb_occlusion shader(Mib环境遮挡材质)作为灯光的shader(材质)

Similar to 3ds Max, changes to the “ambient light level” will now have to be done entirely from inside the occlusion shader.

和在3ds Max中类似,对于”ambient light level”(环境灯光亮度级别)这样的参数控制,已经完全被赋予灯光的材质所控制,你需要调节材质来调整灯光的参数。

Hope this helps.


Mo’ later.





1回复MentalRay 一点点“环境”的乐趣"


电子邮件地址不会被公开。 必填项已用*标注