1
0

Wireframe.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {
  2. InstancedInterleavedBuffer,
  3. InterleavedBufferAttribute,
  4. Mesh,
  5. Vector3,
  6. Vector4
  7. } from 'three';
  8. import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry.js';
  9. import { LineMaterial } from '../lines/LineMaterial.js';
  10. const _start = new Vector3();
  11. const _end = new Vector3();
  12. const _viewport = new Vector4();
  13. /**
  14. * A class for creating wireframes based on wide lines.
  15. *
  16. * This module can only be used with {@link WebGLRenderer}. When using {@link WebGPURenderer},
  17. * import the class from `lines/webgpu/Wireframe.js`.
  18. *
  19. * ```js
  20. * const geometry = new THREE.IcosahedronGeometry();
  21. * const wireframeGeometry = new WireframeGeometry2( geo );
  22. *
  23. * const wireframe = new Wireframe( wireframeGeometry, material );
  24. * scene.add( wireframe );
  25. * ```
  26. *
  27. * @augments Mesh
  28. */
  29. class Wireframe extends Mesh {
  30. /**
  31. * Constructs a new wireframe.
  32. *
  33. * @param {LineSegmentsGeometry} [geometry] - The line geometry.
  34. * @param {LineMaterial} [material] - The line material.
  35. */
  36. constructor( geometry = new LineSegmentsGeometry(), material = new LineMaterial( { color: Math.random() * 0xffffff } ) ) {
  37. super( geometry, material );
  38. /**
  39. * This flag can be used for type testing.
  40. *
  41. * @type {boolean}
  42. * @readonly
  43. * @default true
  44. */
  45. this.isWireframe = true;
  46. this.type = 'Wireframe';
  47. }
  48. /**
  49. * Computes an array of distance values which are necessary for rendering dashed lines.
  50. * For each vertex in the geometry, the method calculates the cumulative length from the
  51. * current point to the very beginning of the line.
  52. *
  53. * @return {Wireframe} A reference to this instance.
  54. */
  55. computeLineDistances() {
  56. // for backwards-compatibility, but could be a method of LineSegmentsGeometry...
  57. const geometry = this.geometry;
  58. const instanceStart = geometry.attributes.instanceStart;
  59. const instanceEnd = geometry.attributes.instanceEnd;
  60. const lineDistances = new Float32Array( 2 * instanceStart.count );
  61. for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) {
  62. _start.fromBufferAttribute( instanceStart, i );
  63. _end.fromBufferAttribute( instanceEnd, i );
  64. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  65. lineDistances[ j + 1 ] = lineDistances[ j ] + _start.distanceTo( _end );
  66. }
  67. const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  68. geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  69. geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  70. return this;
  71. }
  72. onBeforeRender( renderer ) {
  73. const uniforms = this.material.uniforms;
  74. if ( uniforms && uniforms.resolution ) {
  75. renderer.getViewport( _viewport );
  76. this.material.uniforms.resolution.value.set( _viewport.z, _viewport.w );
  77. }
  78. }
  79. }
  80. export { Wireframe };
粤ICP备19079148号