UVsDebug.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @author zz85 / http://github.com/zz85
  3. * @author WestLangley / http://github.com/WestLangley
  4. * @author Mugen87 / https://github.com/Mugen87
  5. *
  6. * tool for "unwrapping" and debugging three.js geometries UV mapping
  7. *
  8. * Sample usage:
  9. * document.body.appendChild( UVsDebug( new THREE.SphereBufferGeometry( 10, 10, 10, 10 ) );
  10. *
  11. */
  12. import {
  13. Vector2
  14. } from "../../../build/three.module.js";
  15. var UVsDebug = function ( geometry, size ) {
  16. // handles wrapping of uv.x > 1 only
  17. var abc = 'abc';
  18. var a = new Vector2();
  19. var b = new Vector2();
  20. var uvs = [
  21. new Vector2(),
  22. new Vector2(),
  23. new Vector2()
  24. ];
  25. var face = [];
  26. var canvas = document.createElement( 'canvas' );
  27. var width = size || 1024; // power of 2 required for wrapping
  28. var height = size || 1024;
  29. canvas.width = width;
  30. canvas.height = height;
  31. var ctx = canvas.getContext( '2d' );
  32. ctx.lineWidth = 2;
  33. ctx.strokeStyle = 'rgba( 0, 0, 0, 1.0 )';
  34. ctx.textAlign = 'center';
  35. // paint background white
  36. ctx.fillStyle = 'rgba( 255, 255, 255, 1.0 )';
  37. ctx.fillRect( 0, 0, width, height );
  38. if ( geometry.isGeometry ) {
  39. var faces = geometry.faces;
  40. var uvSet = geometry.faceVertexUvs[ 0 ];
  41. for ( var i = 0, il = uvSet.length; i < il; i ++ ) {
  42. var face = faces[ i ];
  43. var uv = uvSet[ i ];
  44. face[ 0 ] = face.a;
  45. face[ 1 ] = face.b;
  46. face[ 2 ] = face.c;
  47. uvs[ 0 ].copy( uv[ 0 ] );
  48. uvs[ 1 ].copy( uv[ 1 ] );
  49. uvs[ 2 ].copy( uv[ 2 ] );
  50. processFace( face, uvs, i );
  51. }
  52. } else {
  53. var index = geometry.index;
  54. var uvAttribute = geometry.attributes.uv;
  55. if ( index ) {
  56. // indexed geometry
  57. for ( var i = 0, il = index.count; i < il; i += 3 ) {
  58. face[ 0 ] = index.getX( i );
  59. face[ 1 ] = index.getX( i + 1 );
  60. face[ 2 ] = index.getX( 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. } else {
  67. // non-indexed geometry
  68. for ( var i = 0, il = uvAttribute.count; i < il; i += 3 ) {
  69. face[ 0 ] = i;
  70. face[ 1 ] = i + 1;
  71. face[ 2 ] = i + 2;
  72. uvs[ 0 ].fromBufferAttribute( uvAttribute, face[ 0 ] );
  73. uvs[ 1 ].fromBufferAttribute( uvAttribute, face[ 1 ] );
  74. uvs[ 2 ].fromBufferAttribute( uvAttribute, face[ 2 ] );
  75. processFace( face, uvs, i / 3 );
  76. }
  77. }
  78. }
  79. return canvas;
  80. function processFace( face, uvs, index ) {
  81. // draw contour of face
  82. ctx.beginPath();
  83. a.set( 0, 0 );
  84. for ( var j = 0, jl = uvs.length; j < jl; j ++ ) {
  85. var uv = uvs[ j ];
  86. a.x += uv.x;
  87. a.y += uv.y;
  88. if ( j === 0 ) {
  89. ctx.moveTo( uv.x * width, ( 1 - uv.y ) * height );
  90. } else {
  91. ctx.lineTo( uv.x * width, ( 1 - uv.y ) * height );
  92. }
  93. }
  94. ctx.closePath();
  95. ctx.stroke();
  96. // calculate center of face
  97. a.divideScalar( uvs.length );
  98. // label the face number
  99. ctx.font = '12pt Arial bold';
  100. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  101. ctx.fillText( index, a.x * width, ( 1 - a.y ) * height );
  102. if ( a.x > 0.95 ) {
  103. // wrap x // 0.95 is arbitrary
  104. ctx.fillText( index, ( a.x % 1 ) * width, ( 1 - a.y ) * height );
  105. }
  106. //
  107. ctx.font = '8pt Arial bold';
  108. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  109. // label uv edge orders
  110. for ( j = 0, jl = uvs.length; j < jl; j ++ ) {
  111. var uv = uvs[ j ];
  112. b.addVectors( a, uv ).divideScalar( 2 );
  113. var vnum = face[ j ];
  114. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  115. if ( b.x > 0.95 ) {
  116. // wrap x
  117. ctx.fillText( abc[ j ] + vnum, ( b.x % 1 ) * width, ( 1 - b.y ) * height );
  118. }
  119. }
  120. }
  121. };
  122. export { UVsDebug };