UVsDebug.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ( function () {
  2. /**
  3. * tool for "unwrapping" and debugging three.js geometries UV mapping
  4. *
  5. * Sample usage:
  6. * document.body.appendChild( UVsDebug( new THREE.SphereGeometry( 10, 10, 10, 10 ) );
  7. *
  8. */
  9. function UVsDebug( geometry, size = 1024 ) {
  10. // handles wrapping of uv.x > 1 only
  11. const abc = 'abc';
  12. const a = new THREE.Vector2();
  13. const b = new THREE.Vector2();
  14. const uvs = [ new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() ];
  15. const face = [];
  16. const canvas = document.createElement( 'canvas' );
  17. const width = size; // power of 2 required for wrapping
  18. const height = size;
  19. canvas.width = width;
  20. canvas.height = height;
  21. const ctx = canvas.getContext( '2d' );
  22. ctx.lineWidth = 1;
  23. ctx.strokeStyle = 'rgb( 63, 63, 63 )';
  24. ctx.textAlign = 'center'; // paint background white
  25. ctx.fillStyle = 'rgb( 255, 255, 255 )';
  26. ctx.fillRect( 0, 0, width, height );
  27. if ( geometry.isGeometry ) {
  28. console.error( 'THREE.UVsDebug no longer supports Geometry. Use THREE.BufferGeometry instead.' );
  29. return;
  30. } else {
  31. const index = geometry.index;
  32. const uvAttribute = geometry.attributes.uv;
  33. if ( index ) {
  34. // indexed geometry
  35. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  36. face[ 0 ] = index.getX( i );
  37. face[ 1 ] = index.getX( i + 1 );
  38. face[ 2 ] = index.getX( i + 2 );
  39. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  40. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  41. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  42. processFace( face, uvs, i / 3 );
  43. }
  44. } else {
  45. // non-indexed geometry
  46. for ( let i = 0, il = uvAttribute.count; i < il; i += 3 ) {
  47. face[ 0 ] = i;
  48. face[ 1 ] = i + 1;
  49. face[ 2 ] = 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. }
  56. }
  57. return canvas;
  58. function processFace( face, uvs, index ) {
  59. // draw contour of face
  60. ctx.beginPath();
  61. a.set( 0, 0 );
  62. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  63. const uv = uvs[ j ];
  64. a.x += uv.x;
  65. a.y += uv.y;
  66. if ( j === 0 ) {
  67. ctx.moveTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  68. } else {
  69. ctx.lineTo( uv.x * ( width - 2 ) + 0.5, ( 1 - uv.y ) * ( height - 2 ) + 0.5 );
  70. }
  71. }
  72. ctx.closePath();
  73. ctx.stroke(); // calculate center of face
  74. a.divideScalar( uvs.length ); // label the face number
  75. ctx.font = '18px Arial';
  76. ctx.fillStyle = 'rgb( 63, 63, 63 )';
  77. ctx.fillText( index, a.x * width, ( 1 - a.y ) * height );
  78. if ( a.x > 0.95 ) {
  79. // wrap x // 0.95 is arbitrary
  80. ctx.fillText( index, a.x % 1 * width, ( 1 - a.y ) * height );
  81. } //
  82. ctx.font = '12px Arial';
  83. ctx.fillStyle = 'rgb( 191, 191, 191 )'; // label uv edge orders
  84. for ( let j = 0, jl = uvs.length; j < jl; j ++ ) {
  85. const uv = uvs[ j ];
  86. b.addVectors( a, uv ).divideScalar( 2 );
  87. const vnum = face[ j ];
  88. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  89. if ( b.x > 0.95 ) {
  90. // wrap x
  91. ctx.fillText( abc[ j ] + vnum, b.x % 1 * width, ( 1 - b.y ) * height );
  92. }
  93. }
  94. }
  95. }
  96. THREE.UVsDebug = UVsDebug;
  97. } )();