After reading a concise introduction to shaders for three.js on this website, I decided to simplify the shaders used in the example I previously mentioned to their basic versions found in this potentially relevant link.
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
precision mediump int;
attribute vec4 color;
varying vec4 vColor;
void main() {
vColor = color;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
precision mediump float;
precision mediump int;
varying vec4 vColor;
void main() {
vec4 color = vec4( vColor );
gl_FragColor = color;
}
</script>
Using these simplified shaders and the THREE.ShaderMaterial
,
var material = new THREE.ShaderMaterial({
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent,
transparent: true,
blending: THREE.AdditiveBlending,
depthTest: false
});
I then filled the
var colors = new Float32Array(segments * 4 * 2);
array with color and alpha values, and assigned it to
geometry.addAttribute('color', new THREE.BufferAttribute(colors, 4, true));
This allowed me to achieve the desired effect. As a note, the variable segments
indicates the number of segments in my THREE.LineSegments
mesh. Additionally, as previously advised by WestLangley in my earlier question, I needed to include color vectors twice (for both ends of a segment) hence the multiplier of 2. It's been an interesting journey; next on the agenda: reintroduce lineWidth
into the mix.