Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GLTF2 format support #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions googlepoly-load-component.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
AFRAME.registerComponent('gpoly', {
schema: {
polyid: {default: '5vbJ5vildOq'},
API_KEY: {default: ''}
polyid: { default: '5vbJ5vildOq' },
API_KEY: { default: '' }
},
init: function () {
var id = this.data.polyid;
var polyid = AFRAME.utils.getUrlParameter('polyid');
if (polyid.length > 0) id = polyid;

let API_KEY = this.data.API_KEY;
let url = "https://poly.googleapis.com/v1/assets/"+id+"/?key="+API_KEY;
let url = "https://poly.googleapis.com/v1/assets/" + id + "/?key=" + API_KEY;
let el = this.el;

if (!API_KEY){
if (!API_KEY) {
console.log('Please fill in your API KEY, cf https://developers.google.com/poly/develop/web ')
return;
}

fetch(url)
.then(res => res.json())
.then((out) => {
var model = out.formats[0].root.url;
var materials = out.formats[0].resources[0].url;
// using ob+mtl since glTF format is not 2.0
el.setAttribute("obj-model", "obj", model );
el.setAttribute("obj-model", "mtl", materials );
})
.catch(err => { throw err });
.then(res => res.json())
.then((data) => {
const gltf2Format = data.formats.find((f) => f.formatType === "GLTF2")
if (gltf2Format) { // Use <a-gltf-model> if GLTF2 format is available
el.setAttribute("gltf-model", gltf2Format.root.url);
} else {
const objFormat = data.formats.find((f) => f.formatType === "OBJ")
if (objFormat) { // Use <a-obj-model> if OBJ format is available
el.setAttribute("obj-model", "obj", objFormat.root.url);
el.setAttribute("obj-model", "mtl", objFormat.resources[0].url);
}
}
})
.catch(err => { throw err });
}
});
});