Alternativa 7 loading a .3ds model

With the release of alternativa 7 i thought i’d take a look at it again and see what has changed and improved since i last used it. After struggling somewhat with the way the new version works i eventually understood how to load a 3ds model and apply the textures after reading the documentation and what scraps of information i could find on the forums. So for anyone who is also having trouble getting to grips with it heres an example i made loading the simple alternativa house 3ds file they have used in previous version examples. Enjoy and i hope it helps… Click here to download the .zip file containing the final example I prefer using flash rather than flex or flash builder or anything like that and the truth is i haven’t really had the time to use them and understand them as fully as i’d like to so this tutorial is using flash cs34. STEP 1 - Get all the files you need ready Ok first of all download the alternativa3d library which you can get here : http://alternativaplatform.com/en/alternativa3d/ Now you will also need the house 3ds which can be found in the attachment below or if you have your own model you can try to use that. STEP 2 Create a new flash document and save it somewhere, once thats done go to the publish settings for the document (file - > publish settings) and in the popup click on the flash tab and then click actionscript settings. From here click the library path and add the swc file from alternativa. Once thats all done you can click ok and return to the stage. STEP 3 Now you need to have the texure files, you can either load them using actionscript or just embed them into the flash document. In this case i decided to embed them into the flash document. So just import all your images to the library and right click on each one and go to properties and check the tick box labelled export for actionscript. Make sure to give each image a useful name, i just put bmd standing for bitmapdata on the end of the image e.g fendbmd STEP 4 Right we have all the textures and the flash document setup, now down to some coding… Here is the full code

package  {
import flash.display.MovieClip;
import alternativa.engine3d.containers.ConflictContainer;
import alternativa.engine3d.controllers.SimpleObjectController;
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.View;
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.loaders.Parser3DS;
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.objects.Mesh;
import flash.display.Loader;
import flash.utils.ByteArray;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class house extends MovieClip {
private var bytes:ByteArray = new ByteArray();
private var my3ds:Parser3DS = new Parser3DS();
private var container:ConflictContainer = new ConflictContainer();
private var controller:SimpleObjectController;
private var camera:Camera3D;
private var fencebm:Bitmap = new Bitmap(new fencebmd(0,0));
private var groundbm:Bitmap = new Bitmap(new groundbmd(0,0));
private var housebm:Bitmap = new Bitmap(new housebmd(0,0));
private var smokebm:Bitmap = new Bitmap(new smokebmd(0,0));
public function house()
{
// Camera
camera = new Camera3D();
camera.view = new View(800, 600);
addChild(camera.view);
container.addChild(camera);
camera.rotationX = -120*Math.PI/180;
camera.rotationZ = -10*Math.PI/180;
camera.x = -200;
camera.y = -1000;
camera.z = 400;
// Camera controller
controller = new SimpleObjectController(stage, camera, 200, 3);
// Load 3d Model
var ldr:URLLoader = new URLLoader();
ldr.dataFormat = URLLoaderDataFormat.BINARY;
var req:URLRequest = new URLRequest("model.3ds");
ldr.addEventListener(Event.COMPLETE, onComplete);
ldr.load(req);
// Event Listeners
addEventListener(Event.ENTER_FRAME, onEnterFrame);
// Initial Render
camera.render();
}
private function onComplete(e:Event=null)
{
//get the 3d model bytes and parse them
bytes=e.target.data;
my3ds.parse(bytes);
//house texture
var h:TextureMaterial = new TextureMaterial();
h.texture = housebm.bitmapData;
//ground texture
var g:TextureMaterial = new TextureMaterial();
g.texture = groundbm.bitmapData;
//ground texture
var s:TextureMaterial = new TextureMaterial();
s.texture = smokebm.bitmapData;
//fence texture
var f:TextureMaterial = new TextureMaterial();
f.repeat = true;
f.texture = fencebm.bitmapData;
//get the mesh of each and apply the textures
var Ground:Mesh = my3ds.objects [0] as Mesh;
Ground.setMaterialToAllFaces(g);
var House:Mesh = my3ds.objects [1] as Mesh;
House.setMaterialToAllFaces(h);
var Smoke:Mesh = my3ds.objects [2] as Mesh;
Smoke.setMaterialToAllFaces(s);
var Fence:Mesh = my3ds.objects [18] as Mesh;
Fence.setMaterialToAllFaces(f);
//add all our meshes to the container
container.addChild(Ground);
container.addChild(House);
container.addChild(Smoke);
container.addChild(Fence);
//how to determine the index of my3ds.objects[] for the meshes we want
//trace(my3ds.objects);
}
public function onEnterFrame(e:Event = null):void {
//update controller and camera
controller.update();
camera.render();
}
}
}

Ok so i’ll just go through some minor points, i’m assuming you already know some as3 flash. So we do all the basics setup our package our imports and our document class, then we need to setup some variables for our textures we imported to the flash document in the last step.

private var fencebm:Bitmap = new Bitmap(new fencebmd(0,0));
private var groundbm:Bitmap = new Bitmap(new groundbmd(0,0));
private var housebm:Bitmap = new Bitmap(new housebmd(0,0));
private var smokebm:Bitmap = new Bitmap(new smokebmd(0,0));

