Skip to content

Commit

Permalink
Line2NodeMaterial: Fix broken dash. (#29835)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 authored Nov 7, 2024
1 parent 3728a0a commit d2f7f08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
29 changes: 18 additions & 11 deletions src/materials/nodes/Line2NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { materialColor, materialLineScale, materialLineDashSize, materialLineGap
import { modelViewMatrix } from '../../nodes/accessors/ModelNode.js';
import { positionGeometry } from '../../nodes/accessors/Position.js';
import { mix, smoothstep } from '../../nodes/math/MathNode.js';
import { Fn, varying, float, vec2, vec3, vec4, If } from '../../nodes/tsl/TSLBase.js';
import { Fn, float, vec2, vec3, vec4, If } from '../../nodes/tsl/TSLBase.js';
import { uv } from '../../nodes/accessors/UV.js';
import { viewport } from '../../nodes/display/ScreenNode.js';
import { dashSize, gapSize } from '../../nodes/core/PropertyNode.js';
Expand Down Expand Up @@ -94,6 +94,21 @@ class Line2NodeMaterial extends NodeMaterial {
const start = vec4( modelViewMatrix.mul( vec4( instanceStart, 1.0 ) ) ).toVar( 'start' );
const end = vec4( modelViewMatrix.mul( vec4( instanceEnd, 1.0 ) ) ).toVar( 'end' );

if ( useDash ) {

const dashScaleNode = this.dashScaleNode ? float( this.dashScaleNode ) : materialLineScale;
const offsetNode = this.offsetNode ? float( this.offsetNodeNode ) : materialLineDashOffset;

const instanceDistanceStart = attribute( 'instanceDistanceStart' );
const instanceDistanceEnd = attribute( 'instanceDistanceEnd' );

let lineDistance = positionGeometry.y.lessThan( 0.5 ).select( dashScaleNode.mul( instanceDistanceStart ), dashScaleNode.mul( instanceDistanceEnd ) );
lineDistance = lineDistance.add( offsetNode );

varyingProperty( 'float', 'lineDistance' ).assign( lineDistance );

}

if ( useWorldUnits ) {

varyingProperty( 'vec3', 'worldStart' ).assign( start.xyz );
Expand Down Expand Up @@ -258,24 +273,16 @@ class Line2NodeMaterial extends NodeMaterial {

if ( useDash ) {

const offsetNode = this.offsetNode ? float( this.offsetNodeNode ) : materialLineDashOffset;
const dashScaleNode = this.dashScaleNode ? float( this.dashScaleNode ) : materialLineScale;
const dashSizeNode = this.dashSizeNode ? float( this.dashSizeNode ) : materialLineDashSize;
const gapSizeNode = this.dashSizeNode ? float( this.dashGapNode ) : materialLineGapSize;

dashSize.assign( dashSizeNode );
gapSize.assign( gapSizeNode );

const instanceDistanceStart = attribute( 'instanceDistanceStart' );
const instanceDistanceEnd = attribute( 'instanceDistanceEnd' );

const lineDistance = positionGeometry.y.lessThan( 0.5 ).select( dashScaleNode.mul( instanceDistanceStart ), materialLineScale.mul( instanceDistanceEnd ) );

const vLineDistance = varying( lineDistance.add( materialLineDashOffset ) );
const vLineDistanceOffset = offsetNode ? vLineDistance.add( offsetNode ) : vLineDistance;
const vLineDistance = varyingProperty( 'float', 'lineDistance' );

vUv.y.lessThan( - 1.0 ).or( vUv.y.greaterThan( 1.0 ) ).discard(); // discard endcaps
vLineDistanceOffset.mod( dashSize.add( gapSize ) ).greaterThan( dashSize ).discard(); // todo - FIX
vLineDistance.mod( dashSize.add( gapSize ) ).greaterThan( dashSize ).discard(); // todo - FIX

}

Expand Down
6 changes: 4 additions & 2 deletions src/materials/nodes/LineDashedNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NodeMaterial from './NodeMaterial.js';
import { attribute } from '../../nodes/core/AttributeNode.js';
import { materialLineDashSize, materialLineGapSize, materialLineScale } from '../../nodes/accessors/MaterialNode.js';
import { materialLineDashOffset, materialLineDashSize, materialLineGapSize, materialLineScale } from '../../nodes/accessors/MaterialNode.js';
import { dashSize, gapSize } from '../../nodes/core/PropertyNode.js';
import { varying, float } from '../../nodes/tsl/TSLBase.js';

Expand All @@ -26,6 +26,8 @@ class LineDashedNodeMaterial extends NodeMaterial {

this.setDefaultValues( _defaultValues );

this.dashOffset = 0;

this.offsetNode = null;
this.dashScaleNode = null;
this.dashSizeNode = null;
Expand All @@ -37,7 +39,7 @@ class LineDashedNodeMaterial extends NodeMaterial {

setupVariants() {

const offsetNode = this.offsetNode;
const offsetNode = this.offsetNode ? float( this.offsetNodeNode ) : materialLineDashOffset;
const dashScaleNode = this.dashScaleNode ? float( this.dashScaleNode ) : materialLineScale;
const dashSizeNode = this.dashSizeNode ? float( this.dashSizeNode ) : materialLineDashSize;
const gapSizeNode = this.dashSizeNode ? float( this.dashGapNode ) : materialLineGapSize;
Expand Down

0 comments on commit d2f7f08

Please sign in to comment.