├── README.md ├── googlepoly-load-component.js ├── googlepoly-search-component.js └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # googlepoly-load-component 2 | 3 | Live demo at https://gpoly.glitch.me/ 4 | 5 | See Google Poly documentation at https://developers.google.com/poly/develop/web 6 | 7 | Optionally you can load via URL using the gpolyid parameter. 8 | 9 | ```html 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | ``` 20 | -------------------------------------------------------------------------------- /googlepoly-load-component.js: -------------------------------------------------------------------------------- 1 | AFRAME.registerComponent('gpoly', { 2 | schema: { 3 | polyid: {default: '5vbJ5vildOq'}, 4 | API_KEY: {default: ''} 5 | }, 6 | init: function () { 7 | var id = this.data.polyid; 8 | var polyid = AFRAME.utils.getUrlParameter('polyid'); 9 | if (polyid.length > 0) id = polyid; 10 | 11 | let API_KEY = this.data.API_KEY; 12 | let url = "https://poly.googleapis.com/v1/assets/"+id+"/?key="+API_KEY; 13 | let el = this.el; 14 | 15 | if (!API_KEY){ 16 | console.log('Please fill in your API KEY, cf https://developers.google.com/poly/develop/web ') 17 | return; 18 | } 19 | 20 | fetch(url) 21 | .then(res => res.json()) 22 | .then((out) => { 23 | var model = out.formats[0].root.url; 24 | var materials = out.formats[0].resources[0].url; 25 | // using ob+mtl since glTF format is not 2.0 26 | el.setAttribute("obj-model", "obj", model ); 27 | el.setAttribute("obj-model", "mtl", materials ); 28 | }) 29 | .catch(err => { throw err }); 30 | } 31 | }); 32 | -------------------------------------------------------------------------------- /googlepoly-search-component.js: -------------------------------------------------------------------------------- 1 | 2 | AFRAME.registerComponent('gpoly-search', { 3 | schema: { 4 | keywords: {default: 'chair'}, 5 | API_KEY: {default: ''} 6 | }, 7 | init: function () { 8 | var keywords = this.data.keywords; 9 | var keywordsparam = AFRAME.utils.getUrlParameter('keywords'); 10 | if (keywordsparam.length > 0) keywords = keywordsparam; 11 | 12 | let API_KEY = this.data.API_KEY; 13 | var url = `https://poly.googleapis.com/v1/assets?keywords=${keywords}&format=OBJ&key=${API_KEY}`; 14 | let el = this.el; 15 | 16 | if (!API_KEY){ 17 | console.log('Please fill in your API KEY, cf https://developers.google.com/poly/develop/web ') 18 | return; 19 | } 20 | 21 | fetch(url) 22 | .then(res => res.json()) 23 | .then((out) => { 24 | var id = out.assets[0].name.replace("assets/","") ; 25 | var entity = document.createElement("a-entity"); 26 | entity.setAttribute("gpoly", "polyid", id); 27 | entity.setAttribute("gpoly", "API_KEY", API_KEY); 28 | el.appendChild(entity); 29 | }) 30 | .catch(err => { throw err }); 31 | } 32 | }); 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Fabien Benetou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------