We setup our camera our controller event listener and renderer. Notice i’m loading the .3ds file using a URLLoader, i did try to import it to my flash library but i guess that isn’t possible or at least it wouldn’t let me. You can of course embed it if you are using flex. Ok once its loaded the 3ds its down to texuring it and adding it to the container. So we need a texture material for each of the textures to do this we just use the bitmapdata from the images.

//house texture
var h:TextureMaterial = new TextureMaterial();
h.texture = housebm.bitmapData;

Infact now i’ve looked at this you can probably do away with the private variables at the top and just go straight to assigning the bitmap data. (i haven’t tested this)

//house texture
var h:TextureMaterial = new TextureMaterial();
h.texture = new housebmd(0,0);

Anyway once we have our texture materials we need to grab the meshes and apply the materials. To do this i just traced out the objects available and picked out the meshes from this.

//how to determine the index of my3ds.objects[] for the meshes we want
//trace(my3ds.objects);

The output of doing this gave me [Mesh -1mGround],[Mesh 1MHouse],[Mesh s.5Smoke],[Object3D s.5Smoke01],[Object3D s.5Smoke02],[Object3D s.5Smoke03],[Object3D s.5Smoke04],[Object3D s.5Smoke05],[Object3D s.5Smoke06],[Object3D s.5Smoke07],[Object3D s.5Smoke08],[Object3D s.5Smoke09],[Object3D s.5Smoke10],[Object3D s.5Smoke11],[Object3D s.5Smoke12],[Object3D s.5Smoke13],[Object3D s.5Smoke14],[Object3D s.5Smoke15],[Mesh mGrid] Where i can see that indexes 0,1,2 and 18 are all meshes. The rest is some animated smoke that was in the model and i dont know how to get this working :) So now i know this i can get the meshes and apply the materials and then add it to the container like so

var House:Mesh = my3ds.objects [1] as Mesh;
House.setMaterialToAllFaces(h);
container.addChild(House);

and thats it job done. I hope this helps. [gallery]

Downloads

Comments

  • avatar-vladimir-babushkin
    # Vladimir Babushkin
    Hi. Nice tutorial. We opened special section in our forum: http://forum.alternativaplatform.com/forums/show/35.page Offer this tutorial on forum.
  • avatar-frank-hemsworth
    # Frank Hemsworth

    When i downloaded your code i get this error:

    1046: Type was not found or was not a compile-time constant: Context3D.

    Any ideas? googled it and nothing came up…

  • avatar-davidejones
    # davidejones
    Context3D is for alternativa v8 i believe. I’m doing a few version 8 examples soon but you might want to check out alternativaplatform.com or the examples as you may be mixing versions.
  • avatar-vasta
    # vasta

    Hello, pls can you help me? I’ve got this problem:

    …\House\src\house.as(30): col: 47 Error: Call to a possibly undefined method fencebmd. private var fencebm:Bitmap = new Bitmap(new fencebmd(0,0));

    …\House\src\house.as(30): col: 47 Error: Call to a possibly undefined method fencebmd. private var fencebm:Bitmap = new Bitmap(new fencebmd(0,0));

    …\House\src\house.as(31): col: 48 Error: Call to a possibly undefined method groundbmd. private var groundbm:Bitmap = new Bitmap(new groundbmd(0,0));

    …\House\src\house.as(31): col: 48 Error: Call to a possibly undefined method groundbmd. private var groundbm:Bitmap = new Bitmap(new groundbmd(0,0));

    …\House\src\house.as(32): col: 47 Error: Call to a possibly undefined method housebmd. private var housebm:Bitmap = new Bitmap(new housebmd(0,0));

    …\House\src\house.as(32): col: 47 Error: Call to a possibly undefined method housebmd. private var housebm:Bitmap = new Bitmap(new housebmd(0,0));

    …\House\src\house.as(33): col: 47 Error: Call to a possibly undefined method smokebmd. private var smokebm:Bitmap = new Bitmap(new smokebmd(0,0));

    …\House\src\house.as(33): col: 47 Error: Call to a possibly undefined method smokebmd. private var smokebm:Bitmap = new Bitmap(new smokebmd(0,0));

  • avatar-davidejones
    # davidejones
    I believe this example at the time used adobe flash and i had the assets embedded in the library. You either need to do the same or use the flex style embeds in the actionscript.
  • avatar-melvin
    # Melvin

    TypeError: Error #1009: Cannot access a property or method of a null object reference. at alternativa.engine3d.loaders::Parser3DS/buildMesh() at alternativa.engine3d.loaders::Parser3DS/buildContent() at alternativa.engine3d.loaders::Parser3DS/parse() at Seprai/onComplete() at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()

    hello,

    i just disable var bitmap, and texture, and i get this error, can you help me? thank you

    • avatar-davidejones
      # davidejones
      i’m a bit rusty on this now but it sounds like it needed something you disabled or is still referencing the variable even though its empty (null).

Comments are currently closed