PlaneGeometryB2.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  4. */
  5. THREE.PlaneGeometryB2 = function ( width, height, widthSegments, heightSegments ) {
  6. THREE.BufferGeometry.call( this );
  7. this.parameters = {
  8. width: width,
  9. height: height,
  10. widthSegments: widthSegments,
  11. heightSegments: heightSegments
  12. };
  13. var width_half = width / 2;
  14. var height_half = height / 2;
  15. var gridX = widthSegments || 1;
  16. var gridY = heightSegments || 1;
  17. var gridX1 = gridX + 1;
  18. var gridY1 = gridY + 1;
  19. var segment_width = width / gridX;
  20. var segment_height = height / gridY;
  21. var vertices = new Float32Array( gridX1 * gridY1 * 3 );
  22. var normals = new Float32Array( gridX1 * gridY1 * 3 );
  23. var uvs = new Float32Array( gridX1 * gridY1 * 2 );
  24. var offset = 0;
  25. var offset2 = 0;
  26. for ( var iy = 0; iy < gridY1; iy ++ ) {
  27. var y = iy * segment_height - height_half;
  28. for ( var ix = 0; ix < gridX1; ix ++ ) {
  29. var x = ix * segment_width - width_half;
  30. vertices[ offset ] = x;
  31. vertices[ offset + 1 ] = - y;
  32. normals[ offset + 2 ] = 1;
  33. uvs[ offset2 ] = ix / gridX;
  34. uvs[ offset2 + 1 ] = 1 - ( iy / gridY );
  35. offset += 3;
  36. offset2 += 2;
  37. }
  38. }
  39. offset = 0;
  40. var indices = new Uint16Array( gridX * gridY * 6 );
  41. for ( var iy = 0; iy < gridY; iy ++ ) {
  42. for ( var ix = 0; ix < gridX; ix ++ ) {
  43. var a = ix + gridX1 * iy;
  44. var b = ix + gridX1 * ( iy + 1 );
  45. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  46. var d = ( ix + 1 ) + gridX1 * iy;
  47. indices[ offset ] = a;
  48. indices[ offset + 1 ] = b;
  49. indices[ offset + 2 ] = d;
  50. indices[ offset + 3 ] = b;
  51. indices[ offset + 4 ] = c;
  52. indices[ offset + 5 ] = d;
  53. offset += 6;
  54. }
  55. }
  56. this.attributes[ 'index' ] = { array: indices, itemSize: 1 };
  57. this.attributes[ 'position' ] = { array: vertices, itemSize: 3 };
  58. this.attributes[ 'normal' ] = { array: normals, itemSize: 3 };
  59. this.attributes[ 'uv' ] = { array: uvs, itemSize: 2 };
  60. this.vertices = [];
  61. this.normals = [];
  62. this.uvs = [];
  63. for ( var i = 0, l = vertices.length / 3; i < l; i ++ ) {
  64. this.vertices.push( new THREE.TypedVector3( vertices, i * 3 ) );
  65. this.normals.push( new THREE.TypedVector3( normals, i * 3 ) );
  66. this.uvs.push( new THREE.TypedVector2( uvs, i * 2 ) );
  67. }
  68. };
  69. THREE.PlaneGeometryB2.prototype = Object.create( THREE.BufferGeometry.prototype );
  70. //
  71. THREE.TypedVector2 = function ( array, offset ) {
  72. this.array = array;
  73. this.offset = offset;
  74. };
  75. THREE.TypedVector2.prototype = Object.create( THREE.Vector2.prototype );
  76. Object.defineProperties( THREE.TypedVector2.prototype, {
  77. 'x': {
  78. get: function () { return this.array[ this.offset ]; },
  79. set: function ( v ) { this.array[ this.offset ] = v; }
  80. },
  81. 'y': {
  82. get: function () { return this.array[ this.offset + 1 ]; },
  83. set: function ( v ) { this.array[ this.offset + 1 ] = v; }
  84. }
  85. } );
  86. THREE.TypedVector3 = function ( array, offset ) {
  87. this.array = array;
  88. this.offset = offset;
  89. };
  90. THREE.TypedVector3.prototype = Object.create( THREE.Vector3.prototype );
  91. Object.defineProperties( THREE.TypedVector3.prototype, {
  92. 'x': {
  93. get: function () { return this.array[ this.offset ]; },
  94. set: function ( v ) { this.array[ this.offset ] = v; }
  95. },
  96. 'y': {
  97. get: function () { return this.array[ this.offset + 1 ]; },
  98. set: function ( v ) { this.array[ this.offset + 1 ] = v; }
  99. },
  100. 'z': {
  101. get: function () { return this.array[ this.offset + 2 ]; },
  102. set: function ( v ) { this.array[ this.offset + 2 ] = v; }
  103. }
  104. } );