UVsDebug.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {
  2. Vector2
  3. } from 'three';
  4. /**
  5. * @module UVsDebug
  6. * @three_import import { UVsDebug } from 'three/addons/utils/UVsDebug.js';
  7. */
  8. /**
  9. * Function for "unwrapping" and debugging three.js geometries UV mapping.
  10. *
  11. * ```js
  12. * document.body.appendChild( UVsDebug( new THREE.SphereGeometry() ) );
  13. * ```
  14. *
  15. * @param {BufferGeometry} geometry - The geometry whose uv coordinates should be inspected.
  16. * @param {number} [size=1024] - The size of the debug canvas.
  17. * @return {HTMLCanvasElement} A canvas element with visualized uv coordinates.
  18. */
  19. function UVsDebug( geometry, size = 1024 ) {
  20. // handles wrapping of uv.x > 1 only
  21. const abc = 'abc';
  22. const a = new Vector2();
  23. const b = new Vector2();
  24. const uvs = [
  25. new Vector2(),
  26. new Vector2(),
  27. new Vector2()
  28. ];
  29. const face = [];
  30. const canvas = document.createElement( 'canvas' );
  31. const width = size; // power of 2 required for wrapping
  32. const height = size;
  33. canvas.width = width;
  34. canvas.height = height;
  35. const ctx = canvas.getContext( '2d' );
  36. ctx.lineWidth = 1;
  37. ctx.strokeStyle = 'rgb( 63, 63, 63 )';
  38. ctx.textAlign = 'center';
  39. // paint background white
  40. ctx.fillStyle = 'rgb( 255, 255, 255 )';
  41. ctx.fillRect( 0, 0, width, height );
  42. const index = geometry.index;
  43. const uvAttribute = geometry.attributes.uv;
  44. if ( index ) {
  45. // indexed geometry
  46. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  47. face[ 0 ] = index.getX( i );
  48. face[ 1 ] = index.getX( i + 1 );
  49. face[ 2 ] = index.getX( i + 2 );
  50. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  51. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  52. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  53. processFace( face, uvs, i / 3 );
  54. }
  55. } else {
  56. // non-indexed geometry
  57. for ( let i = 0, il = uvAttribute.count; i < il; i += 3 ) {
  58. face[ 0 ] = i;
  59. face[ 1 ] = i + 1;
  60. face[ 2 ] = i + 2;
  61. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  62. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  63. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  64. processFace( face, uvs, i / 3 );
  65. }
  66. }
  67. return canvas;
  68. function processFace( face, uvs, index ) {
  69. // draw contour of face
  70. ctx.beginPath();
  71. a.set( 0, 0 );
  72. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  73. const uv = uvs[ j ];
  74. a.x += uv.x;
  75. a.y += uv.y;
  76. if ( j === 0 ) {
  77. ctx.moveTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  78. } else {
  79. ctx.lineTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  80. }
  81. }
  82. ctx.closePath();
  83. ctx.stroke();
  84. // calculate center of face
  85. a.divideScalar( uvs.length );
  86. // label the face number
  87. ctx.font = '18px Arial';
  88. ctx.fillStyle = 'rgb( 63, 63, 63 )';
  89. ctx.fillText( index, a.x * width, ( 1 - a.y ) * height );
  90. if ( a.x > 0.95 ) {
  91. // wrap x // 0.95 is arbitrary
  92. ctx.fillText( index, ( a.x % 1 ) * width, ( 1 - a.y ) * height );
  93. }
  94. //
  95. ctx.font = '12px Arial';
  96. ctx.fillStyle = 'rgb( 191, 191, 191 )';
  97. // label uv edge orders
  98. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  99. const uv = uvs[ j ];
  100. b.addVectors( a, uv ).divideScalar( 2 );
  101. const vnum = face[ j ];
  102. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  103. if ( b.x > 0.95 ) {
  104. // wrap x
  105. ctx.fillText( abc[ j ] + vnum, ( b.x % 1 ) * width, ( 1 - b.y ) * height );
  106. }
  107. }
  108. }
  109. }
  110. export { UVsDebug };