Skip to content

Commit 06d48dc

Browse files
feat(Lines): alpha support for line color (#335)
1 parent 6033736 commit 06d48dc

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

src/lines/LineGeometry.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,37 @@ class LineGeometry extends LineSegmentsGeometry {
3030
return this
3131
}
3232

33-
setColors(array) {
34-
// converts [ r1, g1, b1, r2, g2, b2, ... ] to pairs format
33+
setColors(array, itemSize = 3) {
34+
// converts [ r1, g1, b1, (a1), r2, g2, b2, (a2), ... ] to pairs format
3535

36-
const length = array.length - 3
36+
const length = array.length - itemSize
3737
const colors = new Float32Array(2 * length)
3838

39-
for (let i = 0; i < length; i += 3) {
40-
colors[2 * i] = array[i]
41-
colors[2 * i + 1] = array[i + 1]
42-
colors[2 * i + 2] = array[i + 2]
43-
44-
colors[2 * i + 3] = array[i + 3]
45-
colors[2 * i + 4] = array[i + 4]
46-
colors[2 * i + 5] = array[i + 5]
39+
if (itemSize === 3) {
40+
for (let i = 0; i < length; i += itemSize) {
41+
colors[2 * i] = array[i]
42+
colors[2 * i + 1] = array[i + 1]
43+
colors[2 * i + 2] = array[i + 2]
44+
45+
colors[2 * i + 3] = array[i + 3]
46+
colors[2 * i + 4] = array[i + 4]
47+
colors[2 * i + 5] = array[i + 5]
48+
}
49+
} else {
50+
for (let i = 0; i < length; i += itemSize) {
51+
colors[2 * i] = array[i]
52+
colors[2 * i + 1] = array[i + 1]
53+
colors[2 * i + 2] = array[i + 2]
54+
colors[2 * i + 3] = array[i + 3]
55+
56+
colors[2 * i + 4] = array[i + 4]
57+
colors[2 * i + 5] = array[i + 5]
58+
colors[2 * i + 6] = array[i + 6]
59+
colors[2 * i + 7] = array[i + 7]
60+
}
4761
}
4862

49-
super.setColors(colors)
63+
super.setColors(colors, itemSize)
5064

5165
return this
5266
}

src/lines/LineMaterial.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class LineMaterial extends ShaderMaterial {
3636

3737
vertexShader: /* glsl */ `
3838
#include <common>
39-
#include <color_pars_vertex>
4039
#include <fog_pars_vertex>
4140
#include <logdepthbuf_pars_vertex>
4241
#include <clipping_planes_pars_vertex>
@@ -47,8 +46,15 @@ class LineMaterial extends ShaderMaterial {
4746
attribute vec3 instanceStart;
4847
attribute vec3 instanceEnd;
4948
50-
attribute vec3 instanceColorStart;
51-
attribute vec3 instanceColorEnd;
49+
#ifdef USE_LINE_COLOR_ALPHA
50+
varying vec4 vLineColor;
51+
attribute vec4 instanceColorStart;
52+
attribute vec4 instanceColorEnd;
53+
#else
54+
varying vec3 vLineColor;
55+
attribute vec3 instanceColorStart;
56+
attribute vec3 instanceColorEnd;
57+
#endif
5258
5359
#ifdef WORLD_UNITS
5460
@@ -94,11 +100,7 @@ class LineMaterial extends ShaderMaterial {
94100
95101
void main() {
96102
97-
#ifdef USE_COLOR
98-
99-
vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
100-
101-
#endif
103+
vLineColor = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
102104
103105
#ifdef USE_DASH
104106
@@ -297,11 +299,16 @@ class LineMaterial extends ShaderMaterial {
297299
#endif
298300
299301
#include <common>
300-
#include <color_pars_fragment>
301302
#include <fog_pars_fragment>
302303
#include <logdepthbuf_pars_fragment>
303304
#include <clipping_planes_pars_fragment>
304305
306+
#ifdef USE_LINE_COLOR_ALPHA
307+
varying vec4 vLineColor;
308+
#else
309+
varying vec3 vLineColor;
310+
#endif
311+
305312
vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
306313
307314
float mua;
@@ -410,11 +417,15 @@ class LineMaterial extends ShaderMaterial {
410417
#endif
411418
412419
vec4 diffuseColor = vec4( diffuse, alpha );
420+
#ifdef USE_LINE_COLOR_ALPHA
421+
diffuseColor *= vLineColor;
422+
#else
423+
diffuseColor.rgb *= vLineColor;
424+
#endif
413425
414426
#include <logdepthbuf_fragment>
415-
#include <color_fragment>
416427
417-
gl_FragColor = vec4( diffuseColor.rgb, alpha );
428+
gl_FragColor = diffuseColor;
418429
419430
#include <tonemapping_fragment>
420431
#include <${parseInt(REVISION.replace(/\D+/g, '')) >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}>
@@ -428,6 +439,14 @@ class LineMaterial extends ShaderMaterial {
428439

429440
this.isLineMaterial = true
430441

442+
this.onBeforeCompile = function () {
443+
if (this.transparent) {
444+
this.defines.USE_LINE_COLOR_ALPHA = '1'
445+
} else {
446+
delete this.defines.USE_LINE_COLOR_ALPHA
447+
}
448+
}
449+
431450
Object.defineProperties(this, {
432451
color: {
433452
enumerable: true,

src/lines/LineSegmentsGeometry.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export class LineSegmentsGeometry extends InstancedBufferGeometry {
1111
fromLineSegments(lineSegments: LineSegments): this
1212
fromMesh(mesh: Mesh): this
1313
fromWireframeGeometry(geometry: WireframeGeometry): this
14-
setColors(array: number[] | Float32Array): this
14+
setColors(array: number[] | Float32Array, itemSize?: 3 | 4): this
1515
setPositions(array: number[] | Float32Array): this
1616
}

src/lines/LineSegmentsGeometry.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class LineSegmentsGeometry extends InstancedBufferGeometry {
7474
return this
7575
}
7676

77-
setColors(array) {
77+
setColors(array, itemSize = 3) {
7878
let colors
7979

8080
if (array instanceof Float32Array) {
@@ -83,10 +83,10 @@ class LineSegmentsGeometry extends InstancedBufferGeometry {
8383
colors = new Float32Array(array)
8484
}
8585

86-
const instanceColorBuffer = new InstancedInterleavedBuffer(colors, 6, 1) // rgb, rgb
86+
const instanceColorBuffer = new InstancedInterleavedBuffer(colors, itemSize * 2, 1) // rgb(a), rgb(a)
8787

88-
this.setAttribute('instanceColorStart', new InterleavedBufferAttribute(instanceColorBuffer, 3, 0)) // rgb
89-
this.setAttribute('instanceColorEnd', new InterleavedBufferAttribute(instanceColorBuffer, 3, 3)) // rgb
88+
this.setAttribute('instanceColorStart', new InterleavedBufferAttribute(instanceColorBuffer, itemSize, 0)) // rgb(a)
89+
this.setAttribute('instanceColorEnd', new InterleavedBufferAttribute(instanceColorBuffer, itemSize, 3)) // rgb(a)
9090

9191
return this
9292
}

0 commit comments

Comments
 (0)