Zurück   Flashforum > Flash > ActionScript > ActionScript 1

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 25-07-2004, 10:17   #1 (permalink)
Neuer User
 
Registriert seit: Jan 2004
Ort: bei Düsseldorf
Beiträge: 76
Talking Erste 3D Gehversuche :)

Hallo zusammen,
ich hab angefangen eine kleine Wireframe 3D - Engine zu basteln. Ich werd zwar alles neu proggen,da mir die transformationen nicht 100% gefallen,aber vielleicht kann jemand damit was anfangen. Das ist meine erste 3d-Engine überhaupt, auch wenn ich mich vorher schon oft mit 3d beschäftigt habe, also seit nachsichtig mit der Kritik

Code:
<?
ming_setScale(20.00000000);
ming_useswfversion(6);
$movie = new SWFMovie();
$movie->setDimension(400,400);
$movie->setBackground(0x00, 0x00, 0x00 );
$movie->setRate(31);// mx actionscript
$strAction ="

Math.RAD = Math.PI/180;

//Klassendefinition für einen Punkt 
Point= function(x,y,z){
this.x=x;
this.y=y;
this.z=z;
this.x_2d=100;
this.y_2d=100;
};

Point.prototype.move=function(dx,dy,dz){
this.x+=dx;
this.y+=dy;
this.z+=dz;
};

Point.prototype.scale=function(dx,dy,dz){
this.x*=dx;
this.y*=dy;
this.z*=dz;
};

Point.prototype.rotX = function(degrees) {
    var cosine = Math.cos(degrees*Math.RAD);
    var sine = Math.sin(degrees*Math.RAD);
    var tempY = this.y*cosine-this.z*sine;
    var tempZ = this.y*sine+this.z*cosine;
    this.y = tempY;
    this.z = tempZ;
};

Point.prototype.rotY = function(degrees) {
    var cosine = Math.cos(degrees*Math.RAD);
    var sine = Math.sin(degrees*Math.RAD);
    var tempX = this.z*sine+this.x*cosine;
    var tempZ = this.z*cosine-this.x*sine;
    this.x = tempX;
    this.z = tempZ;
};

Point.prototype.rotZ = function(degrees) {
    var cosine = Math.cos(degrees*Math.RAD);
    var sine = Math.sin(degrees*Math.RAD);
    var tempX = this.x*cosine - this.y*sine;
    var tempY = this.x*sine + this.y*cosine;
    this.x = tempX;
    this.y = tempY;
};

Point.prototype.to2d=function(sx,sy){
dis=-300; //sollte änderbar sein
div=this.z+dis;
if(div==0){div=0.0000000001;}
pers = dis/div;
this.x_2d=sx+(this.x*pers);
this.y_2d=sy+(this.y*-pers);
};

//klassendefinition für ein dreieck
Tri=function(p1,p2,p3){
this.p1=p1;
this.p2=p2;
this.p3=p3;
};

Tri.prototype.draw=function(context){
w=200;
h=200;
this.p1.to2d(w,h);
this.p2.to2d(w,h);
this.p3.to2d(w,h);
context.moveTo(this.p1.x_2d,this.p1.y_2d);
context.lineTo(this.p2.x_2d,this.p2.y_2d);
context.lineTo(this.p3.x_2d,this.p3.y_2d);
context.lineTo(this.p1.x_2d,this.p1.y_2d);
};

Tri.prototype.rotate=function(rx,ry,rz){
this.p1.rotX(rx);
this.p2.rotX(rx);
this.p3.rotX(rx);
this.p1.rotY(ry);
this.p2.rotY(ry);
this.p3.rotY(ry);
this.p1.rotZ(rz);
this.p2.rotZ(rz);
this.p3.rotZ(rz);

};

Tri.prototype.move=function(dx,dy,dz){
this.p1.move(dx,dy,dz);
this.p2.move(dx,dy,dz);
this.p3.move(dx,dy,dz);
};

Tri.prototype.scale=function(dx,dy,dz){
this.p1.scale(dx,dy,dz);
this.p2.scale(dx,dy,dz);
this.p3.scale(dx,dy,dz);
};

