Polygon.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package h3d.prim;
  2. import h3d.col.Point;
  3. class Polygon extends Primitive {
  4. public var points : Array<Point>;
  5. public var normals : Array<Point>;
  6. public var uvs : Array<UV>;
  7. public var idx : hxd.IndexBuffer;
  8. public var colors : Array<Point>;
  9. public function new( points, ?idx ) {
  10. this.points = points;
  11. this.idx = idx;
  12. }
  13. override function getBounds() {
  14. var b = new h3d.col.Bounds();
  15. for( p in points )
  16. b.addPoint(p);
  17. return b;
  18. }
  19. override function alloc( engine : h3d.Engine ) {
  20. dispose();
  21. var size = 3;
  22. if( normals != null )
  23. size += 3;
  24. if( uvs != null )
  25. size += 2;
  26. if( colors != null )
  27. size += 3;
  28. var buf = new hxd.FloatBuffer();
  29. for( k in 0...points.length ) {
  30. var p = points[k];
  31. buf.push(p.x);
  32. buf.push(p.y);
  33. buf.push(p.z);
  34. if( uvs != null ) {
  35. var t = uvs[k];
  36. buf.push(t.u);
  37. buf.push(t.v);
  38. }
  39. if( normals != null ) {
  40. var n = normals[k];
  41. buf.push(n.x);
  42. buf.push(n.y);
  43. buf.push(n.z);
  44. }
  45. if( colors != null ) {
  46. var c = colors[k];
  47. buf.push(c.x);
  48. buf.push(c.y);
  49. buf.push(c.z);
  50. }
  51. }
  52. buffer = h3d.Buffer.ofFloats(buf, size, idx == null ? [Triangles] : null);
  53. if( idx != null )
  54. indexes = h3d.Indexes.alloc(idx);
  55. }
  56. public function unindex() {
  57. if( idx != null && points.length != idx.length ) {
  58. var p = [];
  59. var used = [];
  60. for( i in 0...idx.length )
  61. p.push(points[idx[i]].clone());
  62. if( normals != null ) {
  63. var n = [];
  64. for( i in 0...idx.length )
  65. n.push(normals[idx[i]].clone());
  66. normals = n;
  67. }
  68. if( colors != null ) {
  69. var n = [];
  70. for( i in 0...idx.length )
  71. n.push(colors[idx[i]].clone());
  72. colors = n;
  73. }
  74. if( uvs != null ) {
  75. var t = [];
  76. for( i in 0...idx.length )
  77. t.push(uvs[idx[i]].clone());
  78. uvs = t;
  79. }
  80. points = p;
  81. idx = null;
  82. }
  83. }
  84. public function translate( dx, dy, dz ) {
  85. for( p in points ) {
  86. p.x += dx;
  87. p.y += dy;
  88. p.z += dz;
  89. }
  90. }
  91. public function scale( s : Float ) {
  92. for( p in points ) {
  93. p.x *= s;
  94. p.y *= s;
  95. p.z *= s;
  96. }
  97. }
  98. public function addNormals() {
  99. // make per-point normal
  100. normals = new Array();
  101. for( i in 0...points.length )
  102. normals[i] = new Point();
  103. var pos = 0;
  104. for( i in 0...triCount() ) {
  105. var i0, i1, i2;
  106. if( idx == null ) {
  107. i0 = pos++;
  108. i1 = pos++;
  109. i2 = pos++;
  110. } else {
  111. i0 = idx[pos++];
  112. i1 = idx[pos++];
  113. i2 = idx[pos++];
  114. }
  115. var p0 = points[i0];
  116. var p1 = points[i1];
  117. var p2 = points[i2];
  118. // this is the per-face normal
  119. var n = p1.sub(p0).cross(p2.sub(p0));
  120. // add it to each point
  121. normals[i0].x += n.x; normals[i0].y += n.y; normals[i0].z += n.z;
  122. normals[i1].x += n.x; normals[i1].y += n.y; normals[i1].z += n.z;
  123. normals[i2].x += n.x; normals[i2].y += n.y; normals[i2].z += n.z;
  124. }
  125. // normalize all normals
  126. for( n in normals )
  127. n.normalize();
  128. }
  129. public function addUVs() {
  130. throw "Not implemented for this polygon";
  131. }
  132. public function uvScale( su : Float, sv : Float ) {
  133. if( uvs == null )
  134. throw "Missing UVs";
  135. var m = new Map<UV,Bool>();
  136. for( t in uvs ) {
  137. if( m.exists(t) ) continue;
  138. m.set(t, true);
  139. t.u *= su;
  140. t.v *= sv;
  141. }
  142. }
  143. public override function triCount() {
  144. var n = super.triCount();
  145. if( n != 0 )
  146. return n;
  147. return Std.int((idx == null ? points.length : idx.length) / 3);
  148. }
  149. }