PlaneGeometry.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import { Geometry } from '../core/Geometry';
  6. function PlaneGeometry( width, height, widthSegments, heightSegments ) {
  7. Geometry.call( this );
  8. this.type = 'PlaneGeometry';
  9. this.parameters = {
  10. width: width,
  11. height: height,
  12. widthSegments: widthSegments,
  13. heightSegments: heightSegments
  14. };
  15. this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
  16. }
  17. PlaneGeometry.prototype = Object.create( Geometry.prototype );
  18. PlaneGeometry.prototype.constructor = PlaneGeometry;
  19. /**
  20. * @author mrdoob / http://mrdoob.com/
  21. * @author Mugen87 / https://github.com/Mugen87
  22. *
  23. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  24. */
  25. import { Float32BufferAttribute } from '../core/BufferAttribute';
  26. import { BufferGeometry } from '../core/BufferGeometry';
  27. function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
  28. BufferGeometry.call( this );
  29. this.type = 'PlaneBufferGeometry';
  30. this.parameters = {
  31. width: width,
  32. height: height,
  33. widthSegments: widthSegments,
  34. heightSegments: heightSegments
  35. };
  36. var width_half = width / 2;
  37. var height_half = height / 2;
  38. var gridX = Math.floor( widthSegments ) || 1;
  39. var gridY = Math.floor( heightSegments ) || 1;
  40. var gridX1 = gridX + 1;
  41. var gridY1 = gridY + 1;
  42. var segment_width = width / gridX;
  43. var segment_height = height / gridY;
  44. var ix, iy;
  45. // buffers
  46. var indices = [];
  47. var vertices = [];
  48. var normals = [];
  49. var uvs = [];
  50. // generate vertices, normals and uvs
  51. for ( iy = 0; iy < gridY1; iy ++ ) {
  52. var y = iy * segment_height - height_half;
  53. for ( ix = 0; ix < gridX1; ix ++ ) {
  54. var x = ix * segment_width - width_half;
  55. vertices.push( x, - y, 0 );
  56. normals.push( 0, 0, 1 );
  57. uvs.push( ix / gridX );
  58. uvs.push( 1 - ( iy / gridY ) );
  59. }
  60. }
  61. // indices
  62. for ( iy = 0; iy < gridY; iy ++ ) {
  63. for ( ix = 0; ix < gridX; ix ++ ) {
  64. var a = ix + gridX1 * iy;
  65. var b = ix + gridX1 * ( iy + 1 );
  66. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  67. var d = ( ix + 1 ) + gridX1 * iy;
  68. // faces
  69. indices.push( a, b, d );
  70. indices.push( b, c, d );
  71. }
  72. }
  73. // build geometry
  74. this.setIndexArray( indices );
  75. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  76. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  77. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  78. }
  79. PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  80. PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
  81. export { PlaneGeometry, PlaneBufferGeometry };