-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathbehavior.js
408 lines (371 loc) · 12.7 KB
/
behavior.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import {
createScopedThreejs
} from 'threejs-miniprogram'
import {
registerGLTFLoader
} from '../loaders/gltf-loader'
import cloneGltf from '../loaders/gltf-clone'
const info = wx.getSystemInfoSync()
export default function getBehavior() {
return Behavior({
data: {
width: 1,
height: 1,
fps: 0,
memory: 0,
cpu: 0,
},
methods: {
onReady() {
wx.createSelectorQuery()
.select('#webgl')
.node()
.exec(res => {
this.canvas = res[0].node
const info = wx.getSystemInfoSync()
const pixelRatio = info.pixelRatio
const calcSize = (width, height) => {
console.log(`canvas size: width = ${width} , height = ${height}`)
this.canvas.width = width * pixelRatio
this.canvas.height = height * pixelRatio
this.setData({
width,
height,
})
}
calcSize(info.windowWidth, info.windowHeight * 0.8)
this.initVK()
})
},
onUnload() {
if (this._texture) {
this._texture.dispose()
this._texture = null
}
if (this.renderer) {
this.renderer.dispose()
this.renderer = null
}
if (this.scene) {
this.scene.dispose()
this.scene = null
}
if (this.camera) this.camera = null
if (this.model) this.model = null
if (this._insertModel) this._insertModel = null
if (this._insertModels) this._insertModels = null
if (this.planeBox) this.planeBox = null
if (this.mixers) {
this.mixers.forEach(mixer => mixer.uncacheRoot(mixer.getRoot()))
this.mixers = null
}
if (this.clock) this.clock = null
if (this.THREE) this.THREE = null
if (this._tempTexture && this._tempTexture.gl) {
this._tempTexture.gl.deleteTexture(this._tempTexture)
this._tempTexture = null
}
if (this._fb && this._fb.gl) {
this._fb.gl.deleteFramebuffer(this._fb)
this._fb = null
}
if (this._program && this._program.gl) {
this._program.gl.deleteProgram(this._program)
this._program = null
}
if (this.canvas) this.canvas = null
if (this.gl) this.gl = null
if (this.session) this.session = null
if (this.anchor2DList) this.anchor2DList = []
},
initVK() {
// 初始化 threejs
this.initTHREE()
const THREE = this.THREE
// 自定义初始化
if (this.init) this.init()
console.log('this.gl', this.gl)
this.session = wx.createVKSession({
track: {
plane: {
mode: 1
},
},
version: 'v2',
gl: this.gl,
})
const session = this.session
session.start(err => {
if (err) {
this.setData({
errMsg:'VK error: ' + err
})
return console.error('VK error: ', err)
}
console.log('@@@@@@@@ VKSession.version', session.version)
const canvas = this.canvas
const calcSize = (width, height, pixelRatio) => {
console.log(`canvas size: width = ${width} , height = ${height}`)
this.canvas.width = width * pixelRatio
this.canvas.height = height * pixelRatio
this.setData({
width,
height,
})
}
session.on('resize', () => {
const info = wx.getSystemInfoSync()
calcSize(info.windowWidth, info.windowHeight * 0.8, info.pixelRatio)
})
const loader = new THREE.GLTFLoader()
loader.load('https://dldir1.qq.com/weixin/miniprogram/RobotExpressive_aa2603d917384b68bb4a086f32dabe83.glb', gltf => {
this.model = {
scene: gltf.scene,
animations: gltf.animations,
}
})
this.clock = new THREE.Clock()
loader.load('https://dldir1.qq.com/weixin/miniprogram/reticle_4b6cc19698ca4a08b31fd3c95ce412ec.glb', gltf => {
const reticle = this.reticle = gltf.scene
reticle.visible = false
this.scene.add(reticle)
})
this.setData({
showTip: true
})
setTimeout(()=>{
this.setData({
showTip: false
})
}, 10000)
// anchor 检测
const createPlane = size => {
const geometry = new THREE.PlaneGeometry(size.width, size.height)
const material = new THREE.MeshBasicMaterial({
color: 0xffffff,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.5
})
const mesh = new THREE.Mesh(geometry, material)
mesh.rotateX(Math.PI / 2)
const cnt = new THREE.Object3D()
cnt.add(mesh)
return cnt
}
const updateMatrix = (object, m) => {
object.matrixAutoUpdate = false
object.matrix.fromArray(m)
}
session.on('addAnchors', anchors => {
anchors.forEach(anchor => {
const size = anchor.size
let object
if (anchor.type == 0) {
object = createPlane(size)
this.setData({
showTip: false
})
} else {
if (!this.model) {
console.warn('this.model 还没加载完成 !!!!!')
return
}
object = new THREE.Object3D()
const model = this.getRobot()
model.rotateX(-Math.PI / 2)
object.add(model)
}
object._id = anchor.id
object._size = size
updateMatrix(object, anchor.transform)
this.planeBox.add(object)
})
})
session.on('updateAnchors', anchors => {
const map = anchors.reduce((temp, item) => {
temp[item.id] = item
return temp
}, {})
const deletePlaneBoxs = [];
const newPlaneBoxs = [];
this.planeBox.children.forEach(object => {
if (object._id && map[object._id]) {
const anchor = map[object._id]
const size = anchor.size
if (size && object._size && (size.width !== object._size.width || size.height !== object._size.height)) {
// this.planeBox.remove(object)
// object = createPlane(size)
// this.planeBox.add(object)
// 塞入删除队列
deletePlaneBoxs.push(object);
const newPlane = createPlane(size);
newPlane._id = anchor.id
newPlane._size = size
updateMatrix(newPlane, anchor.transform)
// 塞入添加队列
newPlaneBoxs.push(newPlane);
} else {
object._id = anchor.id
object._size = size
updateMatrix(object, anchor.transform)
}
}
})
// 延后删除
for(let i = 0; i < deletePlaneBoxs.length; i++) {
this.planeBox.remove(deletePlaneBoxs[i])
}
// 延后添加
for(let i = 0; i < newPlaneBoxs.length; i++) {
this.planeBox.add(newPlaneBoxs[i]);
}
})
session.on('removeAnchors', anchors => {
const map = anchors.reduce((temp, item) => {
temp[item.id] = item
return temp
}, {})
const deletePlaneBoxs = [];
this.planeBox.children.forEach(object => {
if (object._id && map[object._id]) {
// this.planeBox.remove(object)
// 塞入删除队列
deletePlaneBoxs.push(object);
}
})
// 延后删除
for(let i = 0; i < deletePlaneBoxs.length; i++) {
this.planeBox.remove(deletePlaneBoxs[i])
}
})
// 平面集合
const planeBox = this.planeBox = new THREE.Object3D()
this.scene.add(planeBox)
//限制调用帧率
let fps = 30
let fpsInterval = 1000 / fps
let last = Date.now()
// 逐帧渲染
const onFrame = timestamp => {
let now = Date.now()
const mill = now - last
// 经过了足够的时间
if (mill > fpsInterval) {
last = now - (mill % fpsInterval); //校正当前时间
const frame = session.getVKFrame(canvas.width, canvas.height)
if (frame) {
this.render(frame)
}
}
session.requestAnimationFrame(onFrame)
}
session.requestAnimationFrame(onFrame)
})
},
initTHREE() {
const THREE = this.THREE = createScopedThreejs(this.canvas)
registerGLTFLoader(THREE)
// 相机
this.camera = new THREE.Camera()
// 场景
const scene = this.scene = new THREE.Scene()
// 光源
const light1 = new THREE.HemisphereLight(0xffffff, 0x444444) // 半球光
light1.position.set(0, 0.2, 0)
scene.add(light1)
const light2 = new THREE.DirectionalLight(0xffffff) // 平行光
light2.position.set(0, 0.2, 0.1)
scene.add(light2)
// 渲染层
const renderer = this.renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
})
renderer.gammaOutput = true
renderer.gammaFactor = 2.2
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const scale = 0.1;
const m1 = new THREE.MeshPhysicalMaterial( {
metalness: 0.0,
roughness: 0.1,
color: 0xff0000,
} );
const x = new THREE.Mesh( geometry, m1 );
x.position.set(1.5 * scale, 0, 0 );
x.scale.set(3 * scale, 0.1 * scale, 0.1 * scale);
scene.add( x );
const m2 = new THREE.MeshPhysicalMaterial( {
metalness: 0.0,
roughness: 0.1,
color: 0x00ff00,
} );
const y = new THREE.Mesh( geometry, m2 );
y.position.set(0, 1.5 * scale, 0 );
y.scale.set(0.1 * scale, 3 * scale, 0.1 * scale);
scene.add( y );
const m3 = new THREE.MeshPhysicalMaterial( {
metalness: 0.0,
roughness: 0.1,
color: 0x0000ff,
} );
const z = new THREE.Mesh( geometry, m3 );
z.position.set(0, 0, 1.5 * scale );
z.scale.set(0.1 * scale, 0.1 * scale, 3 * scale);
scene.add( z );
},
updateAnimation() {
const dt = this.clock.getDelta()
if (this.mixers) this.mixers.forEach(mixer => mixer.update(dt))
},
copyRobot() {
const THREE = this.THREE
const {
scene,
animations
} = cloneGltf(this.model, THREE)
scene.scale.set(0.05, 0.05, 0.05)
// 动画混合器
const mixer = new THREE.AnimationMixer(scene)
for (let i = 0; i < animations.length; i++) {
const clip = animations[i]
if (clip.name === 'Dance') {
const action = mixer.clipAction(clip)
action.play()
}
}
this.mixers = this.mixers || []
this.mixers.push(mixer)
scene._mixer = mixer
return scene
},
getRobot() {
const THREE = this.THREE
const model = new THREE.Object3D()
model.add(this.copyRobot())
this._insertModels = this._insertModels || []
this._insertModels.push(model)
if (this._insertModels.length > 5) {
const needRemove = this._insertModels.splice(0, this._insertModels.length - 5)
needRemove.forEach(item => {
if (item._mixer) {
const mixer = item._mixer
this.mixers.splice(this.mixers.indexOf(mixer), 1)
mixer.uncacheRoot(mixer.getRoot())
}
if (item.parent) item.parent.remove(item)
})
}
return model
},
onTouchEnd(evt) {
if (this.scene && this.model && this.reticle) {
const model = this.getRobot()
model.position.copy(this.reticle.position)
model.rotation.copy(this.reticle.rotation)
this.scene.add(model)
}
}
},
})
}