Object3D.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package hrt.prefab;
  2. import hxd.Math;
  3. using Lambda;
  4. class Object3D extends Prefab {
  5. public var x : Float = 0.;
  6. public var y : Float = 0.;
  7. public var z : Float = 0.;
  8. public var scaleX : Float = 1.;
  9. public var scaleY : Float = 1.;
  10. public var scaleZ : Float = 1.;
  11. public var rotationX : Float = 0.;
  12. public var rotationY : Float = 0.;
  13. public var rotationZ : Float = 0.;
  14. public var visible : Bool = true;
  15. public function loadTransform(t) {
  16. x = t.x;
  17. y = t.y;
  18. z = t.z;
  19. scaleX = t.scaleX;
  20. scaleY = t.scaleY;
  21. scaleZ = t.scaleZ;
  22. rotationX = t.rotationX;
  23. rotationY = t.rotationY;
  24. rotationZ = t.rotationZ;
  25. }
  26. public function saveTransform() {
  27. return { x : x, y : y, z : z, scaleX : scaleX, scaleY : scaleY, scaleZ : scaleZ, rotationX : rotationX, rotationY : rotationY, rotationZ : rotationZ };
  28. }
  29. override function load( obj : Dynamic ) {
  30. x = obj.x == null ? 0. : obj.x;
  31. y = obj.y == null ? 0. : obj.y;
  32. z = obj.z == null ? 0. : obj.z;
  33. scaleX = obj.scaleX == null ? 1. : obj.scaleX;
  34. scaleY = obj.scaleY == null ? 1. : obj.scaleY;
  35. scaleZ = obj.scaleZ == null ? 1. : obj.scaleZ;
  36. rotationX = obj.rotationX == null ? 0. : obj.rotationX;
  37. rotationY = obj.rotationY == null ? 0. : obj.rotationY;
  38. rotationZ = obj.rotationZ == null ? 0. : obj.rotationZ;
  39. visible = obj.visible == null ? true : obj.visible;
  40. }
  41. override function makeInstance(ctx:Context):Context {
  42. ctx = ctx.clone(this);
  43. ctx.local3d = new h3d.scene.Object(ctx.local3d);
  44. ctx.local3d.name = name;
  45. updateInstance(ctx);
  46. return ctx;
  47. }
  48. override function save() {
  49. var o : Dynamic = {};
  50. if( x != 0 ) o.x = x;
  51. if( y != 0 ) o.y = y;
  52. if( z != 0 ) o.z = z;
  53. if( scaleX != 1 ) o.scaleX = scaleX;
  54. if( scaleY != 1 ) o.scaleY = scaleY;
  55. if( scaleZ != 1 ) o.scaleZ = scaleZ;
  56. if( rotationX != 0 ) o.rotationX = rotationX;
  57. if( rotationY != 0 ) o.rotationY = rotationY;
  58. if( rotationZ != 0 ) o.rotationZ = rotationZ;
  59. if( !visible ) o.visible = visible;
  60. return o;
  61. }
  62. public function setTransform(mat : h3d.Matrix) {
  63. var rot = mat.getEulerAngles();
  64. x = mat.tx;
  65. y = mat.ty;
  66. z = mat.tz;
  67. var s = mat.getScale();
  68. scaleX = s.x;
  69. scaleY = s.y;
  70. scaleZ = s.z;
  71. rotationX = Math.radToDeg(rot.x);
  72. rotationY = Math.radToDeg(rot.y);
  73. rotationZ = Math.radToDeg(rot.z);
  74. }
  75. public function getTransform() {
  76. var m = new h3d.Matrix();
  77. m.initScale(scaleX, scaleY, scaleZ);
  78. m.rotate(Math.degToRad(rotationX), Math.degToRad(rotationY), Math.degToRad(rotationZ));
  79. m.translate(x, y, z);
  80. return m;
  81. }
  82. public function applyPos( o : h3d.scene.Object ) {
  83. o.x = x;
  84. o.y = y;
  85. o.z = z;
  86. o.scaleX = scaleX;
  87. o.scaleY = scaleY;
  88. o.scaleZ = scaleZ;
  89. o.setRotation(Math.degToRad(rotationX), Math.degToRad(rotationY), Math.degToRad(rotationZ));
  90. }
  91. override function updateInstance( ctx: Context, ?propName : String ) {
  92. var o = ctx.local3d;
  93. o.x = x;
  94. o.y = y;
  95. o.z = z;
  96. if(propName == null || propName.indexOf("scale") == 0) {
  97. o.scaleX = scaleX;
  98. o.scaleY = scaleY;
  99. o.scaleZ = scaleZ;
  100. }
  101. if(propName == null || propName.indexOf("rotation") == 0)
  102. o.setRotation(Math.degToRad(rotationX), Math.degToRad(rotationY), Math.degToRad(rotationZ));
  103. if(propName == null || propName == "visible")
  104. o.visible = visible;
  105. }
  106. override function removeInstance(ctx: Context):Bool {
  107. if(ctx.local3d != null)
  108. ctx.local3d.remove();
  109. return true;
  110. }
  111. #if editor
  112. override function edit( ctx : EditContext ) {
  113. var props = new hide.Element('
  114. <div class="group" name="Position">
  115. <dl>
  116. <dt>X</dt><dd><input type="range" min="-10" max="10" value="0" field="x"/></dd>
  117. <dt>Y</dt><dd><input type="range" min="-10" max="10" value="0" field="y"/></dd>
  118. <dt>Z</dt><dd><input type="range" min="-10" max="10" value="0" field="z"/></dd>
  119. <dt>Scale X</dt><dd><input type="range" min="0" max="5" value="1" field="scaleX"/></dd>
  120. <dt>Scale Y</dt><dd><input type="range" min="0" max="5" value="1" field="scaleY"/></dd>
  121. <dt>Scale Z</dt><dd><input type="range" min="0" max="5" value="1" field="scaleZ"/></dd>
  122. <dt>Rotation X</dt><dd><input type="range" min="-180" max="180" value="0" field="rotationX" /></dd>
  123. <dt>Rotation Y</dt><dd><input type="range" min="-180" max="180" value="0" field="rotationY" /></dd>
  124. <dt>Rotation Z</dt><dd><input type="range" min="-180" max="180" value="0" field="rotationZ" /></dd>
  125. <dt>Visible</dt><dd><input type="checkbox" field="visible"/></dd>
  126. </dl>
  127. </div>
  128. ');
  129. ctx.properties.add(props, this, function(pname) {
  130. ctx.onChange(this, pname);
  131. });
  132. }
  133. override function getHideProps() : HideProps {
  134. // Check children
  135. return {
  136. icon : children == null || children.length > 0 ? "folder-open" : "genderless",
  137. name : "Group"
  138. };
  139. }
  140. #end
  141. override function getDefaultName() {
  142. return type == "object" ? "group" : super.getDefaultName();
  143. }
  144. static var _ = Library.register("object", Object3D);
  145. }