Helpers.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import hxd.Res;
  2. import h3d.Vector;
  3. import h3d.scene.*;
  4. import h3d.scene.fwd.*;
  5. class Helpers extends hxd.App {
  6. var time = 0.0;
  7. var cube : Mesh;
  8. var pointLights = new Array<PointLight>();
  9. override function init() {
  10. s3d.camera.pos.set( 5, 5, 5 );
  11. s3d.camera.setFovX( 70, s3d.camera.screenRatio );
  12. new AxesHelper( s3d );
  13. new GridHelper( s3d, 10, 10 );
  14. var prim = new h3d.prim.Cube( 1, 1, 1, true );
  15. prim.unindex();
  16. prim.addNormals();
  17. prim.addUVs();
  18. cube = new Mesh( prim, s3d );
  19. cube.setPosition( 0, 0, 2 );
  20. cube.material.shadows = false;
  21. new AxesHelper( cube, 1 );
  22. cast(s3d.lightSystem,h3d.scene.fwd.LightSystem).ambientLight.set( 0.3, 0.3, 0.3 );
  23. var dirLight = new DirLight( new Vector( 0.5, 0.5, -0.5 ), s3d );
  24. dirLight.enableSpecular = true;
  25. var pointLightColors = [0xEB304D,0x7FC309,0x288DF9];
  26. for( i in 0...pointLightColors.length ) {
  27. var l = new PointLight( s3d );
  28. l.enableSpecular = true;
  29. l.color.setColor( pointLightColors[i] );
  30. pointLights.push( l );
  31. new PointLightHelper( l );
  32. }
  33. new CameraController(s3d).loadFromCamera();
  34. }
  35. override function update( dt : Float ) {
  36. time += dt;
  37. cube.rotate( 0.01, 0.02, 0.03 );
  38. pointLights[0].x = Math.sin( time ) * 3;
  39. pointLights[1].y = Math.sin( time ) * 3;
  40. pointLights[2].z = Math.sin( time ) * 3;
  41. }
  42. static function main() {
  43. Res.initEmbed();
  44. new Helpers();
  45. }
  46. }
  47. class AxesHelper extends h3d.scene.Graphics {
  48. public function new( ?parent : h3d.scene.Object, size = 2.0, colorX = 0xEB304D, colorY = 0x7FC309, colorZ = 0x288DF9, lineWidth = 2.0 ) {
  49. super( parent );
  50. material.props = h3d.mat.MaterialSetup.current.getDefaults( "ui" );
  51. lineShader.width = lineWidth;
  52. setColor( colorX );
  53. lineTo( size, 0, 0 );
  54. setColor( colorY );
  55. moveTo( 0, 0, 0 );
  56. lineTo( 0, size, 0 );
  57. setColor( colorZ );
  58. moveTo( 0, 0, 0 );
  59. lineTo( 0, 0, size );
  60. }
  61. }
  62. class GridHelper extends h3d.scene.Graphics {
  63. public function new( ?parent : Object, size = 10.0, divisions = 10, color1 = 0x444444, color2 = 0x888888, lineWidth = 1.0 ) {
  64. super( parent );
  65. material.props = h3d.mat.MaterialSetup.current.getDefaults( "ui" );
  66. lineShader.width = lineWidth;
  67. var hsize = size / 2;
  68. var csize = size / divisions;
  69. var center = divisions / 2;
  70. for( i in 0...divisions+1 ) {
  71. var p = i * csize;
  72. setColor( ( i!=0 && i!=divisions && i%center==0 ) ? color2 : color1 );
  73. moveTo( -hsize + p, -hsize, 0 );
  74. lineTo( -hsize + p, -hsize + size, 0 );
  75. moveTo( -hsize, -hsize + p, 0 );
  76. lineTo( -hsize + size, -hsize + p, 0 );
  77. }
  78. }
  79. }
  80. class PointLightHelper extends h3d.scene.Mesh {
  81. public function new( light : h3d.scene.fwd.PointLight, sphereSize = 0.5 ) {
  82. var prim = new h3d.prim.Sphere( sphereSize, 4, 2 );
  83. prim.addNormals();
  84. prim.addUVs();
  85. super( prim, light );
  86. material.color = light.color;
  87. material.mainPass.wireframe = true;
  88. }
  89. }