LDrawConditionalLineMaterial.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {
  2. Color,
  3. ShaderMaterial,
  4. UniformsLib,
  5. UniformsUtils,
  6. } from 'three';
  7. /**
  8. * A special line material for meshes loaded via {@link LDrawLoader}.
  9. *
  10. * This module can only be used with {@link WebGLRenderer}. When using {@link WebGPURenderer},
  11. * import the class from `LDrawConditionalLineNodeMaterial.js`.
  12. *
  13. * @augments ShaderMaterial
  14. */
  15. class LDrawConditionalLineMaterial extends ShaderMaterial {
  16. static get type() {
  17. return 'LDrawConditionalLineMaterial';
  18. }
  19. /**
  20. * Constructs a new conditional line material.
  21. *
  22. * @param {Object} [parameters] - An object with one or more properties
  23. * defining the material's appearance. Any property of the material
  24. * (including any property from inherited materials) can be passed
  25. * in here. Color values can be passed any type of value accepted
  26. * by {@link Color#set}.
  27. */
  28. constructor( parameters ) {
  29. super( {
  30. uniforms: UniformsUtils.merge( [
  31. UniformsLib.fog,
  32. {
  33. diffuse: {
  34. value: new Color()
  35. },
  36. opacity: {
  37. value: 1.0
  38. }
  39. }
  40. ] ),
  41. vertexShader: /* glsl */`
  42. attribute vec3 control0;
  43. attribute vec3 control1;
  44. attribute vec3 direction;
  45. varying float discardFlag;
  46. #include <common>
  47. #include <color_pars_vertex>
  48. #include <fog_pars_vertex>
  49. #include <logdepthbuf_pars_vertex>
  50. #include <clipping_planes_pars_vertex>
  51. void main() {
  52. #include <color_vertex>
  53. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  54. gl_Position = projectionMatrix * mvPosition;
  55. // Transform the line segment ends and control points into camera clip space
  56. vec4 c0 = projectionMatrix * modelViewMatrix * vec4( control0, 1.0 );
  57. vec4 c1 = projectionMatrix * modelViewMatrix * vec4( control1, 1.0 );
  58. vec4 p0 = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  59. vec4 p1 = projectionMatrix * modelViewMatrix * vec4( position + direction, 1.0 );
  60. c0.xy /= c0.w;
  61. c1.xy /= c1.w;
  62. p0.xy /= p0.w;
  63. p1.xy /= p1.w;
  64. // Get the direction of the segment and an orthogonal vector
  65. vec2 dir = p1.xy - p0.xy;
  66. vec2 norm = vec2( -dir.y, dir.x );
  67. // Get control point directions from the line
  68. vec2 c0dir = c0.xy - p1.xy;
  69. vec2 c1dir = c1.xy - p1.xy;
  70. // If the vectors to the controls points are pointed in different directions away
  71. // from the line segment then the line should not be drawn.
  72. float d0 = dot( normalize( norm ), normalize( c0dir ) );
  73. float d1 = dot( normalize( norm ), normalize( c1dir ) );
  74. discardFlag = float( sign( d0 ) != sign( d1 ) );
  75. #include <logdepthbuf_vertex>
  76. #include <clipping_planes_vertex>
  77. #include <fog_vertex>
  78. }
  79. `,
  80. fragmentShader: /* glsl */`
  81. uniform vec3 diffuse;
  82. uniform float opacity;
  83. varying float discardFlag;
  84. #include <common>
  85. #include <color_pars_fragment>
  86. #include <fog_pars_fragment>
  87. #include <logdepthbuf_pars_fragment>
  88. #include <clipping_planes_pars_fragment>
  89. void main() {
  90. if ( discardFlag > 0.5 ) discard;
  91. #include <clipping_planes_fragment>
  92. vec3 outgoingLight = vec3( 0.0 );
  93. vec4 diffuseColor = vec4( diffuse, opacity );
  94. #include <logdepthbuf_fragment>
  95. #include <color_fragment>
  96. outgoingLight = diffuseColor.rgb; // simple shader
  97. gl_FragColor = vec4( outgoingLight, diffuseColor.a );
  98. #include <tonemapping_fragment>
  99. #include <colorspace_fragment>
  100. #include <fog_fragment>
  101. #include <premultiplied_alpha_fragment>
  102. }
  103. `,
  104. } );
  105. Object.defineProperties( this, {
  106. /**
  107. * The material's opacity.
  108. *
  109. * @name LDrawConditionalLineMaterial#opacity
  110. * @type {number}
  111. * @default 1
  112. */
  113. opacity: {
  114. get: function () {
  115. return this.uniforms.opacity.value;
  116. },
  117. set: function ( value ) {
  118. this.uniforms.opacity.value = value;
  119. }
  120. },
  121. /**
  122. * The material's color.
  123. *
  124. * @name LDrawConditionalLineMaterial#color
  125. * @type {Color}
  126. * @default (1,1,1)
  127. */
  128. color: {
  129. get: function () {
  130. return this.uniforms.diffuse.value;
  131. }
  132. }
  133. } );
  134. this.setValues( parameters );
  135. /**
  136. * This flag can be used for type testing.
  137. *
  138. * @type {boolean}
  139. * @readonly
  140. * @default true
  141. */
  142. this.isLDrawConditionalLineMaterial = true;
  143. }
  144. }
  145. export { LDrawConditionalLineMaterial };
粤ICP备19079148号