I’m loading a complex model made in Cinema 4d as .OBJ
Probably the geometry is not correct, but it can be rendered in 3D max.
After loading, I’m going to apply new material and a light to the loaded object.
Problems:
1. New Material is applied only to a fragment of the model. Not sure how to apply it to all.
2. When I add light (uncommenting the line), I get “RangeError: Error #3669:”
Can anybody help? Please!
package
{
import away3d.cameras.Camera3D;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.View3D;
import away3d.events.LoaderEvent;
import away3d.loaders.Loader3D;
import away3d.loaders.parsers.Max3DSParser;
import away3d.loaders.parsers.OBJParser;
import away3d.lights.DirectionalLight;
import away3d.lights.LightBase;
import away3d.lights.PointLight;
import away3d.materials.ColorMaterial
import away3d.materials.SegmentMaterial
import away3d.materials.methods.OutlineMethod
import away3d.entities.Mesh;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.net.URLRequest;
[SWF(width="800", height="450", frameRate="60", backgroundColor="#ffffff")]
public class LoaderObject extends Sprite
{
private var _view : View3D;
private var _container : Loader3D;
private var _light3 : LightBase;
public function LoaderObject()
{
_view = new View3D();
_view.antiAlias = 2;
_view.backgroundColor = 0xffff00;
var camera:Camera3D = _view.camera;
camera.lens = new PerspectiveLens();
camera.z = 800;
_light3 = new DirectionalLight(-1000000, -1000000, -1000000);
_light3.specular = 0;
_light3.diffuse = 0.3;
_light3.color = 0xffffff;
_view.scene.addChild(_light3);
addChild(_view);
Loader3D.enableParser(OBJParser);
_container = new Loader3D();
_container.scale(10);
_container.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
_container.addEventListener(LoaderEvent.LOAD_ERROR, onResourceLoadingError);
_container.load(new URLRequest('mymodels/wto.obj'));
_view.scene.addChild(_container);
_view.camera.lookAt(_container.position);
_view.camera.lens.far = Vector3D.distance(_container.position, _view.camera.position)+100000;
_view.camera.lens.near = 10;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onStageResize);
}
private function onStageResize(event : Event) : void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
private function onResourceComplete(e : away3d.events.LoaderEvent) : void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
setSub(_container)
}
private function setSub(Msh) {
var mtr = new ColorMaterial(0x0000ff, 1)
mtr.ambientColor = 0x0000ff
var nc = Msh.numChildren
if (nc>0) {
for (var i=0; i<nc; i++) {
var subMesh = Mesh(Msh.getChildAt(i));
subMesh.material = mtr;
// subMesh.material.lights=[_light3]
setSub(subMesh)
}
}
}
private function onResourceLoadingError(e:LoaderEvent) : void
{
trace("oops, model load failed");
}
private function onResourceMapsLoadingError(e:LoaderEvent) : void
{
trace("A map failed to load in this model: ["+e.url+"]");
}
private function handleEnterFrame(e : Event) : void
{
_container.rotationY += .5;
_view.render();
}
}
}