Alternativa3D也可以!3D立体效果(需要红蓝眼镜)

来源:互联网 发布:we piano软件 编辑:程序博客网 时间:2024/04/30 10:18

看flare3D之黄河星球demo,提起我的兴趣。故我就用我较熟悉之Alternativa3D实现一下。就用这个方法实现了。贴代码瞧瞧

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.lights.DirectionalLight;import alternativa.engine3d.loaders.ParserCollada;import alternativa.engine3d.materials.FillMaterial;import alternativa.engine3d.materials.NormalMapSpace;import alternativa.engine3d.materials.StandardMaterial;import alternativa.engine3d.materials.TextureMaterial;import alternativa.engine3d.objects.Mesh;import alternativa.engine3d.primitives.Box;import alternativa.engine3d.resources.BitmapTextureResource;import alternativa.engine3d.resources.TextureResource;import flash.display.Bitmap;import flash.display.BitmapData;import flash.display.Sprite;import flash.display.Stage3D;import flash.display.StageAlign;import flash.display.StageScaleMode;import flash.display3D.Context3D;import flash.events.Event;import flash.geom.ColorTransform;import flash.geom.Rectangle;public class Test1 extends Sprite{[Embed(source="textures/texture.jpg")]static private const EmbedTexture:Class;[Embed("models/model.dae",mimeType="application/octet-stream")]static private const EmbedModel:Class;private var stage3D:Stage3D;private var context3D:Context3D;private var rootContainer:Object3D;private var camera:Camera3D;private var view:View;private var controller:SimpleObjectController;private var tm:FillMaterial;private var mb:Boolean;private var mesh:Mesh;private var mesh1:Mesh;private var mesh2:Mesh;private var container:Object3D;private var redTextureResource:BitmapTextureResource;private var blueTextureResource:BitmapTextureResource;private var textureResource:BitmapTextureResource;private var material:TextureMaterial;private var rtb:Boolean=false;public function Test1(){stage.align=StageAlign.TOP_LEFT;stage.scaleMode=StageScaleMode.NO_SCALE;stage.frameRate=60;stage3D=stage.stage3Ds[0];stage3D.addEventListener(Event.CONTEXT3D_CREATE,context3DCreate);stage3D.requestContext3D();}private function context3DCreate(e:Event):void{context3D=stage3D.context3D;rootContainer=new Object3D();view=new View(stage.stageWidth,stage.stageHeight);addChild(view);camera=new Camera3D(0.1,100000);camera.view=view;rootContainer.addChild(camera);camera.rotationX=-100*Math.PI/180;camera.y=-100;camera.z=20;addChild(camera.diagram);controller=new SimpleObjectController(stage,camera,200);var parser:ParserCollada=new ParserCollada();parser.parse(XML(new EmbedModel()));mesh=parser.getObjectByName("Cylinder01") as Mesh;var redTextureBitmapData:BitmapData=new EmbedTexture().bitmapData;redTextureBitmapData.colorTransform(new Rectangle(0,0,1024,1024),new ColorTransform(1,0,0));var blueTextureBitmapData:BitmapData=new EmbedTexture().bitmapData;blueTextureBitmapData.colorTransform(new Rectangle(0,0,1024,1024),new ColorTransform(0,0,1));redTextureResource=new BitmapTextureResource(redTextureBitmapData);blueTextureResource=new BitmapTextureResource(blueTextureBitmapData);textureResource=blueTextureResource;material=new TextureMaterial(textureResource);mesh.setMaterialToAllSurfaces(material);mesh1=mesh.clone() as Mesh;mesh1.x=20;mesh2=mesh1.clone() as Mesh;mesh2.x=40;container=new Object3D();container.addChild(mesh);container.addChild(mesh1);container.addChild(mesh2);rootContainer.addChild(container);for each(var resource:Resource in rootContainer.getResources(true)){resource.upload(context3D);}blueTextureResource.upload(context3D);redTextureResource.upload(context3D);stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);}private function onEnterFrame(e:Event):void{controller.update();container.rotationZ+=0.01;camera.render(stage3D);if(rtb){textureResource=blueTextureResource;camera.x=-10;//camera.rotationZ=-0.06;camera.rotationZ=Math.atan2(mesh.y-camera.y,mesh.x-camera.x)*Math.PI/180-0.06;}else{textureResource=redTextureResource;camera.x=10;//camera.rotationZ=0.06;camera.rotationZ=Math.atan2(mesh.y-camera.y,mesh.x-camera.x)*Math.PI/180+0.06;}rtb=!rtb;material=new TextureMaterial(textureResource);mesh.setMaterialToAllSurfaces(material);mesh1.setMaterialToAllSurfaces(material);mesh2.setMaterialToAllSurfaces(material);}}}

代码简陋,里面嵌入模型与贴图在Alternativa3D官方demo中找,或用自己的。

我实现这个立体效果时,想到了立体效果该是两个摄像机拍一个物体,或舞台之类。再让左眼与右眼分别看两个摄像机之图像。两个图像在两个视口,如何混合为一。我没解决,Flare3D应是解决了,观其demo,再截屏。可见


没想出两个视口如何混合在一起输出,据说stage3D图层不支持混合模式。看nVIDIA官网之3D幻境产品介绍,有要求刷新率120的显示器,就想到其可能用交替显示实现左右眼看不同图像。我也用了这个交替渲染的方法。
因咱没有快门式3D眼镜,就用红蓝眼镜将就看看吧 

我的方法截屏是一个图像,没有重叠



不过效果确实不能做啥东西,就是个玩物而已