---
title: "做了一个变换模型上颜色的程序"
author: "fourfire"
date: "2007-09-05T20:19:33Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-09-05T20:19:33Z"
id: 120753
url: "https://fourfire.iteye.com/blog/120753"
---

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 代码

 

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