UVsUtils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * @author gyuque / https://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 / https://github.com/zz85
  68. *
  69. * tool for "unwrapping" and debugging three.js
  70. * geometries UV mapping
  71. *
  72. * Sample usage:
  73. * document.body.appendChild(
  74. * THREE.UVsDebug(
  75. * new THREE.SphereGeometry(10,10,10,10));
  76. *
  77. */
  78. THREE.UVsDebug = function(geometry) {
  79. var verts = geometry.vertices,
  80. faces = geometry.faces,
  81. uvs = geometry.faceVertexUvs[0];
  82. console.log('debugging geometry', geometry);
  83. var canvas = document.createElement('canvas');
  84. var width = 1024;
  85. var height = 1024;
  86. canvas.width = width;
  87. canvas.height = height;
  88. var ctx = canvas.getContext('2d');
  89. ctx.lineWidth = 1;
  90. ctx.strokeStyle = 'rgba(0,0,0,0.8)';
  91. // paint background white
  92. ctx.fillStyle = 'rgba(255,255,255, 1.0)';
  93. ctx.fillRect(0, 0, width, height);
  94. var abc = 'abcd';
  95. var uv, u, ax, ay;
  96. var i, il, j, jl;
  97. var a = new THREE.Vector2();
  98. var b = new THREE.Vector2();
  99. for (i = 0, il = uvs.length; i < il; i++) {
  100. uv = uvs[i];
  101. // draw lines
  102. ctx.beginPath();
  103. a.set(0, 0);
  104. for (j = 0, jl = uv.length; j < jl; j++) {
  105. u = uv[j];
  106. a.x += u.x;
  107. a.y += u.y;
  108. if (j == 0) {
  109. ctx.moveTo(u.x * width, u.y * height);
  110. } else {
  111. ctx.lineTo(u.x * width, u.y * height);
  112. }
  113. }
  114. ctx.closePath();
  115. ctx.stroke();
  116. a.divideScalar(jl);
  117. // label the face number
  118. ctx.font = "12pt Arial bold";
  119. ctx.fillStyle = 'rgba(0,0,0,0.8)';
  120. ctx.fillText(i, a.x * width, a.y * height);
  121. ctx.font = "8pt Arial bold";
  122. ctx.fillStyle = 'rgba(30,30,0,0.8)';
  123. // label uv edge orders
  124. for (j = 0, jl = uv.length; j < jl; j++) {
  125. u = uv[j];
  126. b.set(u.x, u.y).subSelf(a).divideScalar(4);
  127. b.x = u.x - b.x;
  128. b.y = u.y - b.y;
  129. ctx.fillText(abc[j]
  130. + ':' + faces[i][abc[j]], b.x * width, b.y * height);
  131. }
  132. }
  133. return canvas;
  134. }