Load model and texture

Software: Away3D 4.x

cledus, Newbie
Posted: 07 December 2012 09:57 AM   Total Posts: 11

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.COMPLETEtextureLoaded);
}

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_COMPLETEmodelLoaded);
 
modelLoader.addEventListener(LoaderEvent.LOAD_ERRORloadError);
 
modelLoader.load( new URLRequest('teapot.obj') );
}

function modelLoaded(evt:LoaderEvent):void
{
 modelLoader
.removeEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
 
modelLoader.removeEventListener(LoaderEvent.LOAD_ERRORloadError);
 
modelLoader.material material
 view
.scene.addChild(modelLoader);
 
this.addEventListener(Event.ENTER_FRAMErender);
}

function loadError(evt:LoaderEvent):void
{
 trace
('Could not find'ev.url);
 
modelLoader.removeEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
 
modelLoader.removeEventListener(LoaderEvent.LOAD_ERRORloadError);
 
modelLoader null;
}

function render(ev Event):void
{
 modelLoader
.rotationY +=  2;
 
view.render();
}

initView
(); 
   

Avatar
TrueSign, Member
Posted: 07 December 2012 01:31 PM   Total Posts: 57   [ # 1 ]

Try this:

private var _teapot:Mesh;
private var 
_teapotMaterial:TextureMaterial;

[Embed(source="/../assets/teapot.obj"mimeType="application/octet-stream")]
private var TeapotModel : Class;
[Embed(source="/../assets/teapot.jpg")]
private var TeapotTexture : Class;

var 
bitmapTeapotTexture:BitmapTexture = new BitmapTexture(new TeopotTexture().bitmapData);
_teapotMaterial= new TextureMaterial(bitmapTeapotTexture);

AssetLibrary.enableParser(OBJParser);
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
AssetLibrary.loadData(new TeapotModel (), null"Teapot");

private function 
onAssetComplete(event:AssetEvent):void{
 _teapot 
event.asset as Mesh;
 
_teapot.material _teapotMaterial;
 
_view.scene.addChild(_teapot);
   

cledus, Newbie
Posted: 07 December 2012 01:53 PM   Total Posts: 11   [ # 2 ]

Thanks for the reply!

I’m not using flex i’m only using flash professional. I’m writing all the code directly in flash.

But I have been getting somewhere. Now my only problem is the applying of the bitmap to the material. I’m getting the error:“1067: Implicit coercion of a value of type flash.display:Bitmap to an unrelated type flash.display:BitmapData.”

Here is my updated 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;
import away3d.entities.Mesh;
import away3d.containers.ObjectContainer3D;
import away3d.lights.PointLight;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.cameras.lenses.PerspectiveLens;

var 
view:View3D;
var 
modelLoader:Loader3D;

var 
myBitmap:Bitmap
var textureLoader:Loader
//var material:ColorMaterial
var material:TextureMaterial
var model:Mesh

var mainLight:PointLight
var fillLight:PointLight
var backLight:PointLight

function initView()
{
 view 
= new View3D();
 
view.backgroundColor 0x666666;
 
//position and target the camera
 
view.camera.lens = new PerspectiveLens()
 
view.camera.position = new Vector3D(0,50,-80);
 
view.camera.lookAt(new Vector3D());
 
//add the view to stage;
 
addChild(view);
 
 
//initLights()
 
loadTexture()
}

function loadTexture()
{
 textureLoader 
= new Loader()
 
textureLoader.load(new URLRequest("teapot.jpg"));
 
textureLoader.contentLoaderInfo.addEventListener(Event.COMPLETEtextureLoaded);
}

function textureLoaded(evt:Event):void 

 
var bmp01:Bitmap textureLoader.content as Bitmap
 myBitmap 
= new Bitmap(bmp01.bitmapData)
 
myBitmap.smoothing true
 initLights
()
}

function initLights()
{
 mainLight 
= new PointLight()
 
fillLight = new PointLight()
 
backLight = new PointLight()
 
 
mainLight.color 0xFFFFFF
 fillLight
.color 0x999999
 backLight
.color 0x666666
 
 mainLight
.position = new Vector3D(200,200,-200)
 
fillLight.position = new Vector3D(-200,200,-200)
 
backLight.position = new Vector3D(0,200,200)
 
 
view.scene.addChild(mainLight)
 
view.scene.addChild(fillLight)
 
view.scene.addChild(backLight)
 
initMaterial()
}

function initMaterial()
{
 material 
= new TextureMaterial(new BitmapTexture(myBitmap))
 
//material = new ColorMaterial(0xCCCCCC)
 
material.lightPicker = new StaticLightPicker([mainLightfillLightbackLight])
 
initLoader()
}

function initLoader()
{
 
while (view.scene.numChildren 0)
 
{
  view
.scene.removeChild(view.scene.getChildAt(0));
 
}
 
 Parsers
.enableAllBundled();
 
modelLoader = new Loader3D();
 
modelLoader.addEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
 
modelLoader.addEventListener(LoaderEvent.LOAD_ERRORmodelError);
 
modelLoader.load( new URLRequest('teapot.obj') );
 
view.scene.addChild(modelLoader);
 
view.camera.lookAt(modelLoader.position);
}

function modelLoaded(evt:LoaderEvent):void
{
 modelLoader
.removeEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
 
modelLoader.removeEventListener(LoaderEvent.LOAD_ERRORmodelError);
 
model Mesh(modelLoader.getChildAt(0));
 
model.material material
 view
.scene.addChild(model);
 
modelLoader.dispose()
 
modelLoader null;
 
 
addEventListener(Event.ENTER_FRAMErender);
}

function modelError(evt:LoaderEvent):void
{
 trace
('Could not find'evt.url);
 
modelLoader.removeEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
 
modelLoader.removeEventListener(LoaderEvent.LOAD_ERRORmodelError);
 
modelLoader null;
}

function render(ev Event):void
{
 model
.rotationY +=  2;
 
view.render();
}

initView
(); 
   

Avatar
TrueSign, Member
Posted: 07 December 2012 02:27 PM   Total Posts: 57   [ # 3 ]
var myBitmap:TextureMaterial;

function 
textureLoaded(evt:Event):void 

 
var bmp01:Bitmap textureLoader.content as Bitmap;
var 
bitmapTeapotTexture:BitmapTexture = new BitmapTexture(bmp01.bitmapData);
 
myBitmap = new TextureMaterial(bitmapTeapotTexture)
 
myBitmap.smoothing true
 initLights
()
   

cledus, Newbie
Posted: 07 December 2012 04:26 PM   Total Posts: 11   [ # 4 ]

It worked!!!

Thank you so much!!

   

parele, Jr. Member
Posted: 25 March 2013 02:07 AM   Total Posts: 43   [ # 5 ]

If your using flash cs5 or what have you you still can use flex code, What you need to do is download flex SDK 4.6 or the latest and add the lib path to your document class smile

works for me!

   

cledus, Newbie
Posted: 25 March 2013 07:49 AM   Total Posts: 11   [ # 6 ]

@parele Thanks for the tip!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X