Quad=function(p1,p2,p3,p4){
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
};

Quad.prototype.draw=function(context){
w=200;
h=200;
this.p1.to2d(w,h);
this.p2.to2d(w,h);
this.p3.to2d(w,h);
this.p4.to2d(w,h);
context.moveTo(this.p1.x_2d,this.p1.y_2d);
context.lineTo(this.p2.x_2d,this.p2.y_2d);
context.lineTo(this.p3.x_2d,this.p3.y_2d);
context.lineTo(this.p4.x_2d,this.p4.y_2d);
context.lineTo(this.p1.x_2d,this.p1.y_2d);
};

Quad.prototype.move=function(dx,dy,dz){
this.p1.move(dx,dy,dz);
this.p2.move(dx,dy,dz);
this.p3.move(dx,dy,dz);
this.p4.move(dx,dy,dz);
};

Quad.prototype.scale=function(dx,dy,dz){
this.p1.scale(dx,dy,dz);
this.p2.scale(dx,dy,dz);
this.p3.scale(dx,dy,dz);
this.p4.scale(dx,dy,dz);
};

Quad.prototype.rotate=function(rx,ry,rz){
this.p1.rotX(rx);
this.p2.rotX(rx);
this.p3.rotX(rx);
this.p4.rotX(rx);
this.p1.rotY(ry);
this.p2.rotY(ry);
this.p3.rotY(ry);
this.p4.rotY(ry);
this.p1.rotZ(rz);
this.p2.rotZ(rz);
this.p3.rotZ(rz);
this.p4.rotZ(rz);
};

Quad.prototype.to2d=function(viewDistanz){
this.p1.to2d(viewDistanz);
this.p2.to2d(viewDistanz);
this.p3.to2d(viewDistanz);
this.p4.to2d(viewDistanz);
};


Object3D = function(){
this.faces=new Array(); //array der seitenflächen
};

Object3D.prototype.draw=function(context){
i=0;
while(this.faces[i] != null)
{
this.faces[i].draw(context); // jede fläche zeichnet sich selbst :)
i++;
}
};

Object3D.prototype.rotate=function(rx,ry,rz){
i=0;
while(this.faces[i] != null)
{
this.faces[i].rotate(rx,ry,rz);
i++;
}
};

Object3D.prototype.move=function(dx,dy,dz){
i=0;
while(this.faces[i]!=undefined)
{
this.faces[i].move(dx,dy,dz);
i++;
}
};

Object3D.prototype.scale=function(dx,dy,dz){
i=0;
while(this.faces[i]!=undefined)
{
this.faces[i].scale(dx,dy,dz);
i++;
}
};

Object3D.prototype.addFace=function(face){
this.faces.push(face);
};

_root.obj=new Object3D();
_root.obj.addFace(new Quad(new Point(0,0,0),new Point(50,0,0),new Point(50,50,0),new Point(0,50,0))); //ein Quadart wird hinzugefügt
_root.obj.addFace(new Quad(new Point(0,0,50),new Point(50,0,50),new Point(50,50,50),new Point(0,50,50)));
_root.obj.addFace(new Quad(new Point(0,0,0),new Point(0,0,50),new Point(50,0,50),new Point(50,0,0)));
_root.obj.addFace(new Quad(new Point(0,50,0),new Point(0,50,50),new Point(50,50,50),new Point(50,50,0)));
_root.obj.addFace(new Tri(new Point(0,50,0),new Point(50,50,0),new Point(25,90,25))); //ein dreieck wird hinzugefügt
_root.obj.addFace(new Tri(new Point(0,50,50),new Point(50,50,50),new Point(25,90,25)));
_root.obj.move(-25,-25,-25); //Objekt zentrieren (nicht wirklich notwendig)
_root.obj.scale(2,2,1);
";
$movie->add(new SWFAction(str_replace("\r", "", $strAction)));
$movie->nextFrame();
$movie->nextFrame();
$strAction="
_root.clear();
_root.lineStyle(1,0x888888);

