LDrawConditionalLineNodeMaterial.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { Color } from 'three';
  2. import { attribute, cameraProjectionMatrix, dot, float, Fn, modelViewMatrix, modelViewProjection, NodeMaterial, normalize, positionGeometry, sign, uniform, varyingProperty, vec2, vec4 } from 'three/tsl';
  3. /**
  4. * A special line material for meshes loaded via {@link LDrawLoader}.
  5. *
  6. * This module can only be used with {@link WebGPURenderer}. When using {@link WebGLRenderer},
  7. * import the class from `LDrawConditionalLineMaterial.js`.
  8. *
  9. * @augments NodeMaterial
  10. */
  11. class LDrawConditionalLineMaterial extends NodeMaterial {
  12. static get type() {
  13. return 'LDrawConditionalLineMaterial';
  14. }
  15. /**
  16. * Constructs a new conditional line material.
  17. *
  18. * @param {Object} [parameters] - An object with one or more properties
  19. * defining the material's appearance. Any property of the material
  20. * (including any property from inherited materials) can be passed
  21. * in here. Color values can be passed any type of value accepted
  22. * by {@link Color#set}.
  23. */
  24. constructor( parameters ) {
  25. super();
  26. const vertexNode = /*@__PURE__*/ Fn( () => {
  27. const control0 = attribute( 'control0', 'vec3' );
  28. const control1 = attribute( 'control1', 'vec3' );
  29. const direction = attribute( 'direction', 'vec3' );
  30. const mvp = cameraProjectionMatrix.mul( modelViewMatrix );
  31. // Transform the line segment ends and control points into camera clip space
  32. const c0 = mvp.mul( vec4( control0, 1 ) ).toVar();
  33. const c1 = mvp.mul( vec4( control1, 1 ) ).toVar();
  34. const p0 = mvp.mul( vec4( positionGeometry, 1 ) ).toVar();
  35. const p1 = mvp.mul( vec4( positionGeometry.add( direction ), 1 ) ).toVar();
  36. c0.xy.divAssign( c0.w );
  37. c1.xy.divAssign( c1.w );
  38. p0.xy.divAssign( p0.w );
  39. p1.xy.divAssign( p1.w );
  40. // Get the direction of the segment and an orthogonal vector
  41. const dir = p1.xy.sub( p0.xy ).toVar();
  42. const norm = vec2( dir.y.negate(), dir.x ).toVar();
  43. // Get control point directions from the line
  44. const c0dir = c0.xy.sub( p1.xy ).toVar();
  45. const c1dir = c1.xy.sub( p1.xy ).toVar();
  46. // If the vectors to the controls points are pointed in different directions away
  47. // from the line segment then the line should not be drawn.
  48. const d0 = dot( normalize( norm ), normalize( c0dir ) ).toVar();
  49. const d1 = dot( normalize( norm ), normalize( c1dir ) ).toVar();
  50. const discardFlag = sign( d0 ).notEqual( sign( d1 ) ).select( float( 1 ), float( 0 ) );
  51. varyingProperty( 'float', 'discardFlag' ).assign( discardFlag );
  52. return modelViewProjection;
  53. } )();
  54. const fragmentNode = /*@__PURE__*/ Fn( () => {
  55. const discardFlag = varyingProperty( 'float', 'discardFlag' );
  56. discardFlag.greaterThan( float( 0.5 ) ).discard();
  57. return vec4( this._diffuseUniform, this._opacityUniform );
  58. } )();
  59. this.vertexNode = vertexNode;
  60. this.fragmentNode = fragmentNode;
  61. this._diffuseUniform = uniform( new Color() );
  62. this._opacityUniform = uniform( 1 );
  63. //
  64. Object.defineProperties( this, {
  65. /**
  66. * The material's opacity.
  67. *
  68. * @name LDrawConditionalLineMaterial#opacity
  69. * @type {number}
  70. * @default 1
  71. */
  72. opacity: {
  73. get: function () {
  74. return this._opacityUniform.value;
  75. },
  76. set: function ( value ) {
  77. this._opacityUniform.value = value;
  78. }
  79. },
  80. /**
  81. * The material's color.
  82. *
  83. * @name LDrawConditionalLineMaterial#color
  84. * @type {Color}
  85. * @default (1,1,1)
  86. */
  87. color: {
  88. get: function () {
  89. return this._diffuseUniform.value;
  90. },
  91. set: function ( value ) {
  92. this._diffuseUniform.value.copy( value );
  93. }
  94. }
  95. } );
  96. this.setValues( parameters );
  97. /**
  98. * This flag can be used for type testing.
  99. *
  100. * @type {boolean}
  101. * @readonly
  102. * @default true
  103. */
  104. this.isLDrawConditionalLineMaterial = true;
  105. }
  106. }
  107. export { LDrawConditionalLineMaterial };
粤ICP备19079148号