forked from paddoum/poly-sample-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-sample.html
179 lines (129 loc) · 4.4 KB
/
basic-sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<!--
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html {
height: 100%;
}
body {
height: 100%;
font-family: sans-serif;
line-height: 24px;
}
#viewer {
width: 100%;
height: calc(100% - 80px);
}
#info {
padding: 12px;
text-align: center;
}
#asset_name {
font-size: 22px;
font-weight: bold;
}
#asset_author {
color: #888;
}
</style>
</head>
<body>
<div id="viewer"></div>
<div id="info">
<span id="asset_name">Title</span><br/>
by <span id="asset_author">Author</span>
</div>
<script src="third_party/three.js/js/three.min.js"></script>
<script src="third_party/three.js/js/OBJLoader.js"></script>
<script src="third_party/three.js/js/MTLLoader.js"></script>
<script>
// THREE.JS VIEWER
const WIDTH = viewer.offsetWidth;
const HEIGHT = viewer.offsetHeight;
var camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 0.01, 100 );
camera.position.set( 5, 3, 5 );
camera.lookAt( 0, 1.5, 0 );
var scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
scene.add( new THREE.GridHelper( 10, 10 ) );
var ambient = new THREE.HemisphereLight( 0xbbbbff, 0x886666, 0.75 );
ambient.position.set( -0.5, 0.75, -1 );
scene.add( ambient );
var light = new THREE.DirectionalLight( 0xffffff, 0.75 );
light.position.set( 1, 0.75, 0.5 );
scene.add( light );
var renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( WIDTH, HEIGHT );
viewer.appendChild( renderer.domElement );
function animate() {
var time = performance.now() / 5000;
camera.position.x = Math.sin( time ) * 5;
camera.position.z = Math.cos( time ) * 5;
camera.lookAt( 0, 1.5, 0 );
renderer.render( scene, camera );
requestAnimationFrame( animate );
}
requestAnimationFrame( animate );
// POLY REST API
const API_KEY = '*** INSERT YOUR API KEY HERE ***';
function loadAsset( id ) {
var url = `https://poly.googleapis.com/v1/assets/${id}/?key=${API_KEY}`;
var request = new XMLHttpRequest();
request.open( 'GET', url, true );
request.addEventListener( 'load', function ( event ) {
var asset = JSON.parse( event.target.response );
asset_name.textContent = asset.displayName;
asset_author.textContent = asset.authorName;
var format = asset.formats.find( format => { return format.formatType === 'OBJ'; } );
if ( format !== undefined ) {
var obj = format.root;
var mtl = format.resources.find( resource => { return resource.url.endsWith( 'mtl' ) } );
var path = obj.url.slice( 0, obj.url.indexOf( obj.relativePath ) );
var loader = new THREE.MTLLoader();
loader.setCrossOrigin( true );
loader.setMaterialOptions( { ignoreZeroRGBs: true } );
loader.setTexturePath( path );
loader.load( mtl.url, function ( materials ) {
var loader = new THREE.OBJLoader();
loader.setMaterials( materials );
loader.load( obj.url, function ( object ) {
var box = new THREE.Box3();
box.setFromObject( object );
// re-center
var center = box.getCenter();
center.y = box.min.y;
object.position.sub( center );
// scale
var scaler = new THREE.Group();
scaler.add( object );
scaler.scale.setScalar( 6 / box.getSize().length() );
scene.add( scaler );
} );
} );
}
} );
request.send( null );
}
if ( API_KEY.startsWith( '**' ) ) {
alert( 'Sample incorrectly set up. Please enter your API Key for the Poly API in the API_KEY variable.' );
}
loadAsset( '5vbJ5vildOq' );
</script>
</body>
</html>