Base3D.hx 2.1 KB

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