LDrawConditionalLineMaterial.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. Color,
  3. ShaderMaterial,
  4. UniformsLib,
  5. UniformsUtils,
  6. } from 'three';
  7. class LDrawConditionalLineMaterial extends ShaderMaterial {
  8. static get type() {
  9. return 'LDrawConditionalLineMaterial';
  10. }
  11. constructor( parameters ) {
  12. super( {
  13. uniforms: UniformsUtils.merge( [
  14. UniformsLib.fog,
  15. {
  16. diffuse: {
  17. value: new Color()
  18. },
  19. opacity: {
  20. value: 1.0
  21. }
  22. }
  23. ] ),
  24. vertexShader: /* glsl */`
  25. attribute vec3 control0;
  26. attribute vec3 control1;
  27. attribute vec3 direction;
  28. varying float discardFlag;
  29. #include <common>
  30. #include <color_pars_vertex>
  31. #include <fog_pars_vertex>
  32. #include <logdepthbuf_pars_vertex>
  33. #include <clipping_planes_pars_vertex>
  34. void main() {
  35. #include <color_vertex>
  36. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  37. gl_Position = projectionMatrix * mvPosition;
  38. // Transform the line segment ends and control points into camera clip space
  39. vec4 c0 = projectionMatrix * modelViewMatrix * vec4( control0, 1.0 );
  40. vec4 c1 = projectionMatrix * modelViewMatrix * vec4( control1, 1.0 );
  41. vec4 p0 = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  42. vec4 p1 = projectionMatrix * modelViewMatrix * vec4( position + direction, 1.0 );
  43. c0.xy /= c0.w;
  44. c1.xy /= c1.w;
  45. p0.xy /= p0.w;
  46. p1.xy /= p1.w;
  47. // Get the direction of the segment and an orthogonal vector
  48. vec2 dir = p1.xy - p0.xy;
  49. vec2 norm = vec2( -dir.y, dir.x );
  50. // Get control point directions from the line
  51. vec2 c0dir = c0.xy - p1.xy;
  52. vec2 c1dir = c1.xy - p1.xy;
  53. // If the vectors to the controls points are pointed in different directions away
  54. // from the line segment then the line should not be drawn.
  55. float d0 = dot( normalize( norm ), normalize( c0dir ) );
  56. float d1 = dot( normalize( norm ), normalize( c1dir ) );
  57. discardFlag = float( sign( d0 ) != sign( d1 ) );
  58. #include <logdepthbuf_vertex>
  59. #include <clipping_planes_vertex>
  60. #include <fog_vertex>
  61. }
  62. `,
  63. fragmentShader: /* glsl */`
  64. uniform vec3 diffuse;
  65. uniform float opacity;
  66. varying float discardFlag;
  67. #include <common>
  68. #include <color_pars_fragment>
  69. #include <fog_pars_fragment>
  70. #include <logdepthbuf_pars_fragment>
  71. #include <clipping_planes_pars_fragment>
  72. void main() {
  73. if ( discardFlag > 0.5 ) discard;
  74. #include <clipping_planes_fragment>
  75. vec3 outgoingLight = vec3( 0.0 );
  76. vec4 diffuseColor = vec4( diffuse, opacity );
  77. #include <logdepthbuf_fragment>
  78. #include <color_fragment>
  79. outgoingLight = diffuseColor.rgb; // simple shader
  80. gl_FragColor = vec4( outgoingLight, diffuseColor.a );
  81. #include <tonemapping_fragment>
  82. #include <colorspace_fragment>
  83. #include <fog_fragment>
  84. #include <premultiplied_alpha_fragment>
  85. }
  86. `,
  87. } );
  88. Object.defineProperties( this, {
  89. opacity: {
  90. get: function () {
  91. return this.uniforms.opacity.value;
  92. },
  93. set: function ( value ) {
  94. this.uniforms.opacity.value = value;
  95. }
  96. },
  97. color: {
  98. get: function () {
  99. return this.uniforms.diffuse.value;
  100. }
  101. }
  102. } );
  103. this.setValues( parameters );
  104. this.isLDrawConditionalLineMaterial = true;
  105. }
  106. }
  107. export { LDrawConditionalLineMaterial };
粤ICP备19079148号