1
1
import {
2
2
createScopedThreejs
3
- } from './threejs-miniprogram'
4
- import {
3
+ } from './threejs-miniprogram'
4
+ import {
5
5
registerGLTFLoader
6
- } from '../loaders/gltf-loader'
7
-
8
- const info = wx . getSystemInfoSync ( )
9
-
10
- export default function getBehavior ( ) {
6
+ } from '../loaders/gltf-loader'
7
+
8
+ const info = wx . getSystemInfoSync ( )
9
+
10
+ export default function getBehavior ( ) {
11
11
return Behavior ( {
12
- data : {
13
- width : 1 ,
14
- height : 1 ,
15
- fps : 0 ,
16
- memory : 0 ,
17
- cpu : 0 ,
18
- cameraPosition : 0 ,
12
+ data : {
13
+ width : 1 ,
14
+ height : 1 ,
15
+ fps : 0 ,
16
+ memory : 0 ,
17
+ cpu : 0 ,
18
+ cameraPosition : 0 ,
19
+ } ,
20
+ methods : {
21
+ onReady ( ) {
22
+ wx . createSelectorQuery ( )
23
+ . select ( '#webgl' )
24
+ . node ( )
25
+ . exec ( res => {
26
+ this . canvas = res [ 0 ] . node
27
+
28
+ const info = wx . getSystemInfoSync ( )
29
+ const pixelRatio = info . pixelRatio
30
+ const calcSize = ( width , height ) => {
31
+ console . log ( `canvas size: width = ${ width } , height = ${ height } ` )
32
+ this . canvas . width = width * pixelRatio
33
+ this . canvas . height = height * pixelRatio
34
+ this . setData ( {
35
+ width,
36
+ height,
37
+ } )
38
+ }
39
+ calcSize ( info . windowWidth , info . windowHeight * 0.8 )
40
+
41
+ this . initVK ( )
42
+ } )
19
43
} ,
20
- methods : {
21
- onReady ( ) {
22
- wx . createSelectorQuery ( )
23
- . select ( '#webgl' )
24
- . node ( )
25
- . exec ( res => {
26
- this . canvas = res [ 0 ] . node
27
-
28
- const info = wx . getSystemInfoSync ( )
29
- const pixelRatio = info . pixelRatio
30
- const calcSize = ( width , height ) => {
31
- console . log ( `canvas size: width = ${ width } , height = ${ height } ` )
32
- this . canvas . width = width * pixelRatio
33
- this . canvas . height = height * pixelRatio
34
- this . setData ( {
35
- width,
36
- height,
37
- } )
38
- }
39
- calcSize ( info . windowWidth , info . windowHeight * 0.8 )
40
-
41
- this . initVK ( )
42
- } )
44
+ onUnload ( ) {
45
+ if ( this . _texture ) {
46
+ this . _texture . dispose ( )
47
+ this . _texture = null
48
+ }
49
+ if ( this . renderer ) {
50
+ this . renderer . dispose ( )
51
+ this . renderer = null
52
+ }
53
+ if ( this . scene ) {
54
+ this . scene . dispose ( )
55
+ this . scene = null
56
+ }
57
+ if ( this . camera ) this . camera = null
58
+ if ( this . model ) this . model = null
59
+ if ( this . _insertModel ) this . _insertModel = null
60
+ if ( this . _insertModels ) this . _insertModels = null
61
+ if ( this . planeBox ) this . planeBox = null
62
+ if ( this . mixers ) {
63
+ this . mixers . forEach ( mixer => mixer . uncacheRoot ( mixer . getRoot ( ) ) )
64
+ this . mixers = null
65
+ }
66
+ if ( this . clock ) this . clock = null
67
+
68
+ if ( this . THREE ) this . THREE = null
69
+ if ( this . _tempTexture && this . _tempTexture . gl ) {
70
+ this . _tempTexture . gl . deleteTexture ( this . _tempTexture )
71
+ this . _tempTexture = null
72
+ }
73
+ if ( this . _fb && this . _fb . gl ) {
74
+ this . _fb . gl . deleteFramebuffer ( this . _fb )
75
+ this . _fb = null
76
+ }
77
+ if ( this . _program && this . _program . gl ) {
78
+ this . _program . gl . deleteProgram ( this . _program )
79
+ this . _program = null
80
+ }
81
+ if ( this . canvas ) this . canvas = null
82
+ if ( this . gl ) this . gl = null
83
+ if ( this . session ) this . session = null
84
+ if ( this . anchor2DList ) this . anchor2DList = [ ]
85
+ } ,
86
+ initVK ( ) {
87
+ // 初始化 threejs
88
+ this . initTHREE ( )
89
+
90
+ // 自定义初始化
91
+ if ( this . init ) this . init ( )
92
+
93
+ console . log ( 'this.gl' , this . gl )
94
+
95
+ const session = this . session = wx . createVKSession ( {
96
+ track : {
97
+ plane : {
98
+ mode : 3
99
+ } ,
100
+ body : {
101
+ mode : 1
102
+ }
43
103
} ,
44
- onUnload ( ) {
45
- if ( this . _texture ) {
46
- this . _texture . dispose ( )
47
- this . _texture = null
48
- }
49
- if ( this . renderer ) {
50
- this . renderer . dispose ( )
51
- this . renderer = null
52
- }
53
- if ( this . scene ) {
54
- this . scene . dispose ( )
55
- this . scene = null
56
- }
57
- if ( this . camera ) this . camera = null
58
- if ( this . model ) this . model = null
59
- if ( this . _insertModel ) this . _insertModel = null
60
- if ( this . _insertModels ) this . _insertModels = null
61
- if ( this . planeBox ) this . planeBox = null
62
- if ( this . mixers ) {
63
- this . mixers . forEach ( mixer => mixer . uncacheRoot ( mixer . getRoot ( ) ) )
64
- this . mixers = null
65
- }
66
- if ( this . clock ) this . clock = null
67
-
68
- if ( this . THREE ) this . THREE = null
69
- if ( this . _tempTexture && this . _tempTexture . gl ) {
70
- this . _tempTexture . gl . deleteTexture ( this . _tempTexture )
71
- this . _tempTexture = null
72
- }
73
- if ( this . _fb && this . _fb . gl ) {
74
- this . _fb . gl . deleteFramebuffer ( this . _fb )
75
- this . _fb = null
104
+ gl : this . gl ,
105
+ version : 'v1' ,
106
+ } )
107
+ session . start ( err => {
108
+ if ( err ) return console . error ( 'VK error: ' , err )
109
+
110
+ console . log ( '@@@@@@@@ VKSession.version' , session . version )
111
+
112
+ const canvas = this . canvas
113
+
114
+ const calcSize = ( width , height , pixelRatio ) => {
115
+ console . log ( `canvas size: width = ${ width } , height = ${ height } ` )
116
+ this . canvas . width = width * pixelRatio
117
+ this . canvas . height = height * pixelRatio
118
+ this . setData ( {
119
+ width,
120
+ height,
121
+ } )
122
+ }
123
+
124
+ session . on ( 'resize' , ( ) => {
125
+ const info = wx . getSystemInfoSync ( )
126
+ calcSize ( info . windowWidth , info . windowHeight * 0.8 , info . pixelRatio )
127
+ } )
128
+
129
+ session . on ( 'addAnchors' , anchors => {
130
+ this . data . anchor2DList = anchors . map ( anchor => ( {
131
+ points : anchor . points ,
132
+ origin : anchor . origin ,
133
+ size : anchor . size
134
+ } ) )
135
+ } )
136
+ session . on ( 'updateAnchors' , anchors => {
137
+ this . data . anchor2DList = [ ]
138
+ this . data . anchor2DList = this . data . anchor2DList . concat ( anchors . map ( anchor => ( {
139
+ points : anchor . points ,
140
+ origin : anchor . origin ,
141
+ size : anchor . size
142
+ } ) ) )
143
+
144
+ console . log ( "显示data" )
145
+ console . log ( this . data )
146
+ } )
147
+ session . on ( 'removeAnchors' , anchors => {
148
+ this . data . anchor2DList = [ ]
149
+ } )
150
+
151
+
152
+ //限制调用帧率
153
+ let fps = 30
154
+ let fpsInterval = 1000 / fps
155
+ let last = Date . now ( )
156
+
157
+ // 逐帧渲染
158
+ const onFrame = timestamp => {
159
+ let now = Date . now ( )
160
+ const mill = now - last
161
+ // 经过了足够的时间
162
+ if ( mill > fpsInterval ) {
163
+ last = now - ( mill % fpsInterval ) ; //校正当前时间
164
+ const frame = session . getVKFrame ( canvas . width , canvas . height )
165
+ if ( frame ) {
166
+ this . render ( frame )
76
167
}
77
- if ( this . _program && this . _program . gl ) {
78
- this . _program . gl . deleteProgram ( this . _program )
79
- this . _program = null
80
- }
81
- if ( this . canvas ) this . canvas = null
82
- if ( this . gl ) this . gl = null
83
- if ( this . session ) this . session = null
84
- if ( this . anchor2DList ) this . anchor2DList = [ ]
85
- } ,
86
- initVK ( ) {
87
- // 初始化 threejs
88
- this . initTHREE ( )
89
-
90
- // 自定义初始化
91
- if ( this . init ) this . init ( )
92
-
93
- console . log ( 'this.gl' , this . gl )
94
-
95
- const session = this . session = wx . createVKSession ( {
96
- track : { plane : { mode : 3 } , body : { mode : 1 } } , version : 'v1' ,
97
- } )
98
- session . start ( err => {
99
- if ( err ) return console . error ( 'VK error: ' , err )
100
-
101
- console . log ( '@@@@@@@@ VKSession.version' , session . version )
102
-
103
- const canvas = this . canvas
104
-
105
- const calcSize = ( width , height , pixelRatio ) => {
106
- console . log ( `canvas size: width = ${ width } , height = ${ height } ` )
107
- this . canvas . width = width * pixelRatio
108
- this . canvas . height = height * pixelRatio
109
- this . setData ( {
110
- width,
111
- height,
112
- } )
113
- }
114
-
115
- session . on ( 'resize' , ( ) => {
116
- const info = wx . getSystemInfoSync ( )
117
- calcSize ( info . windowWidth , info . windowHeight * 0.8 , info . pixelRatio )
118
- } )
119
-
120
- session . on ( 'addAnchors' , anchors => {
121
- this . data . anchor2DList = anchors . map ( anchor => ( {
122
- points : anchor . points ,
123
- origin : anchor . origin ,
124
- size : anchor . size
125
- } ) )
126
- } )
127
- session . on ( 'updateAnchors' , anchors => {
128
- this . data . anchor2DList = [ ]
129
- this . data . anchor2DList = this . data . anchor2DList . concat ( anchors . map ( anchor => ( {
130
- points : anchor . points ,
131
- origin : anchor . origin ,
132
- size : anchor . size
133
- } ) ) )
134
-
135
- console . log ( "显示data" )
136
- console . log ( this . data )
137
- } )
138
- session . on ( 'removeAnchors' , anchors => {
139
- this . data . anchor2DList = [ ]
140
- } )
141
-
142
-
143
- //限制调用帧率
144
- let fps = 30
145
- let fpsInterval = 1000 / fps
146
- let last = Date . now ( )
147
-
148
- // 逐帧渲染
149
- const onFrame = timestamp => {
150
- let now = Date . now ( )
151
- const mill = now - last
152
- // 经过了足够的时间
153
- if ( mill > fpsInterval ) {
154
- last = now - ( mill % fpsInterval ) ; //校正当前时间
155
- const frame = session . getVKFrame ( canvas . width , canvas . height )
156
- if ( frame ) {
157
- this . render ( frame )
158
- }
159
- }
160
- session . requestAnimationFrame ( onFrame )
161
- }
162
- session . requestAnimationFrame ( onFrame )
163
- } )
164
- } ,
165
- initTHREE ( ) {
166
- const THREE = this . THREE = createScopedThreejs ( this . canvas )
167
- registerGLTFLoader ( THREE )
168
-
169
- // 相机
170
- this . camera = new THREE . Camera ( )
171
-
172
- // 场景
173
- const scene = this . scene = new THREE . Scene ( )
174
-
175
- // 光源
176
- const light1 = new THREE . HemisphereLight ( 0xffffff , 0x444444 ) // 半球光
177
- light1 . position . set ( 0 , 0.2 , 0 )
178
- scene . add ( light1 )
179
- const light2 = new THREE . DirectionalLight ( 0xffffff ) // 平行光
180
- light2 . position . set ( 0 , 0.2 , 0.1 )
181
- scene . add ( light2 )
182
-
183
- // 渲染层
184
- const renderer = this . renderer = new THREE . WebGLRenderer ( {
185
- antialias : true ,
186
- alpha : true
187
- } )
188
- renderer . gammaOutput = true
189
- renderer . gammaFactor = 2.2
190
- } ,
168
+ }
169
+ session . requestAnimationFrame ( onFrame )
170
+ }
171
+ session . requestAnimationFrame ( onFrame )
172
+ } )
173
+ } ,
174
+ initTHREE ( ) {
175
+ const THREE = this . THREE = createScopedThreejs ( this . canvas )
176
+ registerGLTFLoader ( THREE )
177
+
178
+ // 相机
179
+ this . camera = new THREE . Camera ( )
180
+
181
+ // 场景
182
+ const scene = this . scene = new THREE . Scene ( )
183
+
184
+ // 光源
185
+ const light1 = new THREE . HemisphereLight ( 0xffffff , 0x444444 ) // 半球光
186
+ light1 . position . set ( 0 , 0.2 , 0 )
187
+ scene . add ( light1 )
188
+ const light2 = new THREE . DirectionalLight ( 0xffffff ) // 平行光
189
+ light2 . position . set ( 0 , 0.2 , 0.1 )
190
+ scene . add ( light2 )
191
+
192
+ // 渲染层
193
+ const renderer = this . renderer = new THREE . WebGLRenderer ( {
194
+ antialias : true ,
195
+ alpha : true
196
+ } )
197
+ renderer . gammaOutput = true
198
+ renderer . gammaFactor = 2.2
191
199
} ,
200
+ } ,
192
201
} )
193
- }
202
+ }
0 commit comments