Base3D.hx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import h3d.scene.*;
  2. class Base3D extends SampleApp {
  3. var time : Float = 0.;
  4. var obj1 : Mesh;
  5. var obj2 : Mesh;
  6. override function init() {
  7. // creates a new unit cube
  8. var prim = new h3d.prim.Cube();
  9. // translate it so its center will be at the center of the cube
  10. prim.translate( -0.5, -0.5, -0.5);
  11. // unindex the faces to create hard edges normals
  12. prim.unindex();
  13. // add face normals
  14. prim.addNormals();
  15. // add texture coordinates
  16. prim.addUVs();
  17. // accesss the logo resource and convert it to a texture
  18. var tex = hxd.Res.hxlogo.toTexture();
  19. // create a material with this texture
  20. var mat = new h3d.mat.Material(tex);
  21. // our first cube mesh on the 3D scene with our created material
  22. obj1 = new Mesh(prim, mat, s3d);
  23. // creates another cube, this time with no texture
  24. obj2 = new Mesh(prim, new h3d.mat.Material(), s3d);
  25. // set the second cube color
  26. obj2.material.color.setColor(0xFFB280);
  27. // put it above the first cube
  28. obj2.z = 0.7;
  29. // scale it down to 60%
  30. obj2.scale(0.6);
  31. // adds a directional light to the scene
  32. var light = new h3d.scene.DirLight(new h3d.Vector(0.5, 0.5, -0.5), s3d);
  33. light.enableSpecular = true;
  34. // set the ambient light to 30%
  35. s3d.lightSystem.ambientLight.set(0.3, 0.3, 0.3);
  36. // activate lights on boss cubes
  37. obj1.material.mainPass.enableLights = true;
  38. obj2.material.mainPass.enableLights = true;
  39. }
  40. override function update( dt : Float ) {
  41. // time is flying...
  42. time += 0.01 * dt;
  43. // move the camera position around the two cubes
  44. var dist = 5;
  45. s3d.camera.pos.set(Math.cos(time) * dist, Math.sin(time) * dist, dist * 0.7 * Math.sin(time));
  46. // rotate the second cube along a given axis + angle
  47. obj2.setRotateAxis(-0.5, 2, Math.cos(time), time + Math.PI / 2);
  48. }
  49. static function main() {
  50. // initialize embeded ressources
  51. hxd.Res.initEmbed();
  52. // start the application
  53. new Base3D();
  54. }
  55. }