-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathbehavior.js
253 lines (224 loc) · 9.4 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
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)
const session = this.session = wx.createVKSession({
track: {
plane: {
mode: 3
},
},
version: 'v1',
gl: this.gl
})
session.start(err => {
if (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()
//限制调用帧率
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
},
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) {
// 点击位置放一个机器人
const touches = evt.changedTouches.length ? evt.changedTouches : evt.touches
if (touches.length === 1) {
const touch = touches[0]
if (this.session && this.scene && this.model) {
const hitTestRes = this.session.hitTest(touch.x / this.data.width, touch.y / this.data.height, this.resetPanel)
this.resetPanel = false
if (hitTestRes.length) {
const model = this.getRobot()
model.matrixAutoUpdate = false
model.matrix.fromArray(hitTestRes[0].transform)
this.scene.add(model)
}
}
}
}
},
})
}