_root.obj.rotate(((_root._ymouse/100)-2)*3,((_root._xmouse/100)-2)*3,0); //objekt drehen
//_root.obj.move(_root._xmouse/100,_root._ymouse/100,0);
_root.obj.draw(_root); // Jede Fläche zeichnet sich selbst (Tri oder Quad)


gotoAndPlay(2);
";
$movie->add(new SWFAction(str_replace("\r", "", $strAction)));
$movie->nextFrame();

header("Content-type:application/x-shockwave-flash");
$movie->output(9);
?>
Gruß,
SIGINT

P.S.: Ich hoffe irgendwann mal Flat-Shading einbauen zu können,aber dafür muss ich noch ein paar Mathe - Tuts durchlesen

P.P.S.: Hab's ganz vergessen: Die SWF wird per MING erzeugt
Angehängte Dateien
Dateityp: zip 3d_sigint.zip (1,4 KB, 33x aufgerufen)

Geändert von sigint (25-07-2004 um 10:36 Uhr)
sigint ist offline   Mit Zitat antworten
Alt 25-07-2004, 11:22   #2 (permalink)
Neuer User
 
Registriert seit: Jan 2004
Ort: bei Düsseldorf
Beiträge: 76
Ich hab noch etwas mit SWFTOOLS rumgespielt
Angehängte Dateien
Dateityp: zip output.zip (2,4 KB, 25x aufgerufen)
sigint ist offline   Mit Zitat antworten
Alt 27-07-2004, 12:18   #3 (permalink)
Neuer User
 
Registriert seit: Jan 2004
Ort: bei Düsseldorf
Beiträge: 76
So, jetzt sieht es etwas besser aus und das Timing wird per setInterval gesteuert

