Lights.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import hxd.Math;
  2. class Lights extends SampleApp {
  3. var lights : Array<h3d.scene.pbr.Light>;
  4. var movingObjects : Array<{ m : h3d.scene.Mesh, cx : Float, cy : Float, pos : Float, ray : Float, speed : Float }> = [];
  5. var curLight : Int = 0;
  6. var bitmap : h2d.Bitmap;
  7. var inf : h2d.Text;
  8. var dynCullingEnable = true;
  9. function addCullingCollider() {
  10. dynCullingEnable = true;
  11. for( o in s3d ) {
  12. if( o.cullingCollider != null ) continue;
  13. var absPos = o.getAbsPos();
  14. o.cullingCollider = new h3d.col.Sphere(absPos.tx, absPos.ty, absPos.tz, hxd.Math.max(o.scaleZ, hxd.Math.max(o.scaleX, o.scaleY)));
  15. }
  16. }
  17. function removeCullingCollider() {
  18. dynCullingEnable = false;
  19. for( o in s3d ) {
  20. o.cullingCollider = null;
  21. }
  22. }
  23. override function init() {
  24. super.init();
  25. s3d.camera.pos.set(100, 20, 80);
  26. new h3d.scene.CameraController(s3d).loadFromCamera();
  27. var prim = new h3d.prim.Grid(100,100,1,1);
  28. prim.addNormals();
  29. prim.addUVs();
  30. var floor = new h3d.scene.Mesh(prim, s3d);
  31. floor.material.castShadows = false;
  32. floor.x = -50;
  33. floor.y = -50;
  34. var box = new h3d.prim.Cube(1,1,1,true);
  35. box.unindex();
  36. box.addNormals();
  37. for( i in 0...50 ) {
  38. var m = new h3d.scene.Mesh(box, s3d);
  39. m.material.color.set(Math.random(), Math.random(), Math.random());
  40. m.material.color.normalize();
  41. m.scale(1 + Math.random() * 10);
  42. m.z = m.scaleX * 0.5;
  43. m.setRotation(0,0,Math.random() * Math.PI * 2);
  44. do {
  45. m.x = Std.random(80) - 40;
  46. m.y = Std.random(80) - 40;
  47. } while( m.x * m.x + m.y * m.y < 25 + m.scaleX * m.scaleX );
  48. m.material.getPass("shadow").isStatic = true;
  49. var absPos = m.getAbsPos();
  50. m.cullingCollider = new h3d.col.Sphere(absPos.tx, absPos.ty, absPos.tz, hxd.Math.max(m.scaleZ, hxd.Math.max(m.scaleX, m.scaleY)));
  51. }
  52. var sp = new h3d.prim.Sphere(1,16,16);
  53. sp.addNormals();
  54. for( i in 0...20 ) {
  55. var m = new h3d.scene.Mesh(sp, s3d);
  56. m.material.color.set(Math.random(), Math.random(), Math.random());
  57. m.material.color.normalize();
  58. m.scale(0.5 + Math.random() * 4);
  59. m.z = 2 + Math.random() * 5;
  60. var cx = (Math.random() - 0.5) * 20;
  61. var cy = (Math.random() - 0.5) * 20;
  62. var absPos = m.getAbsPos();
  63. m.cullingCollider = new h3d.col.Sphere(absPos.tx, absPos.ty, absPos.tz, hxd.Math.max(m.scaleZ, hxd.Math.max(m.scaleX, m.scaleY)));
  64. movingObjects.push({ m : m, pos : Math.random() * Math.PI * 2, cx : cx, cy : cy, ray : 8 + Math.random() * 50, speed : (0.5 + Math.random()) * 0.2 });
  65. }
  66. var pt = new h3d.scene.pbr.PointLight(s3d);
  67. pt.setPosition(0,0,15);
  68. pt.range = 40;
  69. pt.color.scale3(20);
  70. var sp = new h3d.scene.pbr.SpotLight(s3d);
  71. sp.setPosition(-30,-30,30);
  72. sp.setDirection(new h3d.Vector(1,2,-5));
  73. sp.range = 70;
  74. sp.angle = 70;
  75. sp.color.scale3(10);
  76. lights = [
  77. new h3d.scene.pbr.DirLight(new h3d.Vector(1,2,-5), s3d),
  78. pt,
  79. sp,
  80. ];
  81. for( l in lights )
  82. l.shadows.mode = Static;
  83. s3d.computeStatic();
  84. for( l in lights )
  85. l.shadows.mode = Dynamic;
  86. for( l in lights )
  87. l.visible = false;
  88. lights[curLight].visible = true;
  89. addChoice("Style",["Directional","Point","Spot", "All"],function(index) {
  90. for( l in lights )
  91. l.visible = false;
  92. curLight = index;
  93. if( curLight == lights.length ) {
  94. for( l in lights )
  95. l.visible = true;
  96. } else
  97. lights[curLight].visible = true;
  98. },curLight);
  99. var modes = ([Dynamic,Static,Mixed,None] : Array<h3d.pass.Shadows.RenderMode>);
  100. addChoice("Shadows",[for( m in modes ) m.getName()],function(sh) {
  101. for( l in lights )
  102. l.shadows.mode = modes[sh];
  103. });
  104. var baseLS = s3d.lightSystem;
  105. addCheck("DynCulling", function() return dynCullingEnable, function(b) {
  106. dynCullingEnable = !dynCullingEnable;
  107. dynCullingEnable ? addCullingCollider() : removeCullingCollider();
  108. });
  109. bitmap = new h2d.Bitmap(null, s2d);
  110. bitmap.scale(192 / 1024);
  111. bitmap.filter = h2d.filter.ColorMatrix.grayed();
  112. inf = addText();
  113. }
  114. override function update(dt:Float) {
  115. for( m in movingObjects ) {
  116. m.pos += m.speed / m.ray;
  117. m.m.x = m.cx + Math.cos(m.pos) * m.ray;
  118. m.m.y = m.cy + Math.sin(m.pos) * m.ray;
  119. var cc = hxd.impl.Api.downcast(m.m.cullingCollider, h3d.col.Sphere);
  120. if( cc != null ) {
  121. var absPos = m.m.getAbsPos();
  122. cc.x = absPos.tx;
  123. cc.y = absPos.ty;
  124. cc.z = absPos.tz;
  125. cc.r = hxd.Math.max(m.m.scaleZ, hxd.Math.max(m.m.scaleX, m.m.scaleY));
  126. }
  127. }
  128. var light = lights[curLight];
  129. var tex = light == null ? null : light.shadows.getShadowTex();
  130. bitmap.tile = tex == null || tex.flags.has(Cube) ? null : h2d.Tile.fromTexture(tex);
  131. bitmap.x = s2d.width - (bitmap.tile == null ? 0 : bitmap.tile.width) * bitmap.scaleX;
  132. inf.text = "Shadows Draw calls: "+ s3d.lightSystem.drawPasses;
  133. for( o in s3d ) {
  134. o.culled = false;
  135. }
  136. }
  137. static function main() {
  138. h3d.mat.MaterialSetup.current = new h3d.mat.PbrMaterialSetup();
  139. new Lights();
  140. }
  141. }