Start a new topic

Crash loading 3D Model

Crash loading 3D Model


Hi,

 

I'm using this code to load a 3D Model

// implementation of AR-Experience (aka "World")

var World = {

    

    initiallyLoaded : false,

    model: new AR.Model("./3d/sample.wt3"),

    modelAnim: new AR.ModelAnimation(model, "sample"),

    

    locationChanged: function locationChangedFn(lat, lon, alt, acc)

    {

        if (! initiallyLoaded)

        {

            if (model.isLoaded)

            {

                modelAnim.start();

                initiallyLoaded = true;

            }

        }

    }

    

};

 

  AR.context.onLocationChanged = World.locationChanged;

 

 

But app crashes and nothing starts... Why? I can't find some tutorials about this argument, so I tried and I don't know if code is correct.

Hi,

Your JavaScript snippet is not valid unfortunately. When you run your code in the ADE, the JavaScript console will give you the following error:

ReferenceError: model is not defined

This is due to the fact that when you create the ModelAnimation, JavaScript has not yet created the Model that you have specified in the line above.

 

To solve this issue, you can use a snippet like this:

// implementation of AR-Experience (aka "World")

var World = function(){

    

    var initiallyLoaded = false;

    var model = new AR.Model("./3d/sample.wt3");

    var modelAnim = new AR.ModelAnimation(model, "sample");

    

    this.locationChanged = function locationChangedFn(lat, lon, alt, acc)

    {

        if (! initiallyLoaded)

        {

            if (model.isLoaded)

            {

                modelAnim.start();

                initiallyLoaded = true;

            }

        }

    }

    

};

 

//generate the world

var world = new World();

 

AR.context.onLocationChanged = world.locationChanged;

Ok, now I changed the code like below, but it doesn't show anything focusing the camera on defined geolocation (you have not to look at locationChanged that it is, in this case, useless):

 

var World =

{

    

    location : new AR.GeoLocation(41.773046, 12.237229, 3.0),

    initiallyLoaded : false,

    model : new AR.Model("3d/car.wt3", {

                         onLoaded: function()

                            {

                         geoObject = new AR.GeoObject(this.location);

                         geoObject.drawables.addCamDrawable =  this.model;

                            }

                         }),

    

    locationChanged: function locationChangedFn(lat, lon, alt, acc)

    {

        document.getElementById("page1").innerHTML = "Ciao";

 

        if (! World.initiallyLoaded)

        {

            if (World.model.isLoaded())

            {

                World.initiallyLoaded = true;

            }

        }

    }

    

};

 

var world = new World();

/* forward locationChanges to custom function */

AR.context.onLocationChanged = World.locationChanged;

Hi,

Sorry, this will not work. There are several issues in this snippet, it is invalid JavaScript. You cannot use new World() when World is not a function (see my example in the previous post).

And you need to remember that 

geoObject.drawables.addCamDrawable

is a function. You should not override it with

geoObject.drawables.addCamDrawable =  this.model;

but instead must use

geoObject.drawables.addCamDrawable(this.model);

So, your best bet is to use the snippet I have provided, and then modify the onLoaded function of the Model as you did in your post.

Please also use the ADE and always check the developer console in your browser before asking for help with the code. The Browser will tell you if the JavaScript is valid or not. In your case with the previous snippet, the Browser immediately tells you that there is something wrong in the JavaScript. It tells you:

TypeError: object is not a function

because you used new World(), but World is a plain JavaScript object, not a function.

Best,

Martin

Really, thank you :) 

 

I solved using this code:

// implementation of AR-Experience (aka "World")

var World = function()

{

    

    var location = new AR.GeoLocation(41.773046, 12.237229, 3.0);

    var initiallyLoaded = false;

    var model = new AR.Model("3d/car.wt3", {

                            onLoaded: function()

                            {

                                var geoObject = new AR.GeoObject(location);

                                geoObject.drawables.addCamDrawable(model);

                            }

                        });

    

    var locationChanged = function locationChangedFn(lat, lon, alt, acc)

    {

        document.getElementById("page1").innerHTML = "Ciao";

 

        if (! World.initiallyLoaded)

        {

        }

    };

    

};

 

var init = new World();

/* forward locationChanges to custom function */

AR.context.onLocationChanged = World.locationChanged;

 
Login or Signup to post a comment