Browse Source

enable conditional lines by default, do perform line logic in vertex shader

Garrett Johnson 6 years ago
parent
commit
f1d06541e7
1 changed files with 24 additions and 27 deletions
  1. 24 27
      examples/js/loaders/LDrawLoader.js

+ 24 - 27
examples/js/loaders/LDrawLoader.js

@@ -11,11 +11,8 @@ THREE.LDrawLoader = ( function () {
 	attribute vec3 control0;
 	attribute vec3 control1;
 	attribute vec3 direction;
+	varying float discardFlag;
 
-	varying vec4 controlOut0;
-	varying vec4 controlOut1;
-	varying vec4 pointOut0;
-	varying vec4 pointOut1;
 	#include <common>
 	#include <color_pars_vertex>
 	#include <fog_pars_vertex>
@@ -23,13 +20,30 @@ THREE.LDrawLoader = ( function () {
 	#include <clipping_planes_pars_vertex>
 	void main() {
 		#include <color_vertex>
+
 		vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
 		gl_Position = projectionMatrix * mvPosition;
 
-		controlOut0 = projectionMatrix * modelViewMatrix * vec4( control0, 1.0 );
-		controlOut1 = projectionMatrix * modelViewMatrix * vec4( control1, 1.0 );
-		pointOut0 = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
-		pointOut1 = projectionMatrix * modelViewMatrix * vec4( position + direction, 1.0 );
+		vec4 c0 = projectionMatrix * modelViewMatrix * vec4( control0, 1.0 );
+		vec4 c1 = projectionMatrix * modelViewMatrix * vec4( control1, 1.0 );
+		vec4 p0 = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
+		vec4 p1 = projectionMatrix * modelViewMatrix * vec4( position + direction, 1.0 );
+
+		c0.xy = c0.xy / c0.w;
+		c1.xy = c1.xy / c1.w;
+		p0.xy = p0.xy / p0.w;
+		p1.xy = p1.xy / p1.w;
+
+		vec2 dir = p1.xy - p0.xy;
+		vec2 norm = vec2( -dir.y, dir.x );
+
+		vec2 c0dir = c0.xy - p1.xy;
+		vec2 c1dir = c1.xy - p1.xy;
+
+		float d0 = dot( normalize( norm ), normalize( c0dir ) );
+		float d1 = dot( normalize( norm ), normalize( c1dir ) );
+
+		if ( sign( d0 ) != sign( d1 ) ) discardFlag = 1.0;
 
 		#include <logdepthbuf_vertex>
 		#include <clipping_planes_vertex>
@@ -40,10 +54,7 @@ THREE.LDrawLoader = ( function () {
 	var optionalLineFragShader = /* glsl */`
 	uniform vec3 diffuse;
 	uniform float opacity;
-	varying vec4 controlOut0;
-	varying vec4 controlOut1;
-	varying vec4 pointOut0;
-	varying vec4 pointOut1;
+	varying float discardFlag;
 
 	#include <common>
 	#include <color_pars_fragment>
@@ -52,20 +63,7 @@ THREE.LDrawLoader = ( function () {
 	#include <clipping_planes_pars_fragment>
 	void main() {
 
-		vec2 c0 = controlOut0.xy / controlOut0.w;
-		vec2 c1 = controlOut1.xy / controlOut1.w;
-		vec2 p0 = pointOut0.xy / pointOut0.w;
-		vec2 p1 = pointOut1.xy / pointOut1.w;
-		vec2 dir = p1 - p0;
-		vec2 norm = vec2( -dir.y, dir.x );
-
-		vec2 c0dir = c0 - p1;
-		vec2 c1dir = c1 - p1;
-
-		float d0 = dot( normalize( norm ), normalize( c0dir ) );
-		float d1 = dot( normalize( norm ), normalize( c1dir ) );
-
-		if ( sign( d0 ) != sign( d1 ) ) discard;
+		if ( discardFlag > 0.5 ) discard;
 
 		#include <clipping_planes_fragment>
 		vec3 outgoingLight = vec3( 0.0 );
@@ -627,7 +625,6 @@ THREE.LDrawLoader = ( function () {
 							var conditionalSegments = parseScope.conditionalSegments;
 							var lines = createObject( conditionalSegments, 2 );
 							lines.isConditionalLine = true;
-							lines.visible = false;
 
 							var controlArray0 = new Float32Array( conditionalSegments.length * 3 * 2 );
 							var controlArray1 = new Float32Array( conditionalSegments.length * 3 * 2 );