ActionScript:
  1. <?
  2. ming_setScale(20.00000000);
  3. ming_useswfversion(6);
  4. $movie = new SWFMovie();
  5. $movie->setDimension(400,400);
  6. $movie->setBackground(0x00, 0x33, 0x00 );
  7. $movie->setRate(31);// mx actionscript
  8. $strAction ="
  9. Math.RAD = Math.PI/180;
  10. //Klassendefinition für einen Punkt
  11. Point= function(x,y,z){
  12. this.x=x;
  13. this.y=y;
  14. this.z=z;
  15. this.x_2d=100;
  16. this.y_2d=100;
  17. };
  18. Point.prototype.move=function(dx,dy,dz){
  19. this.x+=dx;
  20. this.y+=dy;
  21. this.z+=dz;
  22. };
  23. Point.prototype.scale=function(dx,dy,dz){
  24. this.x*=dx;
  25. this.y*=dy;
  26. this.z*=dz;
  27. };
  28. Point.prototype.rotX = function(degrees) {
  29.     var cosine = Math.cos(degrees*Math.RAD);
  30.     var sine = Math.sin(degrees*Math.RAD);
  31.     var tempY = this.y*cosine-this.z*sine;
  32.     var tempZ = this.y*sine+this.z*cosine;
  33.     this.y = tempY;
  34.     this.z = tempZ;
  35. };
  36. Point.prototype.rotY = function(degrees) {
  37.     var cosine = Math.cos(degrees*Math.RAD);
  38.     var sine = Math.sin(degrees*Math.RAD);
  39.     var tempX = this.z*sine+this.x*cosine;
  40.     var tempZ = this.z*cosine-this.x*sine;
  41.     this.x = tempX;
  42.     this.z = tempZ;
  43. };
  44. Point.prototype.rotZ = function(degrees) {
  45.     var cosine = Math.cos(degrees*Math.RAD);
  46.     var sine = Math.sin(degrees*Math.RAD);
  47.     var tempX = this.x*cosine - this.y*sine;
  48.     var tempY = this.x*sine + this.y*cosine;
  49.     this.x = tempX;
  50.     this.y = tempY;
  51. };
  52. Point.prototype.to2d=function(sx,sy){
  53. var dis=-300; //sollte änderbar sein
  54. var div=this.z+dis;
  55. if(div==0){div=0.0000000001;}
  56. pers = dis/div;
  57. this.x_2d=sx+(this.x*pers);
  58. this.y_2d=sy+(this.y*-pers);
  59. };
  60. //klassendefinition für ein dreieck
  61. Tri=function(p1,p2,p3){
  62. this.p1=p1;
  63. this.p2=p2;
  64. this.p3=p3;
  65. };
  66. Tri.prototype.draw=function(context){
  67. w=200;
  68. h=200;
  69. this.p1.to2d(w,h);
  70. this.p2.to2d(w,h);
  71. this.p3.to2d(w,h);
  72. context.moveTo(this.p1.x_2d,this.p1.y_2d);
  73. context.lineTo(this.p2.x_2d,this.p2.y_2d);
  74. context.lineTo(this.p3.x_2d,this.p3.y_2d);
  75. context.lineTo(this.p1.x_2d,this.p1.y_2d);
  76. };
  77. Tri.prototype.rotate=function(rx,ry,rz){
  78. this.p1.rotX(rx);
  79. this.p2.rotX(rx);
  80. this.p3.rotX(rx);
  81. this.p1.rotY(ry);
  82. this.p2.rotY(ry);
  83. this.p3.rotY(ry);
  84. this.p1.rotZ(rz);
  85. this.p2.rotZ(rz);
  86. this.p3.rotZ(rz);
  87. };
  88. Tri.prototype.move=function(dx,dy,dz){
  89. this.p1.move(dx,dy,dz);
  90. this.p2.move(dx,dy,dz);
  91. this.p3.move(dx,dy,dz);
  92. };
  93. Tri.prototype.scale=function(dx,dy,dz){
  94. this.p1.scale(dx,dy,dz);
  95. this.p2.scale(dx,dy,dz);
  96. this.p3.scale(dx,dy,dz);
  97. };
  98. Quad=function(p1,p2,p3,p4){
  99. this.p1=p1;
  100. this.p2=p2;
  101. this.p3=p3;
  102. this.p4=p4;
  103. };
  104. Quad.prototype.draw=function(context){
  105. var w=200;
  106. var h=200;
  107. this.p1.to2d(w,h);
  108. this.p2.to2d(w,h);
  109. this.p3.to2d(w,h);
  110. this.p4.to2d(w,h);
  111. context.moveTo(this.p1.x_2d,this.p1.y_2d);
  112. context.lineTo(this.p2.x_2d,this.p2.y_2d);
  113. context.lineTo(this.p3.x_2d,this.p3.y_2d);
  114. context.lineTo(this.p4.x_2d,this.p4.y_2d);
  115. context.lineTo(this.p1.x_2d,this.p1.y_2d);
  116. };
  117. Quad.prototype.move=function(dx,dy,dz){
  118. this.p1.move(dx,dy,dz);
  119. this.p2.move(dx,dy,dz);
  120. this.p3.move(dx,dy,dz);
  121. this.p4.move(dx,dy,dz);
  122. };
  123. Quad.prototype.scale=function(dx,dy,dz){
  124. this.p1.scale(dx,dy,dz);
  125. this.p2.scale(dx,dy,dz);
  126. this.p3.scale(dx,dy,dz);
  127. this.p4.scale(dx,dy,dz);
  128. };
  129. Quad.prototype.rotate=function(rx,ry,rz){
  130. this.p1.rotX(rx);
  131. this.p2.rotX(rx);
  132. this.p3.rotX(rx);
  133. this.p4.rotX(rx);
  134. this.p1.rotY(ry);
  135. this.p2.rotY(ry);
  136. this.p3.rotY(ry);
  137. this.p4.rotY(ry);
  138. this.p1.rotZ(rz);
  139. this.p2.rotZ(rz);
  140. this.p3.rotZ(rz);
  141. this.p4.rotZ(rz);
  142. };
  143. CPoly=function(points){
  144. this.points=new Array();
  145. };
  146. CPoly.prototype.addPoint=function(point){
  147. this.points.push(point);
  148. };
  149. CPoly.prototype.draw=function(context){
  150. var w=200;
  151. var h=200;
  152. var i=0;
  153. while(this.points[i] != null){
  154. this.points[i].to2d(w,h);
  155. i++;
  156. }
  157. i=0;
  158. context.moveTo(this.points[0].x_2d,this.points[0].y_2d);
  159. while(this.points[i] != null){
  160. context.lineTo(this.points[i].x_2d,this.points[i].y_2d);
  161. i++;
  162. }
  163. context.lineTo(this.points[0].x_2d,this.points[0].y_2d);
  164. };
  165. CPoly.prototype.move=function(dx,dy,dz){
  166. var i=0;
  167. while(this.points[i]!= null){
  168. this.points[i].move(dx,dy,dz);
  169. i++;
  170. }
  171. };
  172. CPoly.prototype.scale=function(dx,dy,dz){
  173. var i=0;
  174. while(this.points[i]!= undefined){
  175. this.points[i].scale(dx,dy,dz);
  176. i++;
  177. }
  178. };
  179. CPoly.prototype.rotate=function(rx,ry,rz){
  180. var i=0;
  181. while(this.points[i]!= undefined){
  182. this.points[i].rotX(rx);
  183. this.points[i].rotY(ry);
  184. this.points[i].rotZ(rz);
  185. i++;
  186. }
  187. };
  188. Object3D = function(){
  189. this.faces=new Array(); //array der seitenflächen
  190. };
  191. Object3D.prototype.draw=function(context,color){
  192. var obj_i=0;
  193. while(this.faces[obj_i] != null)
  194. {
  195. context.lineStyle(1,color);
  196. context.beginFill(color,33);
  197. this.faces[obj_i].draw(context); // jede fläche zeichnet sich selbst :)
  198. context.endFill();
  199. obj_i++;
  200. }
  201. };
  202. Object3D.prototype.rotate=function(rx,ry,rz){
  203. i=0;
  204. while(this.faces[i] != null)
  205. {
  206. this.faces[i].rotate(rx,ry,rz);
  207. i++;
  208. }
  209. };
  210. Object3D.prototype.move=function(dx,dy,dz){
  211. i=0;
  212. while(this.faces[i]!=undefined)
  213. {
  214. this.faces[i].move(dx,dy,dz);
  215. i++;
  216. }
  217. };
  218. Object3D.prototype.scale=function(dx,dy,dz){
  219. i=0;
  220. while(this.faces[i]!=undefined)
  221. {
  222. this.faces[i].scale(dx,dy,dz);
  223. i++;
  224. }
  225. };
  226. Object3D.prototype.addFace=function(face){
  227. this.faces.push(face);
  228. };
  229. this.createEmptyMovieClip('dc',7);
  230. obj=new Object3D();
  231. poly=new CPoly();
  232. poly.addPoint(new Point(40,10,0));
  233. poly.addPoint(new Point(30,0,0));
  234. poly.addPoint(new Point(10,0,0));
  235. poly.addPoint(new Point(0,10,0));
  236. poly.addPoint(new Point(0,20,0));
  237. poly.addPoint(new Point(10,30,0));
  238. poly.addPoint(new Point(30,30,0));
  239. poly.addPoint(new Point(30,40,0));
  240. poly.addPoint(new Point(0,40,0));
  241. poly.addPoint(new Point(10,50,0));
  242. poly.addPoint(new Point(30,50,0));
  243. poly.addPoint(new Point(40,40,0));
  244. poly.addPoint(new Point(40,30,0));
  245. poly.addPoint(new Point(30,20,0));
  246. poly.addPoint(new Point(10,20,0));
  247. poly.addPoint(new Point(10,10,0));
  248. obj.addFace(poly);
  249. poly=new CPoly();
  250. poly.addPoint(new Point(40,10,10));
  251. poly.addPoint(new Point(30,0,10));
  252. poly.addPoint(new Point(10,0,10));
  253. poly.addPoint(new Point(0,10,10));
  254. poly.addPoint(new Point(0,20,10));
  255. poly.addPoint(new Point(10,30,10));
  256. poly.addPoint(new Point(30,30,10));
  257. poly.addPoint(new Point(30,40,10));
  258. poly.addPoint(new Point(0,40,10));
  259. poly.addPoint(new Point(10,50,10));
  260. poly.addPoint(new Point(30,50,10));
  261. poly.addPoint(new Point(40,40,10));
  262. poly.addPoint(new Point(40,30,10));
  263. poly.addPoint(new Point(30,20,10));
  264. poly.addPoint(new Point(10,20,10));
  265. poly.addPoint(new Point(10,10,10));
  266. obj.addFace(poly);
  267. obj.addFace(new Quad(new Point(40,10,0),new Point(40,10,10),new Point(30,0,10),new Point(30,0,0)));
  268. obj.addFace(new Quad(new Point(10,0,0),new Point(10,0,10),new Point(0,10,10),new Point(0,10,0)));
  269. obj.addFace(new Quad(new Point(0,20,0),new Point(0,20,10),new Point(10,30,10),new Point(10,30,0)));
  270. obj.addFace(new Quad(new Point(30,30,0),new Point(30,30,10),new Point(30,40,10),new Point(30,40,0)));
  271. obj.addFace(new Quad(new Point(0,40,0),new Point(0,40,10),new Point(10,50,10),new Point(10,50,0)));
  272. obj.addFace(new Quad(new Point(30,50,0),new Point(30,50,10),new Point(40,40,10),new Point(40,40,0)));
  273. obj.addFace(new Quad(new Point(40,30,0),new Point(40,30,10),new Point(30,20,10),new Point(30,20,0)));
  274. obj.addFace(new Quad(new Point(10,20,0),new Point(10,20,10),new Point(10,10,10),new Point(10,10,0)));
  275. obj.addFace(new Quad(new Point(30,0,0),new Point(30,0,10),new Point(10,0,10),new Point(10,0,0)));
  276. obj.addFace(new Quad(new Point(0,10,0),new Point(0,10,10),new Point(0,20,10),new Point(0,20,0)));
  277. obj.addFace(new Quad(new Point(10,30,0),new Point(10,30,10),new Point(30,30,10),new Point(30,30,0)));
  278. obj.addFace(new Quad(new Point(30,40,0),new Point(30,40,10),new Point(0,40,10),new Point(0,40,0)));
  279. obj.addFace(new Quad(new Point(10,50,0),new Point(10,50,10),new Point(30,50,10),new Point(30,50,0)));
  280. obj.addFace(new Quad(new Point(40,40,0),new Point(40,40,10),new Point(40,30,10),new Point(40,30,0)));
  281. obj.addFace(new Quad(new Point(30,20,0),new Point(30,20,10),new Point(10,20,10),new Point(10,20,0)));
  282. obj.addFace(new Quad(new Point(10,10,0),new Point(10,10,10),new Point(40,10,10),new Point(40,10,0)));
  283. obj.move(-20,-25,-5); //Objekt zentrieren (nicht wirklich notwendig)
  284. obj.scale(5,5,1);
  285. obj.rotate(5,0,10);
  286. this._highquality=0;
  287. var color='0xff0000';
  288. this.onMouseMove=function(){
  289. if(hitTest(_root._xmouse,_root._ymouse,false))
  290. {color='0x00ff00';}
  291. else
  292. {color='0xff0000';}
  293. };
  294. this.onMouseDown=function(){
  295. if(hitTest(_root._xmouse,_root._ymouse,false)){getURL('http://www.google.de',_self);}
  296. };
  297. paint=function(context)
  298. {
  299. context.clear();
  300. obj.rotate(0,3,0);
  301. //obj.rotate(((context._ymouse/100)-2)*3,((context._xmouse/100)-2)*3,0); //objekt drehen
  302. obj.draw(context,color); // Jede Fläche zeichnet sich selbst (Tri oder Quad)
  303. };
  304. Mouse.addListener(this);
  305. setInterval(paint , 25, this);
  306. this.stop();
  307. ";
  308.  
  309. $movie->add(new SWFAction(str_replace("\r", "", $strAction)));
  310. $movie->nextFrame();
  311. header("Content-type:application/x-shockwave-flash");
  312. $movie->output(9);
  313. ?>
Angehängte Dateien
Dateityp: zip interval.zip (2,1 KB, 33x aufgerufen)
sigint ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 11:08 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele