I'm an Away3D beginner, developing in Flash CS4/AS3, and I'm trying to set up a simple movie to import and display a 3DS file. The importing works, but I can't get the materials to work. I just want your basic colored material with smooth shading, and I have this working for built-in primitives, but the imported only shows an unshaded solid color, with black lines for the edges. Strangely, the color is different each time the movie is loaded.

I've posted a demo on my website at http://suitable.com/temp/away3d/PhongLoadedModel.html. The torus is built using the Torus primitive, and the cylinder is the imported 3DS file. Drag on the movie to navigate. Notice that the torus is properly shaded (including the moving light from the original Away3D demo) while the large cylinder is a flat color with black lines.

My first guess was that there were materials specified in the imported 3DS file, but by tracing the Away3D import code I'm now 90+% sure that's not true. The other options seem to be pilot error (quite possible) or an Away3D bug (who knows).

The code is below, or you can find the demo pieces here:
FLA file: http://suitable.com/temp/away3d/PhongLoadedModel.fla
3DS file: http://suitable.com/temp/away3d/exporttest.3ds

Thanks,
Dan


P.S. Here's the code:

import away3d.cameras.*;
import away3d.containers.*;
import away3d.core.utils.Cast;
import away3d.lights.*;
import away3d.loaders.*;
import away3d.materials.*;
import away3d.primitives.*;

//engine variables
var scene:Scene3D;
var camera:HoverCamera3D;
var view:View3D;

//material objects
var material:PhongColorMaterial;

//light objects
var light1:DirectionalLight3D;
var light2:DirectionalLight3D;

//scene objects
var max3ds:Max3DS;
var loader:LoaderCube;
var torus:Torus;
var model:ObjectContainer3D;

//navigation variables
var move:Boolean = false;
var lastPanAngle:Number;
var lastTiltAngle:Number;
var lastMouseX:Number;
var lastMouseY:Number;

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

/**
 * Global initialise function
 */
function init():void
{
        initEngine();
        initMaterials();
        initLights();
        initObjects();
        initListeners();
}

/**
 * Initialise the engine
 */
function initEngine():void
{
        scene = new Scene3D();
//camera = new HoverCamera3D({focus:50, distance:1000, mintiltangle:0, maxtiltangle:90});
        camera = new HoverCamera3D();
        camera.focus = 50;
        camera.distance = 1000;
        camera.mintiltangle = -80;
        camera.maxtiltangle = 80;

        camera.targetpanangle = camera.panangle = 45;
        camera.targettiltangle = camera.tiltangle = 20;

        //view = new View3D({scene:scene, camera:camera});
        view = new View3D();
        view.scene = scene;
        view.camera = camera;

        addChild(view);
}

/**
 * Initialise the materials
 */
function initMaterials():void
{
        material = new PhongColorMaterial(0x808080);
        material.shininess = 20;
        material.specular = 0.4;
}

/**
 * Initialise the lights
 */
function initLights():void
{
        //light1 = new DirectionalLight3D({y:1, ambient:0.1, diffuse:0.7});
        light1 = new DirectionalLight3D();
        light1.y = 1;
        light1.ambient = 0.1;
        light1.diffuse = 0.7;

        scene.addChild(light1);

//light2 = new DirectionalLight3D({y:1, color:0x00FFFF, ambient:0.1, diffuse:0.7});
        light2 = new DirectionalLight3D();
        light2.y = 1;
        light2.color = 0x00FFFF;
        light2.ambient = 0.1;
        light2.diffuse = 0.7;

        scene.addChild(light2);
}

/**
 * Initialise the scene objects
 */
function initObjects():void {

        max3ds = new Max3DS();
        max3ds.centerMeshes = true;
        max3ds.material = material;
        loader = new LoaderCube();
        loader.loadersize = 200;
        loader.ownCanvas = true;
        loader.addOnSuccess(onSuccess);
        loader.loadGeometry("exporttest.3ds", max3ds);

        scene.addChild(loader);

        torus = new Torus();
        torus.ownCanvas = true;
        torus.material = material;
        torus.radius = 150;
        torus.tube = 60;
        torus.x = 500;
        torus.segmentsR = 12;
        torus.segmentsT = 10;

        scene.addChild(torus);

}

/**
 * Listener function for loading complete event on loader
 */
function onSuccess(event:Event):void
{
        model = loader.handle as ObjectContainer3D;
        model.scale(100);
        model.rotationX = 90;
}

/**
 * Initialise the listeners
 */
function initListeners():void
{
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        stage.addEventListener(Event.RESIZE, onResize);
        onResize();
}

/**
 * Navigation and render loop
 */
function onEnterFrame(event:Event):void
{
        tick(getTimer());

        if (move) {
camera.targetpanangle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle; camera.targettiltangle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
        }

        camera.hover();
        view.render();
}

/**
 * Mouse down listener for navigation
 */
function onMouseDown(event:MouseEvent):void
{
        lastPanAngle = camera.targetpanangle;
        lastTiltAngle = camera.targettiltangle;
        lastMouseX = stage.mouseX;
        lastMouseY = stage.mouseY;
        move = true;
        stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
 * Mouse up listener for navigation
 */
function onMouseUp(event:MouseEvent):void
{
        move = false;
        stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
 * Mouse stage leave listener for navigation
 */
function onStageMouseLeave(event:Event):void
{
        move = false;
        stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

/**
 * Stage listener for resize events
 */
function onResize(event:Event = null):void
{
        view.x = stage.stageWidth / 2;
        view.y = stage.stageHeight / 2;
}

/**
* Updates every frame
*/
function tick(time:int):void
{
        light1.x = Math.cos(time/2000);
        light1.z = Math.sin(time/2000);
}

init();

stop();

--
Daniel T. Griscom             [email protected]
Suitable Systems              http://www.suitable.com/
1 Centre Street, Suite 204    (781) 665-0053
Wakefield, MA  01880-2400

Reply via email to