Skip to content

Commit

Permalink
LineMaterial: Refactor GLSL. (#22726)
Browse files Browse the repository at this point in the history
* LineMaterial: Refactor GLSL.

* Clean up.

* More clean up.
  • Loading branch information
Mugen87 authored Oct 25, 2021
1 parent cd8c225 commit 431baa0
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions examples/jsm/lines/LineMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,23 @@ ShaderLib[ 'line' ] = {
attribute vec3 instanceColorStart;
attribute vec3 instanceColorEnd;
varying vec2 vUv;
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef WORLD_UNITS
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef USE_DASH
varying vec2 vUv;
#endif
#else
varying vec2 vUv;
#endif
#ifdef USE_DASH
Expand Down Expand Up @@ -95,19 +108,26 @@ ShaderLib[ 'line' ] = {
#ifdef USE_DASH
vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
vUv = uv;
#endif
float aspect = resolution.x / resolution.y;
vUv = uv;
// camera space
vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
worldStart = start.xyz;
worldEnd = end.xyz;
#ifdef WORLD_UNITS
worldStart = start.xyz;
worldEnd = end.xyz;
#else
vUv = uv;
#endif
// special case for perspective projection, and segments that terminate either in, or behind, the camera plane
// clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
Expand Down Expand Up @@ -263,18 +283,31 @@ ShaderLib[ 'line' ] = {
#endif
varying float vLineDistance;
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef WORLD_UNITS
varying vec4 worldPos;
varying vec3 worldStart;
varying vec3 worldEnd;
#ifdef USE_DASH
varying vec2 vUv;
#endif
#else
varying vec2 vUv;
#endif
#include <common>
#include <color_pars_fragment>
#include <fog_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
varying vec2 vUv;
vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
float mua;
Expand Down Expand Up @@ -333,7 +366,7 @@ ShaderLib[ 'line' ] = {
#ifndef USE_DASH
#ifdef ALPHA_TO_COVERAGE
#ifdef USE_ALPHA_TO_COVERAGE
float dnorm = fwidth( norm );
alpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm );
Expand All @@ -352,7 +385,7 @@ ShaderLib[ 'line' ] = {
#else
#ifdef ALPHA_TO_COVERAGE
#ifdef USE_ALPHA_TO_COVERAGE
// artifacts appear on some hardware if a derivative is taken within a conditional
float a = vUv.x;
Expand Down Expand Up @@ -625,26 +658,26 @@ class LineMaterial extends ShaderMaterial {

get: function () {

return Boolean( 'ALPHA_TO_COVERAGE' in this.defines );
return Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines );

},

set: function ( value ) {

if ( Boolean( value ) !== Boolean( 'ALPHA_TO_COVERAGE' in this.defines ) ) {
if ( Boolean( value ) !== Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ) ) {

this.needsUpdate = true;

}

if ( value === true ) {

this.defines.ALPHA_TO_COVERAGE = '';
this.defines.USE_ALPHA_TO_COVERAGE = '';
this.extensions.derivatives = true;

} else {

delete this.defines.ALPHA_TO_COVERAGE;
delete this.defines.USE_ALPHA_TO_COVERAGE;
this.extensions.derivatives = false;

}
Expand Down

0 comments on commit 431baa0

Please sign in to comment.