Hi all!
I’m new to away3D. I’m trying to do a what I suppose is a simple task, but I can’t get it to work.
I would like to load a model and then load a texture and apply it to the model.
It won’t work. What am I doing wrong?
Here’s my code:
import away3d.containers.View3D;
import flash.events.Event;
import flash.display.MovieClip;
import away3d.containers.View3D;
import flash.geom.Vector3D;
import away3d.materials.ColorMaterial;
import away3d.materials.TextureMaterial;
import away3d.textures.BitmapTexture;
import away3d.events.LoaderEvent;
import away3d.loaders.Loader3D;
import away3d.loaders.parsers.Parsers;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Bitmap;
var view:View3D;
var modelLoader:Loader3D;
var myBitmap:Bitmap
var textureLoader:Loader
var material:TextureMaterial
function initView()
{
view = new View3D();
view.backgroundColor = 0x666666;
//position and target the camera
view.camera.position = new Vector3D(0,50,-80);
view.camera.lookAt(new Vector3D());
//add the view to stage;
addChild(view);
loadTexture()
}
function loadTexture()
{
textureLoader = new Loader()
textureLoader.load(new URLRequest("teapot.jpg"));
textureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureLoaded);
}
function textureLoaded(evt:Event):void
{
var bmp01:Bitmap = textureLoader.content as Bitmap
myBitmap = new Bitmap(bmp01.bitmapData)
myBitmap.smoothing = true
material = new TextureMaterial(new BitmapTexture(myBitmap))
loadModel();
}
function loadModel()
{
Parsers.enableAllBundled();
modelLoader = new Loader3D();
modelLoader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, modelLoaded);
modelLoader.addEventListener(LoaderEvent.LOAD_ERROR, loadError);
modelLoader.load( new URLRequest('teapot.obj') );
}
function modelLoaded(evt:LoaderEvent):void
{
modelLoader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, modelLoaded);
modelLoader.removeEventListener(LoaderEvent.LOAD_ERROR, loadError);
modelLoader.material = material
view.scene.addChild(modelLoader);
this.addEventListener(Event.ENTER_FRAME, render);
}
function loadError(evt:LoaderEvent):void
{
trace('Could not find', ev.url);
modelLoader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, modelLoaded);
modelLoader.removeEventListener(LoaderEvent.LOAD_ERROR, loadError);
modelLoader = null;
}
function render(ev : Event):void
{
modelLoader.rotationY += 2;
view.render();
}
initView();