vec4.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008, Luke Benstead.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. cc.kmVec4 = function (x, y, z, w) {
  24. this.x = x || 0;
  25. this.y = y || 0;
  26. this.z = z || 0;
  27. this.w = w || 0;
  28. };
  29. cc.kmVec4Fill = function(outVec, x, y ,z, w){
  30. outVec.x = x;
  31. outVec.y = y;
  32. outVec.z = z;
  33. outVec.w = w;
  34. return outVec;
  35. };
  36. cc.kmVec4Add = function(outVec, pV1, pV2){
  37. outVec.x = pV1.x + pV2.x;
  38. outVec.y = pV1.y + pV2.y;
  39. outVec.z = pV1.z + pV2.z;
  40. outVec.w = pV1.w + pV2.w;
  41. return outVec;
  42. };
  43. cc.kmVec4Dot = function( vec1, vec2){
  44. return ( vec1.x * vec2.x
  45. + vec1.y * vec2.y
  46. + vec1.z * vec2.z
  47. + vec1.w * vec2.w );
  48. };
  49. cc.kmVec4Length = function(inVec){
  50. return Math.sqrt(cc.kmSQR(inVec.x) + cc.kmSQR(inVec.y) + cc.kmSQR(inVec.z) + cc.kmSQR(inVec.w));
  51. };
  52. cc.kmVec4LengthSq = function(inVec){
  53. return cc.kmSQR(inVec.x) + cc.kmSQR(inVec.y) + cc.kmSQR(inVec.z) + cc.kmSQR(inVec.w);
  54. };
  55. cc.kmVec4Lerp = function(outVec, pV1, pV2, t){
  56. return outVec;
  57. };
  58. cc.kmVec4Normalize = function(outVec, inVec){
  59. var l = 1.0 / cc.kmVec4Length(inVec);
  60. outVec.x *= l;
  61. outVec.y *= l;
  62. outVec.z *= l;
  63. outVec.w *= l;
  64. return outVec;
  65. };
  66. cc.kmVec4Scale = function(outVec, inVec, scale){
  67. cc.kmVec4Normalize(outVec, inVec);
  68. outVec.x *= scale;
  69. outVec.y *= scale;
  70. outVec.z *= scale;
  71. outVec.w *= scale;
  72. return outVec;
  73. };
  74. cc.kmVec4Subtract = function(outVec,vec1, vec2){
  75. outVec.x = vec1.x - vec2.x;
  76. outVec.y = vec1.y - vec2.y;
  77. outVec.z = vec1.z - vec2.z;
  78. outVec.w = vec1.w - vec2.w;
  79. return outVec;
  80. };
  81. cc.kmVec4Transform = function(outVec, vec,mat4Obj){
  82. outVec.x = vec.x * mat4Obj.mat[0] + vec.y * mat4Obj.mat[4] + vec.z * mat4Obj.mat[8] + vec.w * mat4Obj.mat[12];
  83. outVec.y = vec.x * mat4Obj.mat[1] + vec.y * mat4Obj.mat[5] + vec.z * mat4Obj.mat[9] + vec.w * mat4Obj.mat[13];
  84. outVec.z = vec.x * mat4Obj.mat[2] + vec.y * mat4Obj.mat[6] + vec.z * mat4Obj.mat[10] + vec.w * mat4Obj.mat[14];
  85. outVec.w = vec.x * mat4Obj.mat[3] + vec.y * mat4Obj.mat[7] + vec.z * mat4Obj.mat[11] + vec.w * mat4Obj.mat[15];
  86. return outVec;
  87. };
  88. cc.kmVec4TransformArray = function(outVec,outStride,vecObj,stride,mat4Obj,count){
  89. var i = 0;
  90. //Go through all of the vectors
  91. while (i < count) {
  92. var currIn = vecObj + (i * stride); //Get a pointer to the current input
  93. var out = outVec + (i * outStride); //and the current output
  94. cc.kmVec4Transform(out, currIn, mat4Obj); //Perform transform on it
  95. ++i;
  96. }
  97. return outVec;
  98. };
  99. cc.kmVec4AreEqual = function(vec1,vec2){
  100. return (
  101. (vec1.x < vec2.x + cc.kmEpsilon && vec1.x > vec2.x - cc.kmEpsilon) &&
  102. (vec1.y < vec2.y + cc.kmEpsilon && vec1.y > vec2.y - cc.kmEpsilon) &&
  103. (vec1.z < vec2.z + cc.kmEpsilon && vec1.z > vec2.z - cc.kmEpsilon) &&
  104. (vec1.w < vec2.w + cc.kmEpsilon && vec1.w > vec2.w - cc.kmEpsilon)
  105. );
  106. };
  107. cc.kmVec4Assign = function(destVec, srcVec){
  108. if(destVec == srcVec){
  109. cc.log("destVec and srcVec are same object");
  110. return destVec;
  111. }
  112. destVec.x = srcVec.x;
  113. destVec.y = srcVec.y;
  114. destVec.z = srcVec.z;
  115. destVec.w = srcVec.w;
  116. return destVec;
  117. };
  118. cc.kmVec4ToTypeArray = function(vecValue){
  119. if(!vecValue)
  120. return null;
  121. var tyArr = new Float32Array(4);
  122. tyArr[0] = vecValue.x;
  123. tyArr[1] = vecValue.y;
  124. tyArr[2] = vecValue.z;
  125. tyArr[3] = vecValue.w;
  126. return tyArr;
  127. };