BufferAttribute.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @author simonThiele / https://github.com/simonThiele
  3. */
  4. module( "BufferAttribute" );
  5. test( "count", function() {
  6. ok(
  7. new THREE.BufferAttribute( new Float32Array( [1, 2, 3, 4, 5, 6] ), 3 ).count === 2,
  8. 'count is equal to the number of chunks'
  9. );
  10. });
  11. test( "copy", function() {
  12. var attr = new THREE.BufferAttribute( new Float32Array( [1, 2, 3, 4, 5, 6] ), 3 );
  13. attr.setDynamic( true );
  14. attr.needsUpdate = true;
  15. var attrCopy = new THREE.BufferAttribute().copy( attr );
  16. ok( attr.count === attrCopy.count, 'count is equal' );
  17. ok( attr.itemSize === attrCopy.itemSize, 'itemSize is equal' );
  18. ok( attr.dynamic === attrCopy.dynamic, 'dynamic is equal' );
  19. ok( attr.array.length === attrCopy.array.length, 'array length is equal' );
  20. ok( attr.version === 1 && attrCopy.version === 0, 'version is not copied which is good' );
  21. });
  22. test( "copyAt", function() {
  23. var attr = new THREE.BufferAttribute( new Float32Array( [1, 2, 3, 4, 5, 6, 7, 8, 9] ), 3 );
  24. var attr2 = new THREE.BufferAttribute( new Float32Array(9), 3 );
  25. attr2.copyAt( 1, attr, 2 );
  26. attr2.copyAt( 0, attr, 1 );
  27. attr2.copyAt( 2, attr, 0 );
  28. var i = attr.array;
  29. var i2 = attr2.array; // should be [4, 5, 6, 7, 8, 9, 1, 2, 3]
  30. ok( i2[0] === i[3] && i2[1] === i[4] && i2[2] === i[5], 'chunck copied to correct place' );
  31. ok( i2[3] === i[6] && i2[4] === i[7] && i2[5] === i[8], 'chunck copied to correct place' );
  32. ok( i2[6] === i[0] && i2[7] === i[1] && i2[8] === i[2], 'chunck copied to correct place' );
  33. });
  34. test( "copyColorsArray", function() {
  35. var attr = new THREE.BufferAttribute( new Float32Array(6), 3 );
  36. attr.copyColorsArray( [
  37. new THREE.Color( 0, 0.5, 1 ),
  38. new THREE.Color( 0.25, 1, 0 )
  39. ]);
  40. var i = attr.array;
  41. ok( i[0] === 0 && i[1] === 0.5 && i[2] === 1, 'first color was copied correctly' );
  42. ok( i[3] === 0.25 && i[4] === 1 && i[5] === 0, 'second color was copied correctly' );
  43. });
  44. test( "copyIndicesArray", function() {
  45. var attr = new THREE.BufferAttribute( new Float32Array(6), 3 );
  46. attr.copyIndicesArray( [
  47. {a: 1, b: 2, c: 3},
  48. {a: 4, b: 5, c: 6}
  49. ]);
  50. var i = attr.array;
  51. ok( i[0] === 1 && i[1] === 2 && i[2] === 3, 'first indices were copied correctly' );
  52. ok( i[3] === 4 && i[4] === 5 && i[5] === 6, 'second indices were copied correctly' );
  53. });
  54. test( "copyVector2sArray", function() {
  55. var attr = new THREE.BufferAttribute( new Float32Array(4), 2 );
  56. attr.copyVector2sArray( [
  57. new THREE.Vector2(1, 2),
  58. new THREE.Vector2(4, 5)
  59. ]);
  60. var i = attr.array;
  61. ok( i[0] === 1 && i[1] === 2, 'first vector was copied correctly' );
  62. ok( i[2] === 4 && i[3] === 5, 'second vector was copied correctly' );
  63. });
  64. test( "copyVector3sArray", function() {
  65. var attr = new THREE.BufferAttribute( new Float32Array(6), 2 );
  66. attr.copyVector3sArray( [
  67. new THREE.Vector3(1, 2, 3),
  68. new THREE.Vector3(10, 20, 30)
  69. ]);
  70. var i = attr.array;
  71. ok( i[0] === 1 && i[1] === 2 && i[2] === 3, 'first vector was copied correctly' );
  72. ok( i[3] === 10 && i[4] === 20 && i[5] === 30, 'second vector was copied correctly' );
  73. });
  74. test( "copyVector4sArray", function() {
  75. var attr = new THREE.BufferAttribute( new Float32Array(8), 2 );
  76. attr.copyVector4sArray( [
  77. new THREE.Vector4(1, 2, 3, 4),
  78. new THREE.Vector4(10, 20, 30, 40)
  79. ]);
  80. var i = attr.array;
  81. ok( i[0] === 1 && i[1] === 2 && i[2] === 3 && i[3] === 4, 'first vector was copied correctly' );
  82. ok( i[4] === 10 && i[5] === 20 && i[6] === 30 && i[7] === 40, 'second vector was copied correctly' );
  83. });
  84. test( "clone", function() {
  85. var attr = new THREE.BufferAttribute( new Float32Array([1, 2, 3, 4, 0.12, -12]), 2 );
  86. var attrCopy = attr.clone();
  87. ok( attr.array.length === attrCopy.array.length, 'attribute was cloned' );
  88. for ( var i = 0; i < attr.array.length; i++ ) {
  89. ok( attr.array[i] === attrCopy.array[i], 'array item is equal' );
  90. }
  91. });