-
zhangwei
2024-08-22 d5eb1ecff5d0236359c744b334cf30fcfdad1132
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import { createScopedThreejs } from 'threejs-miniprogram'
import { registerGLTFLoader } from '../loaders/gltf-loader'
 
const threeBehavior = Behavior({
    methods: {
        // 针对 threejs 的初始化逻辑
        initTHREE() {
            const THREE = this.THREE = createScopedThreejs(this.canvas)
            registerGLTFLoader(THREE)
 
            // glTF loader
            this.loader = new this.THREE.GLTFLoader()
 
            // 相机
            this.camera = new THREE.PerspectiveCamera(50, 0.7, 0.1, 1000);
 
            // 场景
            const scene = this.scene = new THREE.Scene()
            const sceneCull = this.sceneCull = new THREE.Scene()
 
            // 光源
            const ambientLight = new THREE.AmbientLight( 0x555555 ); // 氛围光
            scene.add( ambientLight );
            const dirLight = new THREE.DirectionalLight(0xffffff, 1) // 平行光
            dirLight.position.set(1, 1, 1);
            scene.add(dirLight)
 
            const ambientLightCull = new THREE.AmbientLight( 0x555555 ); // 氛围光
            sceneCull.add( ambientLightCull );
            const dirLightCull = new THREE.DirectionalLight(0xffffff, 1) // 平行光
            dirLightCull.position.set(1, 1, 1);
            sceneCull.add(dirLightCull)
 
            // 渲染层
            const renderer = this.renderer = new THREE.WebGLRenderer({
                antialias: true,
                alpha: true
            })
            renderer.gammaOutput = true
            renderer.gammaFactor = 2.2
        },
        initYUVShader() {
            const gl = this.gl = this.renderer.getContext()
            const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
            const vs = `
                attribute vec2 a_position;
                attribute vec2 a_texCoord;
                uniform mat3 displayTransform;
                varying vec2 v_texCoord;
                void main() {
                vec3 p = displayTransform * vec3(a_position, 0);
                gl_Position = vec4(p, 1);
                v_texCoord = a_texCoord;
                }
            `
            const fs = `
                precision highp float;
 
                uniform sampler2D y_texture;
                uniform sampler2D uv_texture;
                varying vec2 v_texCoord;
                void main() {
                vec4 y_color = texture2D(y_texture, v_texCoord);
                vec4 uv_color = texture2D(uv_texture, v_texCoord);
 
                float Y, U, V;
                float R ,G, B;
                Y = y_color.r;
                U = uv_color.r - 0.5;
                V = uv_color.a - 0.5;
                
                R = Y + 1.402 * V;
                G = Y - 0.344 * U - 0.714 * V;
                B = Y + 1.772 * U;
                
                gl_FragColor = vec4(R, G, B, 1.0);
                }
            `
            const vertShader = gl.createShader(gl.VERTEX_SHADER)
            gl.shaderSource(vertShader, vs)
            gl.compileShader(vertShader)
 
            const fragShader = gl.createShader(gl.FRAGMENT_SHADER)
            gl.shaderSource(fragShader, fs)
            gl.compileShader(fragShader)
 
            const program = this._program = gl.createProgram()
            this._program.gl = gl
            gl.attachShader(program, vertShader)
            gl.attachShader(program, fragShader)
            gl.deleteShader(vertShader)
            gl.deleteShader(fragShader)
            gl.linkProgram(program)
            gl.useProgram(program)
 
            const uniformYTexture = gl.getUniformLocation(program, 'y_texture')
            gl.uniform1i(uniformYTexture, 5)
            const uniformUVTexture = gl.getUniformLocation(program, 'uv_texture')
            gl.uniform1i(uniformUVTexture, 6)
 
            this._dt = gl.getUniformLocation(program, 'displayTransform')
            gl.useProgram(currentProgram)
        },
        initVAO(program) {
            const gl = this.renderer.getContext()
            const ext = gl.getExtension('OES_vertex_array_object')
            this.ext = ext
 
            const currentVAO = gl.getParameter(gl.VERTEX_ARRAY_BINDING)
            const vao = ext.createVertexArrayOES()
 
            ext.bindVertexArrayOES(vao)
 
            const posAttr = gl.getAttribLocation(program, 'a_position')
            const pos = gl.createBuffer()
            gl.bindBuffer(gl.ARRAY_BUFFER, pos)
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([1, 1, -1, 1, 1, -1, -1, -1]), gl.STATIC_DRAW)
            gl.vertexAttribPointer(posAttr, 2, gl.FLOAT, false, 0, 0)
            gl.enableVertexAttribArray(posAttr)
            vao.posBuffer = pos
 
            const texcoordAttr = gl.getAttribLocation(program, 'a_texCoord')
            const texcoord = gl.createBuffer()
            gl.bindBuffer(gl.ARRAY_BUFFER, texcoord)
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([1, 1, 0, 1, 1, 0, 0, 0]), gl.STATIC_DRAW)
            gl.vertexAttribPointer(texcoordAttr, 2, gl.FLOAT, false, 0, 0)
            gl.enableVertexAttribArray(texcoordAttr)
            vao.texcoordBuffer = texcoord
 
            ext.bindVertexArrayOES(currentVAO)
            return vao;
        },
        initYUV() {
            this.initYUVShader()
            this._vao = this.initVAO(this._program);
        },
        renderYUV(frame) {
            const gl = this.renderer.getContext()
            gl.disable(gl.DEPTH_TEST)
            const {
                yTexture,
                uvTexture
            } = frame.getCameraTexture(gl, 'yuv')
            const displayTransform = frame.getDisplayTransform()
            if (yTexture && uvTexture) {
                const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
                const currentActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE)
                const currentVAO = gl.getParameter(gl.VERTEX_ARRAY_BINDING)
 
                gl.useProgram(this._program)
                this.ext.bindVertexArrayOES(this._vao)
 
                gl.uniformMatrix3fv(this._dt, false, displayTransform)
                gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1)
 
                gl.activeTexture(gl.TEXTURE0 + 5)
                const bindingTexture5 = gl.getParameter(gl.TEXTURE_BINDING_2D)
                gl.bindTexture(gl.TEXTURE_2D, yTexture)
 
                gl.activeTexture(gl.TEXTURE0 + 6)
                const bindingTexture6 = gl.getParameter(gl.TEXTURE_BINDING_2D)
                gl.bindTexture(gl.TEXTURE_2D, uvTexture)
 
                gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
 
                gl.bindTexture(gl.TEXTURE_2D, bindingTexture6)
                gl.activeTexture(gl.TEXTURE0 + 5)
                gl.bindTexture(gl.TEXTURE_2D, bindingTexture5)
 
                gl.useProgram(currentProgram)
                gl.activeTexture(currentActiveTexture)
                this.ext.bindVertexArrayOES(currentVAO)
            }
        },
        initDepthShaderHint() {
            const gl = this.gl = this.renderer.getContext()
            const ext = gl.getExtension("OES_texture_float");
            if (!ext)
              console.warn('OES_texture_float not support');
            const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
            const vs = `
              precision highp float;
              attribute vec2 a_position;
              attribute vec2 a_texCoord;
              uniform mat3 displayTransform;
              varying vec2 v_texCoord;
              void main() {
                vec3 p = displayTransform * vec3(a_position, 0);
                gl_Position = vec4(p, 1);
                v_texCoord = a_texCoord;
              }
            `
            const fs = `
              precision highp float;
              uniform sampler2D depth_texture;
              varying vec2 v_texCoord;
              void main() {
                vec4 depth_color = texture2D(depth_texture, v_texCoord);
                gl_FragColor = vec4(depth_color.rgb, 1.0);
              }
            `
            
            const vertShader = gl.createShader(gl.VERTEX_SHADER)
            gl.shaderSource(vertShader, vs)
            gl.compileShader(vertShader)
      
            const fragShader = gl.createShader(gl.FRAGMENT_SHADER)
            gl.shaderSource(fragShader, fs)
            gl.compileShader(fragShader)
      
            const program = this._depthProgram = gl.createProgram()
            this._depthProgram.gl = gl
            gl.attachShader(program, vertShader)
            gl.attachShader(program, fragShader)
            gl.deleteShader(vertShader)
            gl.deleteShader(fragShader)
            gl.linkProgram(program)
            gl.useProgram(program)
      
            const uniformTexture = gl.getUniformLocation(program, 'depth_texture')
            gl.uniform1i(uniformTexture, 5)
      
            this._depthDt = gl.getUniformLocation(program, 'displayTransform')
            gl.useProgram(currentProgram)
        },
        initDepthVAOHint() {
            const gl = this.renderer.getContext()
            const ext = gl.getExtension('OES_vertex_array_object')
            this.ext = ext
      
            const currentVAO = gl.getParameter(gl.VERTEX_ARRAY_BINDING)
            const vao = ext.createVertexArrayOES()
      
            ext.bindVertexArrayOES(vao)
      
            const posAttr = gl.getAttribLocation(this._depthProgram, 'a_position')
            const pos = gl.createBuffer()
            gl.bindBuffer(gl.ARRAY_BUFFER, pos)
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.3, 0.3, 1, 0.3, 0.3, 1, 1, 1]), gl.STATIC_DRAW)
            gl.vertexAttribPointer(posAttr, 2, gl.FLOAT, false, 0, 0)
            gl.enableVertexAttribArray(posAttr)
            vao.posBuffer = pos
      
            const texcoordAttr = gl.getAttribLocation(this._depthProgram, 'a_texCoord')
            const texcoord = gl.createBuffer()
            gl.bindBuffer(gl.ARRAY_BUFFER, texcoord)
            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]), gl.STATIC_DRAW)
            gl.vertexAttribPointer(texcoordAttr, 2, gl.FLOAT, false, 0, 0)
            gl.enableVertexAttribArray(texcoordAttr)
            vao.texcoordBuffer = texcoord
      
            ext.bindVertexArrayOES(currentVAO)
            this._depthVao = vao
        },
        initDepthShader() {
            const gl = this.gl = this.renderer.getContext()
            const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
 
            const dvs = `#version 300 es
                precision highp float;
                in vec2 a_position;
                in vec2 a_texCoord;
                uniform mat3 displayTransform;
                uniform sampler2D depth_texture;     
                out vec2 v_texCoord;
        
                void main() {
                vec3 p = displayTransform * vec3(a_position, 1);
                v_texCoord = a_texCoord;
                vec4 depth_color = texture(depth_texture, v_texCoord);
                gl_Position = vec4(p.x, p.y, p.z, 1);
                }
            `
        
            const dfs = `#version 300 es
                precision highp float;
                uniform sampler2D depth_texture;      
                out vec4 FragColor;
                in vec2 v_texCoord;
        
                void main() {
                vec4 depth_color = texture(depth_texture, v_texCoord);
                gl_FragDepth = depth_color.r;
            //   FragColor = vec4(depth_color.rgb, 1.0);
                }
            `
            const vertShader = gl.createShader(gl.VERTEX_SHADER)
            gl.shaderSource(vertShader, dvs)
            gl.compileShader(vertShader)
      
            const fragShader = gl.createShader(gl.FRAGMENT_SHADER)
            gl.shaderSource(fragShader, dfs)
            gl.compileShader(fragShader)
      
            const program = this._depthOutputProgram = gl.createProgram()
            this._depthOutputProgram.gl = gl
            gl.attachShader(program, vertShader)
            gl.attachShader(program, fragShader)
            gl.deleteShader(vertShader)
            gl.deleteShader(fragShader)
            gl.linkProgram(program)
            gl.useProgram(program)
 
            const uniformDepthTexture = gl.getUniformLocation(this._depthOutputProgram, 'depth_texture')
            gl.uniform1i(uniformDepthTexture, 5)
            gl.getUniformLocation(this._depthOutputProgram, 'displayTransform')
      
            gl.useProgram(currentProgram)
        },
        initDepthGL(){
            // 初始化提示
            this.initDepthShaderHint()
            this.initDepthVAOHint()
            // 初始化深度纹理相关
            this.initDepthShader();
            this._vaoDepth = this.initVAO(this._depthOutputProgram);
        },
        renderDepthGLHint(frame) {
            const gl = this.renderer.getContext();
            const displayTransform = frame.getDisplayTransform()
 
            // DepthBuffer
            const depthBufferRes = frame.getDepthBuffer();
            const depthBuffer = new Float32Array(depthBufferRes.DepthAddress);
 
            // console.log('depthBuffer', depthBuffer[0], depthBuffer[16], depthBuffer[16 * 16], depthBuffer[56 * 56]);
 
            const texture = gl.createTexture();
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
 
            const width = depthBufferRes.width;
            const height = depthBufferRes.height;
 
            // 先直接采用 uint8 写入深度纹理,使用浮点写入的方法会存在锯齿
            const data = new Uint8Array(width * height * 4);
            for (let i = 0; i < depthBuffer.length; i++) {
                let num = parseInt(depthBuffer[i] * 255);
                data[i] = num;
            }
            gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
 
            const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
            const currentActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE)
            const currentVAO = gl.getParameter(gl.VERTEX_ARRAY_BINDING)
 
            gl.useProgram(this._depthProgram)
            this.ext.bindVertexArrayOES(this._depthVao)
 
            gl.uniformMatrix3fv(this._depthDt, false, displayTransform)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
            const bindingTexture5 = gl.getParameter(gl.TEXTURE_BINDING_2D)
            gl.bindTexture(gl.TEXTURE_2D, texture)
 
            gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
            gl.bindTexture(gl.TEXTURE_2D, bindingTexture5)
 
            gl.useProgram(currentProgram)
            gl.activeTexture(currentActiveTexture)
            this.ext.bindVertexArrayOES(currentVAO)
        },
        renderDepthGL(frame) {
            const gl = this.renderer.getContext()
            const displayTransform = frame.getDisplayTransform()
      
            // DepthBuffer
            const depthBufferRes = frame.getDepthBuffer();
            const depthBuffer = new Float32Array(depthBufferRes.DepthAddress);
      
            const texture = gl.createTexture();
            gl.bindTexture(gl.TEXTURE_2D, texture);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
      
            const width = depthBufferRes.width;
            const height = depthBufferRes.height;
 
            // 先直接采用 uint8 写入深度纹理,使用浮点写入的方法会存在锯齿
            const data = new Uint8Array(width * height * 4);
            for (let i = 0; i < depthBuffer.length; i++) {
                let num = parseInt(depthBuffer[i] * 255);
                data[i] = num;
            }
            gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
            
            // console.log('gl depth texture end')
 
            // 绘制左下角提示
            const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM)
            const currentActiveTexture = gl.getParameter(gl.ACTIVE_TEXTURE)
            const currentVAO = gl.getParameter(gl.VERTEX_ARRAY_BINDING)
            const bindingTexture = gl.getParameter(gl.TEXTURE_BINDING_2D)
 
            gl.useProgram(this._depthProgram)
            this.ext.bindVertexArrayOES(this._depthVao)
 
            gl.uniformMatrix3fv(this._depthDt, false, displayTransform)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
            const bindingTexture5 = gl.getParameter(gl.TEXTURE_BINDING_2D)
            gl.bindTexture(gl.TEXTURE_2D, texture)
 
            gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
            gl.bindTexture(gl.TEXTURE_2D, bindingTexture5)
 
            gl.useProgram(currentProgram)
            gl.activeTexture(currentActiveTexture)
            this.ext.bindVertexArrayOES(currentVAO)
 
            // console.log('gl hint end')
 
            // 写入深度遮挡纹理到深度值
 
            gl.enable(gl.DEPTH_TEST)
            gl.depthMask(true)
            gl.depthFunc(gl.ALWAYS)
             
            this.ext.bindVertexArrayOES(this._vaoDepth)
            gl.useProgram(this._depthOutputProgram)
 
            gl.uniformMatrix3fv(this._depthDt, false, displayTransform)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
            const bindingTexture5Depth = gl.getParameter(gl.TEXTURE_BINDING_2D)
 
            gl.bindTexture(gl.TEXTURE_2D, texture)
 
            gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
 
            gl.activeTexture(gl.TEXTURE0 + 5)
 
            gl.bindTexture(gl.TEXTURE_2D, bindingTexture5Depth)
 
            gl.useProgram(currentProgram)
            gl.activeTexture(currentActiveTexture)
            gl.bindTexture(gl.TEXTURE_2D, bindingTexture)
 
            this.ext.bindVertexArrayOES(currentVAO)
 
            gl.depthMask(false)
 
            gl.depthFunc(gl.LESS)
 
            // console.log('gl depth draw end')
 
 
 
        },
    },
})
 
export default threeBehavior;