偶然发现一个外国人写的基于PV3D的投射阴影类——Papervision Shadow Casting。除了效率比较低(这是PV3D的通病了,和这个类没任何关系 - -!),还算是比较强了。
先不多说,我们先看看如何使用。
首先,我们需要光源
然后创建一个DisplayObject3D,例子里用了DAE
Preview:http://niuniuzhu.cn/p/3DRenderDemo/index.html?p=4
偶然发现一个外国人写的基于PV3D的阴影投射类——Papervision Shadow Casting。除了效率比较低(这是PV3D的通病了,和这个类没任何关系 - -!),还算是比较强了。
先不多说,我们先看看如何使用。
首先,我们需要光源
Code
var l:PointLight3D = new PointLight3D();
然后创建一个DisplayObject3D,例子里用了DAE
Code
dae = new DAE();
dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, onLoad);
dae.load(XML(new DemoModel()));
private function onLoad(e:Event):void
{
scene.addChild(dae);
//
}
为了加强光影效果,这里做一个小小的Hack。我们复制多一个3D对象,然后给他一个FlatShadeMaterial材质(或者GouraudMaterial材质),让3D对象能够“反光”
Code
cloned = new DAE();
var m:FlatShadeMaterial = new FlatShadeMaterial(l, 0xFFFFFF, 0x1a1a1a);
var ml:MaterialsList = new MaterialsList();
ml.addMaterial(m, "material0");
cloned.addEventListener(FileLoadEvent.LOAD_COMPLETE, cloneLoad);
cloned.load(XML(new DemoModel()), ml);
//
scene.addChild(dae);
添加一个平面,用于呈现投影。注意:这个平面的材质必须是MovieMaterial类或其子类。
Code
var movie:Sprite = new Sprite();
var bmp:BitmapData = Bitmap(new floorMaterial()).bitmapData;
movie.graphics.beginBitmapFill(bmp, null, true);
movie.graphics.drawRect(0, 0, 512, 512);
movie.graphics.endFill();
var movieMat:MovieMaterial = new MovieMaterial(movie, false, true, true);
plane = new Plane(movieMat, 600, 600, 1, 1);
plane.rotationX = -90;
plane.y = - 62;
scene.addChild(plane);
然后主角登场了,我们看看如何使用这个ShadowCaster类。只有两行代码,很简单。
Code
var shadowCaster:ShadowCaster = new ShadowCaster("shadow1", 0, BlendMode.NORMAL, 0.75, [new BlurFilter(4, 4, 2)]);
shadowCaster.setType(ShadowCaster.DIRECTIONAL);
参数:
uid : String - a unique identifier that tells the shadowCaster what layer to draw on. It will create this layer in your MovieMaterial on its first cast.
color : uint - Color of the shadow
blend : String - BlendMode to use. Defaults to Multiply for nice shading.
alpha: alpha of the shadow
filters: an array of filters to apply to the shadow. If you pass null a default BlurFilter will be used.
最后,监听Event.ENTER_FRAME
Code
private function onEnterFrame(e:Event):void
{
//
shadowCaster.castModel(dae, l, plane, true, false); //cast a shadow for next render!
}
Preview:http://niuniuzhu.cn/p/3DRenderDemo/index.html?p=4
Hope you enjoy it!