How can i load model on run time loading fine on static url

Play canvas code:

var Obj = pc.createScript('obj');

window.onmessage = function(e){
Obj.attributes.add('material', { type: 'asset', assetType: 'material' });
    Obj.prototype.loadModel = function (objStr) {
            var objData = new OBJ.Mesh(objStr);
            var url;
            var options = {};
            if (objData.vertexNormals) options.normals = objData.vertexNormals;
            if (objData.textures) options.uvs = objData.textures;
            if (objData.indices) options.indices = objData.indices;
            if (objData.vertices && objData.vertexNormals && objData.textures && objData.indices) {
                var tangents = pc.calculateTangents(objData.vertices, objData.vertexNormals, objData.textures, objData.indices);
                options.tangents = tangents;

            var mesh = pc.createMesh(, objData.vertices, options);
            var node = new pc.GraphNode();
            var material = this.material ? this.material.resource : new pc.StandardMaterial();
            var meshInstance = new pc.MeshInstance(node, mesh, material);
            var model = new pc.Model();
            model.graph = node;
            model.meshInstances = [ meshInstance ];

            this.entity.model.model = model;


        Obj.prototype.initialize = function() {
        var asset = new pc.Asset("obj", "text", {
            url: ''

        var self = this;
        asset.ready(function (asset) {
        Obj.prototype.update = function(dt) {

        // url = '';
        // Obj.attributes.add('url', { type: 'string' });

Web page code :

Click me
                                                function myFunction(){
                                                    var app = document.getElementById('app');
                                                    app.contentWindow.postMessage('{{ $models_3d->model }}', '*');

Hi @Fahadi_Nadeem,

Loading an .obj model on runtime requires first adding to your project the obj-loader extension, from the Playcanvas repo.

You can find the script here:

And here is an example on how to add it to your project and then how to call the loadFromUrl() to load the remote file:

1 Like

We have worked on it its working fine when we code the static url but when use the postmessage its not working with iframe please let me know what to do with iframe

Sorry that’s on your parent website codebase, not Playcanvas.

You will have to search for a JavaScript tutorial on how to setup a communication between two windows using postMessage.

@Leonidas we are already communicating the communication works fine i have debug the communication is working fine but the model is not load after i get the url from website that the problem

Try sharing here a url to your project to take a look, not sure what’s wrong with the code you shared.

What error message do you get as well?

Is new OBJ.Mesh(objStr) one of your classes/code?

Looking at the code of the project, you seem to have wrapped a bunch of code in the on window.message function.

Sounds like this is what you wanted to do and have to on message callback do something with the message string?

1 Like

@yaustar link is not working !


this is the test link i have created for you !

yes !

yes exactly !

Oops. Left it on private. It’s now public.

can you please check the link and suggest the code

You can now access the project I posted with the code that I believe fixes your issue. You still need to tweak it should be pretty straightforward forward.


their is an error of undefined asset

it is showing :Uncaught TypeError: Cannot read property ‘assets’ of undefined

I’ve modified the PlayCanvas project and set up an example on JSFiddle:
The code is just a modified version of what was given here: Connect web App with Playcanvas iFrame

What if we want to load on page load not on click when we should load it ?

  1. You should send first a message from the Playcanvas side to your parent page, that Playcanvas loading has finished, app is ready.

  2. As a response send back from the parent page to Playcanvas the message @yaustar prepared on jsfiddle:

app.contentWindow.postMessage('', '*');