ITeye博客

做了一个变换模型上颜色的程序

fourfire · 2007-09-05 20:19 · flex · Markdown

1 install 3dmax Collada plug-in
2 draw a model in 3dmax or download one
3 export the model from 3dmax
4 install PV3D to flex
5 PV3D1.5 ,AS3
refer to code below
room.as
package
{
    import flash.display.Sprite;
    import flash.display.*;
    import flash.events.*;
    import mx.controls.Alert;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    // Import Papervision3D
    import org.papervision3d.scenes.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.materials.*;
    public class Room extends Sprite
    {
        private var container :Sprite;
        private var scene     :Scene3D;
        private var camera    :Camera3D;
        private var sphere    :Sphere;
        private var alphas      :Array=new Array(100,100,100);
        private var colors      :Array=new Array(0x777777,0x111111,0x555555);
        private var materials      :Array=new Array("lhd-2001-1-0055-1","lhd-2001-1-0055-2","Material__7");
        private var complexObject:DisplayObject3D=new DisplayObject3D();
        [Embed(source="model/table.DAE", mimeType="application/octet-stream")]
          private var MyCollada:Class;
         
//          private var loader:URLLoader = new URLLoader();
//        private var request:URLRequest = new URLRequest("model/table.DAE");
        public function Room()
        {
            this.init3D();
            this.camera.x = 100;
            this.camera.y = 0;
            this.camera.z = 0;
            this.scene.renderCamera( camera );
           
        // onEnterFrame
        this.addEventListener( Event.ENTER_FRAME, loop3D );
       
        }
        public function setAlpha(inx:Number,alphaNum:Number):void
        {
            alphas[inx]=alphaNum;
            var child:Collada=complexObject.getChildByName("bed") as Collada;
           
            child.getMaterialsList().getMaterialByName(materials[inx]).fillAlpha=alphaNum;
            this.scene.renderCamera( camera );
        }
        public function setColor(inx:Number,colorNum:Number):void
        {
            colors[inx]=colorNum;
           
            var child:Collada=complexObject.getChildByName("bed") as Collada;
           
            child.getMaterialsList().getMaterialByName(materials[inx]).fillColor=colorNum;
            this.scene.renderCamera( camera );
        }
        private function init3D():void
        {
            this.container = new Sprite();
            addChild( container );
            this.container.x = 320;
            this.container.y = 240;
            this.scene = new Scene3D( container );
            this.camera = new Camera3D();
            this.addRoom();
//            loader.load(request);
//              loader.addEventListener(Event.COMPLETE, onComplete);   
//            loader.addEventListener(Event.ACTIVATE, onActive);
        }
        private function addRoom():void
        {
    //        addFloor();
//            addWall();
            addBed();
//            complexObject.yaw(10);
            this.scene.addChild( complexObject );
        }
        private function addFloor():void
        {
           
            var material:ColorMaterial = new ColorMaterial();
                material.fillColor = 0xFF0000;
                material.doubleSided = true;
                material.fillAlpha = 1.0;
                material.lineAlpha = 0;
                material.lineColor = 0x000000;
            var floor:Cube=new Cube(material,500,500,10);
            floor.x=0;
            floor.y=-100;
            floor.z=0;
            complexObject.addChild( floor );
           
            //complexObject.addChild( new Cube(new ColorMaterial(0x00ff00),200,200,100) );
        }
        private function addWall():void
        {
           
            var material:ColorMaterial = new ColorMaterial();
                material.fillColor = 0x0000FF;
                material.doubleSided = true;
                material.fillAlpha = 1.0;
                material.lineAlpha = 0;
                material.lineColor = 0x000000;
            var wall:Cube=new Cube(material,500,10,200);
           
            wall.x=0;
            wall.y=0;
            wall.z=-250;
            wall.rotationY=90;
            complexObject.addChild( wall );
        }
        private function addDoor():void
        {
            var material:ColorMaterial = new ColorMaterial();
                material.fillColor = 0x0000FF;
                material.doubleSided = true;
                material.fillAlpha = 1.0;
                material.lineAlpha = 0;
                material.lineColor = 0x000000;
           
            var door:Ase=new Ase(material,"model/door.ase");
           
//            door.scaleY=0.05;
//            door.scaleZ=0.1;
           
           
//            door.x=0;
//            door.y=0;
//            door.z=-250;
//            door.rotationY=90;
            complexObject.addChild( door );
        }
        private function addBed():void
        {
           
//            var material:BitmapFileMaterial=new BitmapFileMaterial("images/11408839842840.jpg");
                var mlist:MaterialsList=new MaterialsList();
//                mlist.addMaterial(material,"lhd-2001-1-0055-2");
                //mlist.addMaterial(new BitmapFileMaterial("images/RadioButton.png"),"lhd-2001-1-0055-1");
                for (var i:uint=0;i

room3d.mxml 代码

 

  1. xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
  3. <mx:HDividedBox x="19" y="19" width="100%" height="100%">
  4. <mx:Panel width="250" height="100%" layout="absolute">
  5. <mx:ColorPicker id="color1" x="57" y="35" width="66" change="colorChange(event)"/>
  6. <mx:Label x="8" y="35" text="台面色" width="51"/>
  7. <mx:ColorPicker id="color2" x="57" y="76" width="66" change="colorChange(event)"/>
  8. <mx:Label x="10" y="76" text="茶几腿"/>
  9. <mx:Label x="10" y="120" text="轮子"/>
  10. <mx:ColorPicker id="color3" x="57" y="120" width="66" change="colorChange(event)"/>
  11. <mx:HSlider id="alpha1" x="131" y="36" width="66" minimum="0" maximum="1" value="1" change="alphaChange(event)"/>
  12. <mx:HSlider id="alpha2" x="131" y="81" width="66" minimum="0" maximum="1" value="1" change="alphaChange(event)"/>
  13. <mx:HSlider id="alpha3" x="131" y="126" width="66" minimum="0" maximum="1" value="1" change="alphaChange(event)"/>
  14. mx:Panel>
  15. <mx:Canvas id="drawCanvas">
  16. mx:Canvas>
  17. mx:HDividedBox>
  18. <mx:Script>
  19. mx:Script>
  20. mx:Application>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。