-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathbehavior.js
184 lines (166 loc) · 5.56 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
import {
createScopedThreejs
} from 'threejs-miniprogram'
import {
registerGLTFLoader
} from '../loaders/gltf-loader'
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()
// 自定义初始化
if (this.init) this.init()
console.log('this.gl', this.gl)
const session = this.session = wx.createVKSession({
track: {
depth: {
mode: 2
}
},
cameraPosition: 0,
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)
})
session.on('addAnchors', anchors => {
})
session.on('updateAnchors', anchors => {
let depthArray = []
// 手动传入图像的时候用dom画点和框就行
let anchor2DList = anchors.map(anchor => {
return {
value: anchor.depthArray,
size: anchor.size
}})
wx.hideLoading()
if (anchor2DList.length > 0) {
let width = 80
let height = 80
anchor2DList.forEach(anchor => {
width = anchor.size[0]
height = anchor.size[1]
anchor.value.forEach(item =>{
depthArray.push(item.value)
})
this.renderDepthGL(depthArray, width, height)
})
}
}
)
session.on('removeAnchors', anchors => {
})
})
},
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
},
},
})
}