Casting shadows in PV3D 

偶然发现一个外国人写的基于PV3D的阴影投射类——Papervision Shadow Casting。除了效率比较低(这是PV3D的通病了,和这个类没任何关系 - -!),还算是比较强了。

 

先不多说,我们先看看如何使用。

 

首先,我们需要光源

 

 PointLight3D();

 

然后创建一个DisplayObject3D,例子里用了DAE

 

 DAE();
dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, onLoad);
dae.load(XML(new DemoModel()));

private function onLoad(e:Event):void
{
    scene.addChild(dae);
    
//Casting shadows in PV3D
}

 

为了加强光影效果,这里做一个小小的Hack。我们复制多一个3D对象,然后给他一个FlatShadeMaterial材质(或者GouraudMaterial材质),让3D对象能够“反光”

 

 DAE();
var m:FlatShadeMaterial = new FlatShadeMaterial(l, 0xFFFFFF0x1a1a1a);
var ml:MaterialsList 
= new MaterialsList();
ml.addMaterial(m, 
"material0");
cloned.addEventListener(FileLoadEvent.LOAD_COMPLETE, cloneLoad);
cloned.load(XML(
new DemoModel()), ml);

//Casting shadows in PV3D
scene.addChild(dae);

 

添加一个平面,用于呈现投影。注意:这个平面的材质必须是MovieMaterial类或其子类。

 

 Sprite();
var bmp:BitmapData 
= Bitmap(new floorMaterial()).bitmapData;
            
movie.graphics.beginBitmapFill(bmp, 
nulltrue);
movie.graphics.drawRect(
00512512);
movie.graphics.endFill();
var movieMat:MovieMaterial 
= new MovieMaterial(movie, falsetruetrue);
            
plane 
= new Plane(movieMat, 60060011);
plane.rotationX 
= -90;
plane.y 
= - 62;
scene.addChild(plane);

 

然后主角登场了,我们看看如何使用这个ShadowCaster类。只有两行代码,很简单。

 

)]);
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

     


    {
        //Casting shadows in PV3D
        shadowCaster.castModel(dae, l, plane, truefalse); //cast a shadow for next render!
    }

     

    Preview:http://niuniuzhu.cn/p/3DRenderDemo/index.html?p=4

     

    Hope you enjoy it!

  • 相关文章:

    • 2022-12-23
    • 2021-07-31
    • 2021-09-09
    • 2022-12-23
    • 2021-09-17
    • 2022-12-23
    • 2022-02-08
    • 2021-07-25
    猜你喜欢
    • 2021-08-16
    • 2021-06-19
    • 2021-12-12
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    • 2021-08-06
    相关资源
    相似解决方案