I have moved to using the AssetLibrary for reasons like all that
above. Here's what I'd do:
AssetLibrary.enableParser(OBJParser);
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE,
onAssetRetrieved);
AssetLibrary.addEventListener(LoaderEvent.RESOURCE_COMPLETE,
onResourceComplete);
AssetLibrary.load(new treedata()); // classes should be capitalized
btw
then name your stuff in the onAssetRetrieved function:
function onAssetRetrieved(event:AssetEvent) : void {
trace(event.asset.assetType);
if (event.asset.assetType == AssetType.MESH) event.asset.name =
"treeMesh";
if (event.asset.assetType == AssetType.BITMAP) event.asset.name =
"treeBitmap"; // for a model that loads a texture file
}
then when everything has been parsed completely
function onResourceComplete(event:LoaderEvent):void {
var mesh:Mesh = AssetLibrary.getAsset("treeMesh") as Mesh;
var treematerial:BitmapMaterial = new BitmapMaterial(new
treeimage().bitmapData,true);
mesh.material = treematerial;
}
I think it's way cleaner than all that weird for loop stuff and you
can access stuff in the library whenever you need to. Also, not sure
if you are declaring variables the way you show above, but typing them
when you declare helps a lot in debugging.
On May 31, 6:57 pm, McFunkypants <[email protected]> wrote:
> Hello,
>
> Firstly, thanks guys for being such a wonderful, hard-working team. I
> have what I believe to be an ultra-simple conundrum.
>
> I would like to change the material of a Loader3D asset. I'm stumped!
>
> Using the most recent GIT version of away3d 4 broomstick, I am
> successfully loading an embedded mesh into my scene and it properly
> parses and triggers a LoaderEvent.RESOURCE_COMPLETE event. No errors:
> it parses perfectly. It renders just fine in my scene, by the way -
> just with a black and white checkered default material.
>
> This object has zero for the numChildren property and so I cannot use:
>
> private function onResourceRetrieved_tree(e:LoaderEvent) : void
> {
> for (var i : int = 0; i < tree1.numChildren; ++i) // has no
> children!
> {
> mesh = Mesh(tree1.getChildAt(i));
> mesh.material = treematerial;
> }
>
> }
>
> I also cannot seem to cast a Loader3D to be a Mesh nor does it have a
> material property, so neither of these two attempts works if added to
> the above function:
>
> Mesh(tree1).material = treematerial;
> ObjectContainer3D(tree1).material = treematerial;
>
> Just to help, here's the code that loads this tree mesh. I've tried
> both 3ds and obj.
>
> tree1 = new Loader3D(false);
> tree1.addEventListener(LoaderEvent.LOAD_ERROR,
> onResourceLoadingError_tree);
> tree1.addEventListener(LoaderEvent.RESOURCE_COMPLETE,
> onResourceRetrieved_tree);
> tree1.parseData(new treedata(), new OBJParser(),new
> AssetLoaderContext(false),null);
> view.scene.addChild(tree1);
> treematerial = new BitmapMaterial(new treeimage().bitmapData,true);
> treematerial.lights = [mylight];
> treematerial.ambientColor = 0x777777;
> // I also tried AssetLoaderContext and mapUrlToData with no errors but
> nothing happens.
>
> This stuff really shouldn't be so hard. =( I feel pretty stupid
> wasting 4 hours on this. Oh model loader code, why art thou so
> complex and counterintuitive? I sure would like Loader3D.material =
> to just automagically work.
>
> I would be most grateful for any hints on setting the material of a
> completely embedded model file that has no children meshes but renders
> just fine. What prop lets me access the mesh in this case?
>
> What I really want is a way to either cast Loader3D to Mesh or
> ObjectContainer3D, or some way to change the material of a Loader3D
> object.
>
> THANK YOU VERY MUCH! I'm really grateful for any help.