roxy-cobewebgl 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +1 -1
- package/src/fallback.js +12 -4
- package/src/globe.worker.js +11 -4
- package/src/react/RoxyCobewebgl.jsx +1 -0
- package/src/vue/RoxyCobewebgl.vue +1 -0
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Cobe 风格的高性能点阵地球:纯 WebGL + GLSL,可选 **OffscreenCanvas Worker** 渲染(主线程只做交互),不支持时自动 **主线程 WebGL 回退**。以 **ESM** 发布,可在 Vite / Webpack 5+ 等打包器中使用。
|
|
4
4
|
|
|
5
|
+
**画布背景**:WebGL 使用带 **alpha** 的上下文(`premultipliedAlpha: false`),每帧以**透明色**清空缓冲,并用 `SRC_ALPHA` 混合绘制;球圆外(**`glowOn` 为关**时)为**全透明**,可透出下层。若仍看到暗边,多为**外圈光晕**,可把 `glowOn` 设为 `0`。`<canvas>` 建议配合 CSS `background: transparent`。
|
|
6
|
+
|
|
5
7
|
## 安装
|
|
6
8
|
|
|
7
9
|
```bash
|
package/package.json
CHANGED
package/src/fallback.js
CHANGED
|
@@ -421,12 +421,18 @@ function normalizeArcs(arcsData) {
|
|
|
421
421
|
*/
|
|
422
422
|
export async function createFallbackGlobe(canvas, config) {
|
|
423
423
|
const onError = config.onError || ((e) => console.error('[cobewebgl-fallback]', e));
|
|
424
|
-
const gl = canvas.getContext('webgl', {
|
|
424
|
+
const gl = canvas.getContext('webgl', {
|
|
425
|
+
antialias: false,
|
|
426
|
+
alpha: true,
|
|
427
|
+
premultipliedAlpha: false,
|
|
428
|
+
});
|
|
425
429
|
if (!gl) {
|
|
426
430
|
onError(new Error('WebGL 不可用'));
|
|
427
431
|
return null;
|
|
428
432
|
}
|
|
429
433
|
|
|
434
|
+
gl.clearColor(0, 0, 0, 0);
|
|
435
|
+
|
|
430
436
|
const program = createProgram(gl, VERTEX_SHADER, FRAGMENT_SHADER);
|
|
431
437
|
if (!program) {
|
|
432
438
|
onError(new Error('主着色器编译失败'));
|
|
@@ -511,6 +517,11 @@ export async function createFallbackGlobe(canvas, config) {
|
|
|
511
517
|
|
|
512
518
|
const debugVal = typeof state.debug === 'number' ? state.debug : (typeof window !== 'undefined' && window.__GLOBE_DEBUG__) || 0;
|
|
513
519
|
|
|
520
|
+
gl.clearColor(0, 0, 0, 0);
|
|
521
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
522
|
+
gl.enable(gl.BLEND);
|
|
523
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
524
|
+
|
|
514
525
|
gl.useProgram(program);
|
|
515
526
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
516
527
|
gl.enableVertexAttribArray(loc.a_position);
|
|
@@ -534,8 +545,6 @@ export async function createFallbackGlobe(canvas, config) {
|
|
|
534
545
|
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
535
546
|
|
|
536
547
|
if (arcProgram && arcAnimator) {
|
|
537
|
-
gl.enable(gl.BLEND);
|
|
538
|
-
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
539
548
|
gl.useProgram(arcProgram);
|
|
540
549
|
const aspect = canvas.width / canvas.height;
|
|
541
550
|
gl.uniform1f(arcLoc.u_phi, state.phi);
|
|
@@ -555,7 +564,6 @@ export async function createFallbackGlobe(canvas, config) {
|
|
|
555
564
|
gl.vertexAttribPointer(arcLoc.a_arcPos, 3, gl.FLOAT, false, 0, 0);
|
|
556
565
|
gl.drawArrays(gl.LINE_STRIP, st.startIdx, st.drawCount);
|
|
557
566
|
}
|
|
558
|
-
gl.disable(gl.BLEND);
|
|
559
567
|
}
|
|
560
568
|
rafId = requestAnimationFrame(render);
|
|
561
569
|
}
|
package/src/globe.worker.js
CHANGED
|
@@ -441,11 +441,16 @@ async function init(msg) {
|
|
|
441
441
|
if (msg.params) Object.assign(params, msg.params);
|
|
442
442
|
|
|
443
443
|
console.log('[Worker] init, canvas size:', canvas.width, canvas.height);
|
|
444
|
-
gl = canvas.getContext('webgl', {
|
|
444
|
+
gl = canvas.getContext('webgl', {
|
|
445
|
+
antialias: false,
|
|
446
|
+
alpha: true,
|
|
447
|
+
premultipliedAlpha: false,
|
|
448
|
+
});
|
|
445
449
|
if (!gl) {
|
|
446
450
|
self.postMessage({ type: 'error', msg: 'WebGL 不可用' });
|
|
447
451
|
return;
|
|
448
452
|
}
|
|
453
|
+
gl.clearColor(0, 0, 0, 0);
|
|
449
454
|
console.log('[Worker] WebGL context OK');
|
|
450
455
|
|
|
451
456
|
program = createProgram(gl, VERTEX_SHADER, FRAGMENT_SHADER);
|
|
@@ -521,6 +526,11 @@ function render(t) {
|
|
|
521
526
|
const timeSec = t * 0.001;
|
|
522
527
|
if (params.autoRotate) params.phi += params.rotationSpeed || 0.003;
|
|
523
528
|
|
|
529
|
+
gl.clearColor(0, 0, 0, 0);
|
|
530
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
531
|
+
gl.enable(gl.BLEND);
|
|
532
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
533
|
+
|
|
524
534
|
// Pass 1: 球体
|
|
525
535
|
gl.useProgram(program);
|
|
526
536
|
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
@@ -547,8 +557,6 @@ function render(t) {
|
|
|
547
557
|
|
|
548
558
|
// Pass 2: 弧线
|
|
549
559
|
if (arcProgram && arcAnimator) {
|
|
550
|
-
gl.enable(gl.BLEND);
|
|
551
|
-
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
552
560
|
gl.useProgram(arcProgram);
|
|
553
561
|
const aspect = canvas.width / canvas.height;
|
|
554
562
|
gl.uniform1f(arcLoc.u_phi, params.phi);
|
|
@@ -570,7 +578,6 @@ function render(t) {
|
|
|
570
578
|
gl.vertexAttribPointer(arcLoc.a_arcPos, 3, gl.FLOAT, false, 0, 0);
|
|
571
579
|
gl.drawArrays(gl.LINE_STRIP, state.startIdx, state.drawCount);
|
|
572
580
|
}
|
|
573
|
-
gl.disable(gl.BLEND);
|
|
574
581
|
}
|
|
575
582
|
|
|
576
583
|
rafId = requestAnimationFrame(render);
|