/
index.html
227 lines (183 loc) · 8.79 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta charset="utf-8">
<title>EnchantAR</title>
<meta name="keywords" content="ar, enchanter, arjs, threejs, augmented reality, image tracking, nft, natural feature tracking, 3d model, animated model, animation" />
<meta name="description" content="Enchant images to life!" />
<!-- Favicon -->
<link href="assets/images/favicon.png" rel="shortcut icon">
<!-- Custom Styles -->
<link href="assets/css/styles.css" rel="stylesheet">
<body>
<div class="arjs-loader">
<div class="arjs-loader-spinner"></div>
</div>
<!--
Material fab pure CSS
https://codepen.io/ruslan_khomiak/pen/QGmwMP
-->
<div class="fab">
<span class="fab-action-button">
<i class="fab-icon"></i>
</span>
<ul class="fab-buttons">
<li class="fab-buttons-item">
<a class="fab-buttons-link" target="_blank" href="https://github.com/HEXcube/EnchantAR/blob/master/README.md" data-tooltip="How to use">
<i class="icon-material icon-material-info"></i>
</a>
</li>
<li class="fab-buttons-item">
<a class="fab-buttons-link" target="_blank" href="https://github.com/HEXcube/EnchantAR/raw/master/assets/markers/phoenix-of-autumn.jpg" data-tooltip="Open marker image">
<i class="icon-material icon-material-image"></i>
</a>
</li>
<li class="fab-buttons-item">
<a class="fab-buttons-link" target="_blank" href="https://github.com/HEXcube/EnchantAR" data-tooltip="View source code">
<i class="icon-material icon-material-source"></i>
</a>
</li>
<li class="fab-buttons-item">
<a class="fab-buttons-link" target="_blank" href="https://hexcube.github.io" data-tooltip="About me">
<i class="icon-material icon-material-about"></i>
</a>
</li>
</ul>
</div>
<!-- three.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r119/three.min.js" integrity="sha512-E1+SnTWyHYoY5MN8wpL0SI4TYbpWN12sHXyEViwbzzEyq6GHxQrPFDzEeUDab4emXcf3Yj41Tht2JZnpo3kjwA==" crossorigin="anonymous"></script>
<!-- three.js load GLTF -->
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r119/examples/js/loaders/GLTFLoader.js" integrity="sha384-/PZ48PhdIRvZ+UTt2RqFnNIKZe88fy+KztzdmJ4RQl05/HHGRRsjlDIFTNVDeGyC" crossorigin="anonymous"></script>
<!-- ar.js -->
<script src="https://cdn.jsdelivr.net/gh/AR-js-org/AR.js@3.2.1/three.js/build/ar-nft.js" integrity="sha384-BpUrCkowTHVfmA7JvAFs+ZumBGQv7JM2WNxuKyMsotw5i9JLE0VHYAZtOVM7Cba7" crossorigin="anonymous"></script>
<!-- <script>THREEx.ArToolkitContext.baseURL = '../'</script> -->
<script>THREEx.ArToolkitContext.baseURL = 'https://cdn.jsdelivr.net/gh/AR-js-org/AR.js@3.2.1/data/'</script>
<script>
//////////////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////////////
var renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
precision: 'mediump',
});
var clock = new THREE.Clock();
var mixers = [];
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(new THREE.Color('lightgrey'), 0)
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.domElement.style.position = 'absolute'
renderer.domElement.style.top = '0px'
renderer.domElement.style.left = '0px'
document.body.appendChild( renderer.domElement );
// init scene and camera
var scene = new THREE.Scene();
//////////////////////////////////////////////////////////////////////////////////
// Initialize a basic camera
//////////////////////////////////////////////////////////////////////////////////
// Create a camera
var camera = new THREE.Camera();
scene.add(camera);
var light = new THREE.AmbientLight(0xffffff);
scene.add(light);
////////////////////////////////////////////////////////////////////////////////
// handle arToolkitSource
////////////////////////////////////////////////////////////////////////////////
var arToolkitSource = new THREEx.ArToolkitSource({
sourceType : 'webcam',
sourceWidth: 480,
sourceHeight: 640,
})
arToolkitSource.init(function onReady(){
// use a resize to fullscreen mobile devices
setTimeout(function() {
onResize()
}, 1000);
})
// handle resize
window.addEventListener('resize', function(){
onResize()
})
// listener for end loading of NFT marker
window.addEventListener('arjs-nft-loaded', function(ev){
console.log(ev);
})
function onResize(){
arToolkitSource.onResizeElement()
arToolkitSource.copyElementSizeTo(renderer.domElement)
if( arToolkitContext.arController !== null ){
arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas)
}
}
////////////////////////////////////////////////////////////////////////////////
// initialize arToolkitContext
////////////////////////////////////////////////////////////////////////////////
// create atToolkitContext
var arToolkitContext = new THREEx.ArToolkitContext({
detectionMode: 'mono',
canvasWidth: 480,
canvasHeight: 640,
}, {
sourceWidth: 480,
sourceHeight: 640,
})
// initialize it
arToolkitContext.init(function onCompleted(){
// copy projection matrix to camera
camera.projectionMatrix.copy( arToolkitContext.getProjectionMatrix() );
})
////////////////////////////////////////////////////////////////////////////////
// Create a ArMarkerControls
////////////////////////////////////////////////////////////////////////////////
// init controls for camera
var markerControls = new THREEx.ArMarkerControls(arToolkitContext, camera, {
type : 'nft',
// https://ar-js-org.github.io/AR.js-Docs/image-tracking/
// descriptorsUrl : './assets/markers/phoenix-of-autumn',
descriptorsUrl : 'https://cdn.jsdelivr.net/gh/HEXcube/EnchantAR/assets/markers/phoenix-of-autumn',
changeMatrixMode: 'cameraTransformMatrix'
})
scene.visible = false
var root = new THREE.Object3D();
scene.add(root);
//////////////////////////////////////////////////////////////////////////////////
// add an object in the scene
//////////////////////////////////////////////////////////////////////////////////
var threeGLTFLoader = new THREE.GLTFLoader();
var model;
// threeGLTFLoader.load("https://cdn.jsdelivr.net/gh/AR-js-org/AR.js@3.2.1/three.js/examples/resources/Flamingo.glb", function (gltf) {
threeGLTFLoader.load("assets/models/phoenix-bird.glb", function (gltf) {
model = gltf.scene.children[0];
model.name = 'Phoenix Bird';
var animation = gltf.animations[0];
var mixer = new THREE.AnimationMixer(model);
mixers.push(mixer);
var action = mixer.clipAction(animation);
action.play();
root.matrixAutoUpdate = false;
root.add(model);
model.position.z = -200;
model.position.x = 100;
model.position.y = 100;
//////////////////////////////////////////////////////////////////////////////////
// render the whole thing on the page
//////////////////////////////////////////////////////////////////////////////////
var animate = function() {
requestAnimationFrame(animate);
if (mixers.length > 0) {
for (var i = 0; i < mixers.length; i++) {
mixers[i].update(clock.getDelta());
}
}
if (!arToolkitSource.ready) {
return;
}
arToolkitContext.update( arToolkitSource.domElement )
// update scene.visible if the marker is seen
scene.visible = camera.visible;
renderer.render(scene, camera);
};
requestAnimationFrame(animate);
}
);
</script>
</body>