Using Loader() to load jpeg at runtime

Software: Away3D 4.x

Pokey, Newbie
Posted: 01 June 2013 09:21 PM   Total Posts: 23

Hello all,

I would appreciate some expert insight if anyone has the time.

I am using the code below to try and swap out an embedded image at runtime but the second (loaded) texture appears as grey when the mouse clicks. I am not getting any error messages though.

[Embed(source="../embeds/surface1.jpg")]
 
public static var surfaceDiffuse:Class; 

 ...

 
_loader = new Loader();
 var 
url:String "surface2.jpg";
 var 
urlReq:URLRequest = new URLRequest(url);
 
_loader.load(urlReq);
 
_loader.addEventListener(LoaderEvent.RESOURCE_COMPLETEonSceneResourceComplete);
   
 ... 
   
 
Material = new TextureMaterialCast.bitmapTexturesurfaceDiffuse ) );
 
Material.gloss 5;
 
Material.lightPicker _lightPicker;

 
Material2 = new TextureMaterialCast.bitmapTexture_loader.content ));
 
Material2.gloss 5;
 
Material2.lightPicker _lightPicker;

 
_shpere1 = new Mesh( new SphereGeometry10010080 ), earthMaterial );

 ...
 
 private function 
stageMouseDownHandlerevent:MouseEvent ):void {
 _mouseIsDown 
true;
 
_sphere1.material Material2;
 

TIA

   

Pierce, Jr. Member
Posted: 07 June 2013 07:05 PM   Total Posts: 40   [ # 1 ]

Have you tried sending the bitmapData rather than the DisplayObject itself? This may be a little cleaner. Also, you can try cloning your original material and then changing the texture property rather than creating a new material, or skip the second material altogether and just swap the textures. This way you know that all other factors are perfectly consistent. Beyond that it’s pretty hard to troubleshoot without something compilable. Hopefully, you’ve already figured this one out anyways, but best of luck.

   

Pokey, Newbie
Posted: 08 June 2013 07:15 PM   Total Posts: 23   [ # 2 ]

I did get it working using AssetLibrary (modified from the LoadMD2 example) thusly:

AssetLibrary.load(new URLRequest("surface1.jpg"), nullnull, new ImageParser());
 
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);

 ...

 private function 
onAssetComplete(event:AssetEvent):void
  {
   
if (event.asset.assetType == AssetType.TEXTURE {
    _hiresmap 
event.asset as BitmapTexture;
    
trace(('loaded an '+event.asset))
    
   
}else{trace(("sometin else"))}
  }

 
...

  private function 
stageMouseDownHandlerevent:MouseEvent ):void {
 _mouseIsDown 
true;
  
_sphere1.material = new TextureMaterialCast.bitmapTexture_hiresmap ));
  

I am still a beginner so I have no doubt there are better ways to do this than what I cobbled together. I think I will try your intriguing suggestion of just swapping the bitmap data.

Thanks for the response Pierce.

 

   

ScottL, Newbie
Posted: 16 August 2013 02:24 PM   Total Posts: 7   [ # 3 ]

Thanks for this!  I’ve been looking for a few days now for a way to load textures *without* embedding them.

I had modify the code slightly to get it working for me:

Imports:

import away3d.loaders.parsers.ImageParser;
 
import away3d.loaders.AssetLoader;
 
import away3d.library.assets.*;
 
import away3d.events.AssetEvent;
 
import away3d.utils.Cast;
 
import away3d.materials.*; 

Throw this in your init code:

var skydomeTexture:AssetLoader = new AssetLoader();
skydomeTexture.load(new URLRequest('../assets/maps/park1/skydome.jpg'), nullnull, new ImageParser());
skydomeTexture.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete); 

And the assetComplete function:

private function onAssetComplete(event:AssetEvent):void {
   skySphere
.material = new TextureMaterial(Cast.bitmapTexture(event.asset));

Whiz bang.  Hope this helps someone else.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X