Advertisement
nzisaacnz

3D funny shape

Apr 17th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <style>
  4. #cvs
  5. {
  6.     border:1px solid;
  7. }
  8. </style>
  9. <script>
  10. function Point(x,y,z)
  11. {
  12.     this.x = x;
  13.     this.y = y;
  14.     this.z = z;
  15. }
  16. function Distance(pointA,pointB)
  17. {
  18.    
  19. }
  20. function Sprite(mesh,rot)
  21. {
  22.     this.mesh = mesh;
  23.     this.rot = rot;
  24. }
  25. function Camera(pos,rot)
  26. {
  27.     this.pos = pos;
  28.     this.rot = rot;
  29. }
  30. function Rotate(point,rot)
  31. {
  32.     var ox=0,oy=1,oz=2;
  33.     var s=[Math.sin(rot.x),Math.sin(rot.y),Math.sin(rot.z)];
  34.     var c=[Math.cos(rot.x),Math.cos(rot.y),Math.cos(rot.z)];
  35.     var x = point.x;
  36.     var y = point.y;
  37.     var z = point.z;
  38.     var dx = c[oy]*(s[oz]*y+c[oz]*x)-s[oy]*z;
  39.     var dy = s[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))+c[ox]*(c[oz]*y-s[oz]*x);
  40.     var dz = c[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))-s[ox]*(c[oz]*y-s[oz]*x);
  41.     return new Point(dx,dy,dz);
  42. }
  43. function Project(sprite,cam,Dim,perp)
  44. {
  45.     var ox=0,oy=1,oz=2;
  46.     var s=[Math.sin(cam.rot.x),Math.sin(cam.rot.y),Math.sin(cam.rot.z)];
  47.     var c=[Math.cos(cam.rot.x),Math.cos(cam.rot.y),Math.cos(cam.rot.z)];
  48.     var cx=cam.pos.x
  49.     var cy=cam.pos.y
  50.     var cz=cam.pos.z
  51.     var P = [];
  52.     for(var i=0; i<sprite.mesh.length; i++)
  53.     {
  54.         var point = Rotate(sprite.mesh[i],sprite.rot);
  55.         var x = point.x-cx;
  56.         var y = point.y-cy;
  57.         var z = point.z-cz;
  58.         var dx = c[oy]*(s[oz]*y+c[oz]*x)-s[oy]*z;
  59.         var dy = s[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))+c[ox]*(c[oz]*y-s[oz]*x);
  60.         var dz = c[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))-s[ox]*(c[oz]*y-s[oz]*x);
  61.         P[i] = new Point(perp/dz*dx+Dim.width/2,perp/dz*dy+Dim.height/2);
  62.     }
  63.     return P;
  64. }
  65.  
  66.  
  67. var sprite = new Sprite([],new Point(
  68. Math.sin(0*180/Math.PI),
  69. Math.sin(0*180/Math.PI),
  70. Math.sin(0*180/Math.PI)));
  71. var camera = new Camera(new Point(0,0,2000),new Point(0,0,0));
  72. var perspective = 1000;
  73. var cvs,G,Dim,active=false,mouseActive =false;
  74. function loadMesh()
  75. {
  76.    
  77.    
  78.     //MESH LOADED HERE
  79.     var M = 1000;
  80.     for(var i=0; i<M; i++)
  81.     {
  82.         sprite.mesh[sprite.mesh.length] = new Point(Math.cos(i)*Math.sin(i/M*Math.PI)*100,Math.cos(i/M*Math.PI*3)*50,i);
  83.     }
  84.     for(var i=-M; i<M; i++)
  85.     {
  86.         var x = Math.sqrt(Math.pow(M,2)-Math.pow(i,2))*Math.sin(i/M*65)*100/M;
  87.         var y = Math.sqrt(Math.pow(M,2)-Math.pow(i,2))/M*100-50;
  88.         var z = M+Math.sqrt(Math.pow(M,2)-Math.pow(i,2))/M*Math.cos(i/M*64)*100;
  89.         sprite.mesh[sprite.mesh.length] = new Point(x,y,z);
  90.     }
  91.     //MESH LOADED HERE
  92.    
  93.    
  94.     webkitRequestAnimationFrame(draw);
  95. }
  96.  
  97. function init()
  98. {
  99.     cvs = document.getElementById("cvs");
  100.     G = cvs.getContext("2d");
  101.     Dim = {width:cvs.width,height:cvs.height};
  102.     loadMesh();
  103. }
  104. function draw()
  105. {
  106.     if(!mouseActive)
  107.     {
  108.         sprite.rot.x-=-document.getElementById("rX").value;
  109.         sprite.rot.y-=-document.getElementById("rY").value;
  110.         sprite.rot.z-=-document.getElementById("rZ").value;
  111.     }
  112.     G.fillStyle="rgba(255,255,255,"+document.getElementById("fade").value+")"
  113.     G.fillRect(0,0,Dim.width,Dim.height);
  114.     var points = Project(sprite,camera,Dim,perspective);
  115.     G.beginPath();
  116.     for(var i=0; i<points.length; i++)
  117.     {
  118.         G.lineTo(points[i].x,points[i].y);
  119.     }
  120.     G.stroke();
  121.     if(active&&!mouseActive)webkitRequestAnimationFrame(draw);
  122. }
  123. function activate(CB)
  124. {
  125.     active = CB.checked;
  126.     if(active)webkitRequestAnimationFrame(draw);
  127. }
  128. function mouse(e)
  129. {
  130.     mouseActive = true;
  131.     sprite.rot.z=0;
  132.     sprite.rot.y=e.offsetX/100-0.002;
  133.     sprite.rot.x=e.offsetY/100-0.001;
  134.     webkitRequestAnimationFrame(draw);
  135. }
  136. function mouseOut()
  137. {
  138.    
  139.     mouseActive = false;
  140.     webkitRequestAnimationFrame(draw);
  141. }
  142. </script>
  143. </head>
  144. <body>
  145. <canvas width="1600" height="800" id="cvs" onmousemove="mouse(event)" onmouseout="mouseOut()"></canvas><br>
  146. <script>
  147. init();
  148. </script>
  149. <input type="checkbox" onclick="activate(this)" />Rotate<br>Fade: <input type="number" id="fade" value="0.01"/><br>
  150. Rotation speed {<br>
  151.     X:<input type="number" id="rX" value="0.01"/><br>
  152.     Y:<input type="number" id="rY" value="0.01"/><br>
  153.     Z:<input type="number" id="rZ" value="0.01"/><br>
  154. }
  155. </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement