A3D Decal类

Decal,一种用于表现在普通材质之上的特殊纹理。一般用于表现场景中的血迹,弹痕,标记等。

而且这种纹理不会像普通纹理一样出现深度冲突。

普通纹理和Decal对比

当我们拉远镜头时,就变成了这样:

package {
import alternativa.engine3d.controllers.SimpleObjectController;
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3D;
import alternativa.engine3d.core.Resource;
import alternativa.engine3d.core.View;
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.objects.Decal;
import alternativa.engine3d.primitives.Box;
import alternativa.engine3d.resources.BitmapTextureResource;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

/**
* ...
* @author redefy
*/
public class TestDecal extends Sprite {
[Embed(source="resources/Smoke Kick.png")]
private static const Smoke:Class;
[Embed(source="resources/crumple.jpg")]
private static const Crumple:Class;
private var rootContainer:Object3D = new Object3D();

private var camera:Camera3D;
private var stage3D:Stage3D;
private var simpleController:SimpleObjectController;

private var boxOut:Box;
private var boxIn:Box;

private var decalOut:Decal;
private var decalIn:Decal;

public function TestDecal(){
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

camera = new Camera3D(0.1, 200000);
camera.view = new View(800, 500);
camera.rotationX = -90 * Math.PI / 180;
camera.z = 100
camera.y = -1200;
addChild(camera.diagram);
addChild(camera.view);

boxOut = new Box(400, 400, 400);
var boxOutMaterial:TextureMaterial = new TextureMaterial(new BitmapTextureResource(new Crumple().bitmapData));
boxOut.addSurface(boxOutMaterial, 12, 2);
boxOut.x = 400;
rootContainer.addChild(boxOut);

boxIn = new Box(200, 200, 200);
var boxInMaterial:TextureMaterial = new TextureMaterial(new BitmapTextureResource(new Smoke().bitmapData));
boxInMaterial.useDiffuseAlphaChannel = true;
boxIn.addSurface(boxInMaterial, 12, 2);
boxIn.x = 400;
boxIn.y = -101;
rootContainer.addChild(boxIn);

decalIn = new Decal();
decalIn.geometry = boxIn.geometry;
decalIn.addSurface(boxInMaterial, 12, 2);
decalIn.x = -400;
decalIn.y = -101;
rootContainer.addChild(decalIn);

decalOut = new Decal();
decalOut.geometry = boxOut.geometry;
decalOut.addSurface(boxOutMaterial, 12, 2);
decalOut.x = -400;
rootContainer.addChild(decalOut);

rootContainer.addChild(camera);

simpleController = new SimpleObjectController(stage, camera, 400);

stage3D = stage.stage3Ds[0];
stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
stage3D.requestContext3D();
}

private function onContextCreate(e:Event):void {
for each (var resource:Resource in rootContainer.getResources(true)){
resource.upload(stage3D.context3D);
}
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(e:Event):void {
camera.render(stage3D);
simpleController.update();
}
}
}



posted on 2012-02-08 23:52  泰达  阅读(357)  评论(0)    收藏  举报

导航