UVsUtils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * @author gyuque / http://github.com/gyuque
  3. *
  4. * Cylinder Mapping for ExtrudeGeometry
  5. *
  6. */
  7. THREE.UVsUtils = {
  8. };
  9. THREE.UVsUtils.CylinderUVGenerator = function() {
  10. this.uRepeat = 1;
  11. this.targetGeometry = null;
  12. this.lengthCache = null;
  13. };
  14. THREE.UVsUtils.CylinderUVGenerator.prototype = {
  15. generateTopUV: THREE.ExtrudeGeometry.WorldUVGenerator.generateTopUV,
  16. generateBottomUV: THREE.ExtrudeGeometry.WorldUVGenerator.generateBottomUV,
  17. generateSideWallUV: function( geometry, extrudedShape, wallContour, extrudeOptions,
  18. indexA, indexB, indexC, indexD, stepIndex, stepsLength,
  19. contourIndex1, contourIndex2 ) {
  20. // first call
  21. if (this.targetGeometry !== geometry) {
  22. this.prepare(geometry, wallContour);
  23. }
  24. // generate uv
  25. var u_list = this.lengthCache;
  26. var v1 = stepIndex / stepsLength;
  27. var v2 = ( stepIndex + 1 ) / stepsLength;
  28. var u1 = u_list[contourIndex1];
  29. var u2 = u_list[contourIndex2];
  30. if (u1 < u2) {u1 += 1.0;}
  31. u1 *= this.uRepeat;
  32. u2 *= this.uRepeat;
  33. return [
  34. new THREE.Vector2( u1, v1 ),
  35. new THREE.Vector2( u2, v1 ),
  36. new THREE.Vector2( u2, v2 ),
  37. new THREE.Vector2( u1, v2 )
  38. ];
  39. },
  40. prepare: function(geometry, wallContour) {
  41. var p1, p2;
  42. var u_list = [];
  43. var lengthSum = 0;
  44. var len = wallContour.length;
  45. for (var i = 0;i < len;i++) {
  46. p1 = wallContour[ i ];
  47. p2 = wallContour[ (i+1) % len ];
  48. var dx = p1.x - p2.x;
  49. var dy = p1.y - p2.y;
  50. var segmentLength = Math.sqrt(dx*dx + dy*dy);
  51. u_list.push(lengthSum);
  52. lengthSum += segmentLength;
  53. }
  54. this.normalizeArray(u_list, lengthSum);
  55. this.targetGeometry = geometry;
  56. this.lengthCache = u_list;
  57. },
  58. normalizeArray: function(ls, v) {
  59. var len = ls.length;
  60. for (var i = 0;i < len;i++) {
  61. ls[i] /= v;
  62. }
  63. return ls;
  64. }
  65. };
  66. /*
  67. * @author zz85 / http://github.com/zz85
  68. * @author WestLangley / http://github.com/WestLangley
  69. *
  70. * tool for "unwrapping" and debugging three.js
  71. * geometries UV mapping
  72. *
  73. * Sample usage:
  74. * document.body.appendChild(
  75. * THREE.UVsDebug(
  76. * new THREE.SphereGeometry(10,10,10,10));
  77. *
  78. */
  79. THREE.UVsDebug = function( geometry, size ) {
  80. // handles wrapping of uv.x > 1 only
  81. var abc = 'abcd';
  82. var uv, u, ax, ay;
  83. var i, il, j, jl;
  84. var vnum;
  85. var a = new THREE.Vector2();
  86. var b = new THREE.Vector2();
  87. var faces = geometry.faces;
  88. var uvs = geometry.faceVertexUvs[ 0 ];
  89. var canvas = document.createElement( 'canvas' );
  90. var width = size || 1024; // power of 2 required for wrapping
  91. var height = size || 1024;
  92. canvas.width = width;
  93. canvas.height = height;
  94. var ctx = canvas.getContext( '2d' );
  95. ctx.lineWidth = 2;
  96. ctx.strokeStyle = 'rgba( 0, 0, 0, 1.0 )';
  97. ctx.textAlign = 'center';
  98. // paint background white
  99. ctx.fillStyle = 'rgba( 255, 255, 255, 1.0 )';
  100. ctx.fillRect( 0, 0, width, height );
  101. for ( i = 0, il = uvs.length; i < il; i++ ) {
  102. uv = uvs[ i ];
  103. // draw lines
  104. ctx.beginPath();
  105. a.set( 0, 0 );
  106. for ( j = 0, jl = uv.length; j < jl; j++ ) {
  107. u = uv[ j ];
  108. a.x += u.x;
  109. a.y += u.y;
  110. if ( j == 0 ) {
  111. ctx.moveTo( u.x * width, ( 1 - u.y ) * height );
  112. } else {
  113. ctx.lineTo( u.x * width, ( 1 - u.y ) * height );
  114. }
  115. }
  116. ctx.closePath();
  117. ctx.stroke();
  118. a.divideScalar( jl );
  119. // label the face number
  120. ctx.font = "12pt Arial bold";
  121. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  122. ctx.fillText( i, a.x * width, ( 1 - a.y ) * height );
  123. if ( a.x > 0.95 ) { // wrap x // 0.95 is arbitrary
  124. ctx.fillText( i, ( a.x % 1 ) * width, ( 1 - a.y ) * height );
  125. }
  126. ctx.font = "8pt Arial bold";
  127. ctx.fillStyle = 'rgba( 0, 0, 0, 1.0 )';
  128. // label uv edge orders
  129. for ( j = 0, jl = uv.length; j < jl; j++ ) {
  130. u = uv[ j ];
  131. b.addVectors( a, u ).divideScalar( 2 );
  132. vnum = faces[ i ][ abc[ j ] ];
  133. ctx.fillText( abc[ j ] + vnum, b.x * width, ( 1 - b.y ) * height );
  134. if ( b.x > 0.95 ) { // wrap x
  135. ctx.fillText( abc[ j ] + vnum, ( b.x % 1 ) * width, ( 1 - b.y ) * height );
  136. }
  137. }
  138. }
  139. return canvas;
  140. }