/web/threejs/3DGrapher_files/generator.js

https://github.com/PatMartin/Dex · JavaScript · 207 lines · 189 code · 6 blank · 12 comment · 56 complexity · baa7d5326a5701f44c7382007bf52e47 MD5 · raw file

  1. function addFace(geom,p1,p2,p3,color) {
  2. return geom.faces.push(new THREE.Face3(p1,p2,p3/*,new THREE.Vector3(0,1,0),null,geom.materials.push(new THREE.MeshBasicMaterial({color: color}))-1*/));
  3. }
  4. function addFace4(geom,p1,p2,p3,p4,color) {
  5. return geom.faces.push(new THREE.Face4(p1,p2,p3,p4/*,new THREE.Vector3(0,1,0),null,geom.materials.push(new THREE.MeshBasicMaterial({color: color}))-1*/));
  6. }
  7. function addTriangles(geom,triangles)
  8. {
  9. var len = triangles.length, i, ti;
  10. for(i=0; i < len; i++)
  11. {
  12. ti = triangles[i];
  13. addFace(geom,ti.p1,ti.p2,ti.p3,ti.color);
  14. }
  15. }
  16. function szescian(bok, color, material)
  17. {
  18. var tmp = bok / 2;
  19. var geom = new THREE.Geometry();
  20. geom.vertices = [
  21. new THREE.Vector3(-tmp, -tmp, -tmp),
  22. new THREE.Vector3(-tmp, -tmp, tmp),
  23. new THREE.Vector3(-tmp, tmp, -tmp),
  24. new THREE.Vector3(-tmp, tmp, tmp),
  25. new THREE.Vector3(tmp, -tmp, -tmp),
  26. new THREE.Vector3(tmp, -tmp, tmp),
  27. new THREE.Vector3(tmp, tmp, -tmp),
  28. new THREE.Vector3(tmp, tmp, tmp)
  29. ];
  30. addTriangles(geom, [
  31. {p1: 0, p2: 2, p3: 4, color: color},//dolna1
  32. {p1: 2, p2: 4, p3: 6, color: color},//dolna2
  33. {p1: 1, p2: 3, p3: 5, color: color},//gorna1
  34. {p1: 3, p2: 5, p3: 7, color: color},//gorna2
  35. {p1: 0, p2: 1, p3: 2, color: color},//lewa1
  36. {p1: 1, p2: 2, p3: 3, color: color},//lewa2
  37. {p1: 6, p2: 5, p3: 7, color: color},//prawa1
  38. {p1: 6, p2: 5, p3: 4, color: color},//prawa2
  39. {p1: 0, p2: 1, p3: 4, color: color},//przednia1
  40. {p1: 1, p2: 4, p3: 5, color: color},//przednia2
  41. {p1: 3, p2: 2, p3: 6, color: color},//tylna1
  42. {p1: 3, p2: 6, p3: 7, color: color}//tylna2
  43. ]);
  44. geom.computeFaceNormals();
  45. var obj = new THREE.Mesh(geom,new THREE.MeshFaceMaterial());
  46. obj.doubleSided = true;
  47. return obj;
  48. }
  49. function plot(f,range,step)
  50. {
  51. var geom = new THREE.Geometry();
  52. var x, y, z, xi = 0, yi = 0, c, maxi = (range*2)/step;
  53. for (x = -range; x <= range; x += step)
  54. {
  55. for (y = -range; y <= range; y += step)
  56. {
  57. ci = yi + xi* maxi;
  58. if (!f)
  59. {
  60. z = 0;
  61. }
  62. else
  63. {
  64. z = f(x, y);
  65. if (isNaN(z) || (z == Number.NEGATIVE_INFINITY) ||
  66. (z == Number.POSITIVE_INFINITY) || (Math.abs(z) > 2e5) || !isFinite(z)) {
  67. z = Number.NaN;
  68. }
  69. }
  70. geom.vertices.push(new THREE.Vector3(x*50 , y*50 , z*50 ));
  71. if (y > -range && x > -range)
  72. {
  73. c = '0xFFFFFF';
  74. //addFace(geom,ci-1,ci,ci-maxi,c);
  75. //addFace(geom,ci-1,ci-maxi-1,ci-maxi,c);
  76. addFace4(geom,ci-1,ci,ci-maxi,ci-maxi-1,c);
  77. geom.faceVertexUvs[ 0 ].push( [
  78. new THREE.UV( xi / maxi, yi / maxi ),
  79. new THREE.UV( xi / maxi, ( yi + 1 ) / maxi ),
  80. new THREE.UV( ( xi + 1 ) / maxi, ( yi + 1 ) / maxi ),
  81. new THREE.UV( ( xi + 1 ) / maxi, yi / maxi )
  82. ] );
  83. }
  84. yi += 1;
  85. }
  86. xi += 1;
  87. yi = 0;
  88. }
  89. geom.computeFaceNormals();
  90. return geom;
  91. }
  92. function plot_parametric(iterations,xtu,ytu,ztu,range1_1,range1_2,range2_1,range2_2)
  93. {
  94. var geom = new THREE.Geometry();
  95. step1=Math.abs(range1_2-range1_1)/iterations, step2=Math.abs(range2_2-range2_1)/iterations;
  96. var xi = 0, yi = 0, c, max_yi = iterations, max_xi = iterations;
  97. for (t = range1_1; t <= range1_2; t += step1)
  98. {
  99. yi = 0;
  100. for (u = range2_1; u <= range2_2; u += step2)
  101. {
  102. ci = yi + xi* (max_yi+1);
  103. x = xtu(t,u);
  104. y = ytu(t,u);
  105. z = ztu(t,u);
  106. if (isNaN(x) || (x == Number.NEGATIVE_INFINITY) ||
  107. (x == Number.POSITIVE_INFINITY) || (Math.abs(x) > 2e5) || !isFinite(x)) {
  108. //x=0;
  109. x = Number.NaN;
  110. }
  111. if (isNaN(y) || (x == Number.NEGATIVE_INFINITY) ||
  112. (y == Number.POSITIVE_INFINITY) || (Math.abs(y) > 2e5) || !isFinite(y)) {
  113. //y=0;
  114. y = Number.NaN;
  115. }
  116. if (isNaN(z) || (z == Number.NEGATIVE_INFINITY) ||
  117. (z == Number.POSITIVE_INFINITY) || (Math.abs(z) > 2e5) || !isFinite(z)) {
  118. //z=0;
  119. z = Number.NaN;
  120. }
  121. geom.vertices.push(new THREE.Vector3(x*50 , y*50 , z*50 ));
  122. if (u > range2_1 && t > range1_1)
  123. {
  124. c = '0xFFFFFF';
  125. //addFace(geom,ci,ci-1,ci - max_yi-1,c);
  126. //addFace(geom,ci - max_yi-2,ci-1,ci - max_yi-1,c);
  127. addFace4(geom,ci-1,ci,ci-max_yi-1,ci-max_yi-2,c);
  128. geom.faceVertexUvs[ 0 ].push( [
  129. new THREE.UV( xi / max_yi, yi / max_yi ),
  130. new THREE.UV( xi / max_yi, ( yi + 1 ) / max_yi ),
  131. new THREE.UV( ( xi + 1 ) / max_yi, ( yi + 1 ) / max_yi ),
  132. new THREE.UV( ( xi + 1 ) / max_yi, yi / max_yi )
  133. ] );
  134. }
  135. yi += 1;
  136. }
  137. xi += 1;
  138. }
  139. geom.computeFaceNormals();
  140. return geom;
  141. }
  142. function plot_supershape(m, n1, n2, n3, m2, n12, n22, n32, a, b, a2, b2)
  143. {
  144. var geom = new THREE.Geometry();
  145. var ustep = 140;
  146. var vstep = 140;
  147. var piA = Math.PI ;
  148. var piB = Math.PI ;
  149. var theta = -piA;
  150. var r2, r1, phi;
  151. var xi = 0, yi = 0, c, max_yi = 140, max_xi = 140;
  152. while (theta <= piA)
  153. {
  154. phi = -piB/2;
  155. //r1 = 1/Math.pow((Math.pow(Math.abs(Math.cos(m*theta/4)/a)),n2)+Math.pow((Math.abs(Math.sin(m*theta/4)/b)),n3),n1);
  156. r1 = Math.pow(Math.pow(Math.abs((1/a)*Math.cos(m*theta/4)),n2)+Math.pow(Math.abs((1/b)*Math.sin(m*theta/4)),n3),-1/n1);
  157. yi = 0;
  158. while (phi <= piB/2)
  159. {
  160. //r2 = 1/Math.pow((Math.pow(Math.abs(Math.cos(m*phi/4)/a)),n2)+Math.pow((Math.abs(Math.sin(m*phi/4)/b)),n3),n1);
  161. r2 = Math.pow(Math.pow(Math.abs((1/a2)*Math.cos(m2*phi/4)),n22)+Math.pow(Math.abs((1/b2)*Math.sin(m2*phi/4)),n32),-1/n12);
  162. ci = yi + xi* (max_yi+1);
  163. x = r1 * Math.cos(theta) * r2 *Math.cos(phi);
  164. y = r1 * Math.sin(theta) * r2 * Math.cos(phi);
  165. z = r2 * Math.sin(phi);
  166. if (isNaN(x) || (x == Number.NEGATIVE_INFINITY) ||
  167. (x == Number.POSITIVE_INFINITY) || (Math.abs(x) > 2e5) || !isFinite(x)) {
  168. //x=0;
  169. x = Number.NaN;
  170. }
  171. if (isNaN(y) || (x == Number.NEGATIVE_INFINITY) ||
  172. (y == Number.POSITIVE_INFINITY) || (Math.abs(y) > 2e5) || !isFinite(y)) {
  173. //y=0;
  174. y = Number.NaN;
  175. }
  176. if (isNaN(z) || (z == Number.NEGATIVE_INFINITY) ||
  177. (z == Number.POSITIVE_INFINITY) || (Math.abs(z) > 2e5) || !isFinite(z)) {
  178. //z=0;
  179. z = Number.NaN;
  180. }
  181. geom.vertices.push(new THREE.Vector3(x*50 , y*50 , z*50 ));
  182. if (phi > -piB/2 && theta > -piA)
  183. {
  184. c = '0xFFFFFF';
  185. addFace4(geom,ci-1,ci,ci-max_yi-1,ci-max_yi-2,c);
  186. geom.faceVertexUvs[ 0 ].push( [
  187. new THREE.UV( xi / max_yi, yi / max_yi ),
  188. new THREE.UV( xi / max_yi, ( yi + 1 ) / max_yi ),
  189. new THREE.UV( ( xi + 1 ) / max_yi, ( yi + 1 ) / max_yi ),
  190. new THREE.UV( ( xi + 1 ) / max_yi, yi / max_yi )
  191. ] );
  192. }
  193. yi += 1;
  194. phi = phi + (piB/2 / (vstep/2));
  195. }
  196. xi += 1;
  197. theta = theta + (piA / (ustep/2));
  198. }
  199. geom.computeFaceNormals();
  200. return geom;
  201. }