Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <style>
- #cvs
- {
- border:1px solid;
- }
- </style>
- <script>
- function Point(x,y,z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- function Distance(pointA,pointB)
- {
- }
- function Sprite(mesh,rot)
- {
- this.mesh = mesh;
- this.rot = rot;
- }
- function Camera(pos,rot)
- {
- this.pos = pos;
- this.rot = rot;
- }
- function Rotate(point,rot)
- {
- var ox=0,oy=1,oz=2;
- var s=[Math.sin(rot.x),Math.sin(rot.y),Math.sin(rot.z)];
- var c=[Math.cos(rot.x),Math.cos(rot.y),Math.cos(rot.z)];
- var x = point.x;
- var y = point.y;
- var z = point.z;
- var dx = c[oy]*(s[oz]*y+c[oz]*x)-s[oy]*z;
- var dy = s[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))+c[ox]*(c[oz]*y-s[oz]*x);
- var dz = c[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))-s[ox]*(c[oz]*y-s[oz]*x);
- return new Point(dx,dy,dz);
- }
- function Project(sprite,cam,Dim,perp)
- {
- var ox=0,oy=1,oz=2;
- var s=[Math.sin(cam.rot.x),Math.sin(cam.rot.y),Math.sin(cam.rot.z)];
- var c=[Math.cos(cam.rot.x),Math.cos(cam.rot.y),Math.cos(cam.rot.z)];
- var cx=cam.pos.x
- var cy=cam.pos.y
- var cz=cam.pos.z
- var P = [];
- for(var i=0; i<sprite.mesh.length; i++)
- {
- var point = Rotate(sprite.mesh[i],sprite.rot);
- var x = point.x-cx;
- var y = point.y-cy;
- var z = point.z-cz;
- var dx = c[oy]*(s[oz]*y+c[oz]*x)-s[oy]*z;
- var dy = s[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))+c[ox]*(c[oz]*y-s[oz]*x);
- var dz = c[ox]*(c[oy]*z+s[oy]*(s[oz]*y+c[oz]*x))-s[ox]*(c[oz]*y-s[oz]*x);
- P[i] = new Point(perp/dz*dx+Dim.width/2,perp/dz*dy+Dim.height/2);
- }
- return P;
- }
- var sprite = new Sprite([],new Point(
- Math.sin(0*180/Math.PI),
- Math.sin(0*180/Math.PI),
- Math.sin(0*180/Math.PI)));
- var camera = new Camera(new Point(0,0,2000),new Point(0,0,0));
- var perspective = 1000;
- var cvs,G,Dim,active=false,mouseActive =false;
- function loadMesh()
- {
- //MESH LOADED HERE
- var M = 1000;
- for(var i=0; i<M; i++)
- {
- 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);
- }
- for(var i=-M; i<M; i++)
- {
- var x = Math.sqrt(Math.pow(M,2)-Math.pow(i,2))*Math.sin(i/M*65)*100/M;
- var y = Math.sqrt(Math.pow(M,2)-Math.pow(i,2))/M*100-50;
- var z = M+Math.sqrt(Math.pow(M,2)-Math.pow(i,2))/M*Math.cos(i/M*64)*100;
- sprite.mesh[sprite.mesh.length] = new Point(x,y,z);
- }
- //MESH LOADED HERE
- webkitRequestAnimationFrame(draw);
- }
- function init()
- {
- cvs = document.getElementById("cvs");
- G = cvs.getContext("2d");
- Dim = {width:cvs.width,height:cvs.height};
- loadMesh();
- }
- function draw()
- {
- if(!mouseActive)
- {
- sprite.rot.x-=-document.getElementById("rX").value;
- sprite.rot.y-=-document.getElementById("rY").value;
- sprite.rot.z-=-document.getElementById("rZ").value;
- }
- G.fillStyle="rgba(255,255,255,"+document.getElementById("fade").value+")"
- G.fillRect(0,0,Dim.width,Dim.height);
- var points = Project(sprite,camera,Dim,perspective);
- G.beginPath();
- for(var i=0; i<points.length; i++)
- {
- G.lineTo(points[i].x,points[i].y);
- }
- G.stroke();
- if(active&&!mouseActive)webkitRequestAnimationFrame(draw);
- }
- function activate(CB)
- {
- active = CB.checked;
- if(active)webkitRequestAnimationFrame(draw);
- }
- function mouse(e)
- {
- mouseActive = true;
- sprite.rot.z=0;
- sprite.rot.y=e.offsetX/100-0.002;
- sprite.rot.x=e.offsetY/100-0.001;
- webkitRequestAnimationFrame(draw);
- }
- function mouseOut()
- {
- mouseActive = false;
- webkitRequestAnimationFrame(draw);
- }
- </script>
- </head>
- <body>
- <canvas width="1600" height="800" id="cvs" onmousemove="mouse(event)" onmouseout="mouseOut()"></canvas><br>
- <script>
- init();
- </script>
- <input type="checkbox" onclick="activate(this)" />Rotate<br>Fade: <input type="number" id="fade" value="0.01"/><br>
- Rotation speed {<br>
- X:<input type="number" id="rX" value="0.01"/><br>
- Y:<input type="number" id="rY" value="0.01"/><br>
- Z:<input type="number" id="rZ" value="0.01"/><br>
- }
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement