Skip to content

Commit ca7261b

Browse files
authored
LineMaterial: Fix clipping at edge artifacts (#27349)
* Fix containing cube * Sceenshot update
1 parent a79aa37 commit ca7261b

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

examples/jsm/lines/LineMaterial.js

+16-33
Original file line numberDiff line numberDiff line change
@@ -169,51 +169,34 @@ ShaderLib[ 'line' ] = {
169169
170170
#ifdef WORLD_UNITS
171171
172-
// get the offset direction as perpendicular to the view vector
173172
vec3 worldDir = normalize( end.xyz - start.xyz );
174-
vec3 offset;
175-
if ( position.y < 0.5 ) {
173+
vec3 tmpFwd = normalize( mix( start.xyz, end.xyz, 0.5 ) );
174+
vec3 worldUp = normalize( cross( worldDir, tmpFwd ) );
175+
vec3 worldFwd = cross( worldDir, worldUp );
176+
worldPos = position.y < 0.5 ? start: end;
176177
177-
offset = normalize( cross( start.xyz, worldDir ) );
178-
179-
} else {
180-
181-
offset = normalize( cross( end.xyz, worldDir ) );
182-
183-
}
184-
185-
// sign flip
186-
if ( position.x < 0.0 ) offset *= - 1.0;
187-
188-
float forwardOffset = dot( worldDir, vec3( 0.0, 0.0, 1.0 ) );
178+
// height offset
179+
float hw = linewidth * 0.5;
180+
worldPos.xyz += position.x < 0.0 ? hw * worldUp : - hw * worldUp;
189181
190182
// don't extend the line if we're rendering dashes because we
191183
// won't be rendering the endcaps
192184
#ifndef USE_DASH
193185
194-
// extend the line bounds to encompass endcaps
195-
start.xyz += - worldDir * linewidth * 0.5;
196-
end.xyz += worldDir * linewidth * 0.5;
186+
// cap extension
187+
worldPos.xyz += position.y < 0.5 ? - hw * worldDir : hw * worldDir;
197188
198-
// shift the position of the quad so it hugs the forward edge of the line
199-
offset.xy -= dir * forwardOffset;
200-
offset.z += 0.5;
189+
// add width to the box
190+
worldPos.xyz += worldFwd * hw;
201191
202-
#endif
203-
204-
// endcaps
205-
if ( position.y > 1.0 || position.y < 0.0 ) {
206-
207-
offset.xy += dir * 2.0 * forwardOffset;
192+
// endcaps
193+
if ( position.y > 1.0 || position.y < 0.0 ) {
208194
209-
}
195+
worldPos.xyz -= worldFwd * 2.0 * hw;
210196
211-
// adjust for linewidth
212-
offset *= linewidth * 0.5;
197+
}
213198
214-
// set the world position
215-
worldPos = ( position.y < 0.5 ) ? start : end;
216-
worldPos.xyz += offset;
199+
#endif
217200
218201
// project the worldpos
219202
vec4 clip = projectionMatrix * worldPos;
167 Bytes
Loading

0 commit comments

Comments
 (0)