-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
ARnft.js
181 lines (160 loc) · 5.08 KB
/
ARnft.js
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
import Utils from './utils/Utils'
import Container from './utils/html/Container'
import Stats from 'stats.js'
import ThreejsRenderer from './renderers/ThreejsRenderer'
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
export default class ARnft {
constructor (width, height, config) {
this.width = width
this.height = height
this.root = new THREE.Object3D()
this.root.matrixAutoUpdate = false
this.config = config
this.listeners = {}
this.version = '0.8.0'
console.log('ARnft ', this.version)
}
_initialize (markerUrl, stats) {
console.log('ARnft init() %cstart...', 'color: yellow; background-color: blue; border-radius: 4px; padding: 2px')
const root = this.root
const config = this.config
const data = Utils.jsonParser(config)
data.then((configData) => {
Container.createLoading(configData)
Container.createStats(stats)
const containerObj = Container.createContainer()
const container = containerObj.container
const canvas = containerObj.canvas
let statsMain, statsWorker
if (stats) {
statsMain = new Stats()
statsMain.showPanel(0) // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById('stats1').appendChild(statsMain.dom)
statsWorker = new Stats()
statsWorker.showPanel(0) // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById('stats2').appendChild(statsWorker.dom)
}
const statsObj = {
statsMain: statsMain,
statsWorker: statsWorker,
stats: stats
}
Utils.getUserMedia(configData).then((video) => {
Utils._startWorker(
container,
markerUrl,
video,
video.videoWidth,
video.videoHeight,
canvas,
() => {
if (statsObj.stats) {
statsObj.statsMain.update()
}
},
() => {
if (statsObj.stats) {
statsObj.statsWorker.update()
}
},
configData)
})
if (configData.renderer.type === 'three') {
const renderer = new ThreejsRenderer(configData, canvas, root)
renderer.initRenderer()
const tick = () => {
renderer.draw()
window.requestAnimationFrame(tick)
}
tick()
}
})
return this
}
static async init (width, height, markerUrl, config, stats) {
const nft = new ARnft(width, height, config)
return await nft._initialize(markerUrl, stats)
}
add (obj) {
const root = this.root
document.addEventListener('getNFTData', (ev) => {
var msg = ev.detail
obj.position.y = (msg.height / msg.dpi * 2.54 * 10) / 2.0
obj.position.x = (msg.width / msg.dpi * 2.54 * 10) / 2.0
})
root.add(obj)
}
addModel (url, x, y, z, scale) {
const root = this.root
let model
/* Load Model */
const threeGLTFLoader = new GLTFLoader()
threeGLTFLoader.load(url, gltf => {
model = gltf.scene
model.scale.set(scale, scale, scale)
model.rotation.x = Math.PI / 2
model.position.x = x
model.position.y = y
model.position.z = z
root.add(model)
})
}
addImage (url, color, scale) {
const root = this.root
const texture = new THREE.TextureLoader().load(url)
const mat = new THREE.MeshLambertMaterial({ color: color, map: texture })
const planeGeom = new THREE.PlaneGeometry(1, 1, 1, 1)
const plane = new THREE.Mesh(planeGeom, mat)
plane.scale.set(scale, scale, scale)
document.addEventListener('getNFTData', (ev) => {
var msg = ev.detail
plane.position.y = (msg.height / msg.dpi * 2.54 * 10) / 2.0
plane.position.x = (msg.width / msg.dpi * 2.54 * 10) / 2.0
})
root.add(plane)
}
addVideo (id, scale) {
const root = this.root
var ARVideo = document.getElementById(id)
var texture = new THREE.VideoTexture(ARVideo)
var mat = new THREE.MeshLambertMaterial({ color: 0xbbbbff, map: texture })
ARVideo.play()
var planeGeom = new THREE.PlaneGeometry(1, 1, 1, 1)
var plane = new THREE.Mesh(planeGeom, mat)
plane.scale.set(scale, scale, scale)
document.addEventListener('getNFTData', (ev) => {
var msg = ev.detail
plane.position.y = (msg.height / msg.dpi * 2.54 * 10) / 2.0
plane.position.x = (msg.width / msg.dpi * 2.54 * 10) / 2.0
})
root.add(plane)
}
dispatchEvent (event) {
const listeners = this.listeners[event.name]
if (listeners) {
for (let i = 0; i < listeners.length; i++) {
listeners[i].call(this, event)
}
}
}
addEventListener (name, callback) {
if (!this.listeners[name]) {
this.listeners[name] = []
}
this.listeners[name].push(callback)
};
removeEventListener (name, callback) {
if (this.listeners[name]) {
const index = this.listeners[name].indexOf(callback)
if (index > -1) {
this.listeners[name].splice(index, 1)
}
}
};
_teardownVideo (video) {
video.srcObject.getVideoTracks()[0].stop()
video.srcObject = null
video.src = null
};
}