解决破面的一个方法
noxss (2009-09-24 18:35:32)
破面大家都知道了吧,两个平面靠的很近的情况下就有可能出现破面哦。
除了美工的方法,这里给出个解决方案(应该还没人发过吧?):给场景中的物体分层,并根据物体的旋转动态地改变图层顺序。
请看例子:
这里给出一个比较极端的例子:两个平面重叠(你可以理解为“靠的很近”的极端情况),一个蓝色,一个绿色,材质都是双面的(这里是为了看出更清楚地看出破面的效果),两个平面同时旋转。想要的效果是旋转中两平面交替出现,并且不出现破面。如果不采取任何措施的话是不行的,因为靠的太近,会出现破面。代码如下:
package
{
import flash.events.Event;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.view.*
import org.papervision3d.materials.*;
public class example_01 extends BasicView
{
private var Green :ColorMaterial;
private var Blue :ColorMaterial;
private var GrePlane :Plane;
private var BluPlane :Plane;
public function example_01()
{
super(0, 0, true);
Green = new ColorMaterial(0x95D500);
Blue = new ColorMaterial(0x0D7DC8);
Green.doubleSided = true;
Blue.doubleSided = true;
GrePlane = new Plane(Green, 500, 500, 20, 20);
BluPlane = new Plane(Blue , 500, 500, 20, 20);
scene.addChild(GrePlane);
scene.addChild(BluPlane);
startRendering();
}
override protected function onRenderTick(event:Event = null):void
{
GrePlane.pitch(2.5);
BluPlane.pitch(2.5);
super.onRenderTick();
}
}
}
试着编译看看,会出现很多破面吧。。那么再看看修改过的代码吧:
package
{
import flash.events.Event;
import org.papervision3d.view.layer.*;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.view.*
import org.papervision3d.materials.*;
public class example_01 extends BasicView
{
private var Green :ColorMaterial;
private var Blue :ColorMaterial;
private var GrePlane :Plane;
private var BluPlane :Plane;
private var GrePlaneLay :ViewportLayer;
private var BluPlaneLay :ViewportLayer;
public function example_01()
{
super(0, 0, true);
viewport.containerSprite.sortMode = 'index';
Green = new ColorMaterial(0x95D500);
Blue = new ColorMaterial(0x0D7DC8);
Green.doubleSided = true;
Blue.doubleSided = true;
GrePlane = new Plane(Green, 500, 500, 20, 20);
BluPlane = new Plane(Blue , 500, 500, 20, 20);
GrePlaneLay = viewport.getChildLayer(GrePlane);
BluPlaneLay = viewport.getChildLayer(BluPlane);
GrePlaneLay.layerIndex = 3;
BluPlaneLay.layerIndex = 2;
scene.addChild(GrePlane);
scene.addChild(BluPlane);
startRendering();
}
private function setLayIndexByRotation():void
{
if (GrePlane.rotationX < 90 && GrePlane.rotationX > -90)
{
GrePlaneLay.layerIndex = 1;
}else{
GrePlaneLay.layerIndex = 3;
}
}
override protected function onRenderTick(event:Event = null):void
{
GrePlane.pitch(2.5);
BluPlane.pitch(2.5);
setLayIndexByRotation();
super.onRenderTick();
}
}
}
再编译一次看看,是不是解决了?^_^
这里贴出的是以前做的一个小演示,这个应用可以有很多扩展,也许有时间我会把他做成一个类来。。。