screen.d 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. module three.gfx.scene;
  2. /+++
  3. struct PositionGraph {
  4. struct InternalId {
  5. uint id;
  6. this(size_t depth, size_t position) {
  7. id = (depth & 0x000000ff) | (position << 8);
  8. }
  9. size_t depth() {
  10. return id & 0x000000ff;
  11. }
  12. size_t position() {
  13. return (id & 0xffffff00) >>> 8;
  14. }
  15. }
  16. enum initialDepth = 5;
  17. InternalId[Entity] _internalMapping; //fast entity -> component lookup
  18. SoAEntity[] _entity = new SoAEntity[initialDepth]; //fast component -> entity lookup
  19. SoAVec3[] _position = new SoAVec3[initialDepth];
  20. void createRootNode(Entity entity, Position position) {
  21. _appendNode(0, entity, position);
  22. }
  23. void createChildNode(Entity parent, Entity entity, Position position) {
  24. auto p = parent in _internalMapping;
  25. if(p is null) {
  26. //TODO: if not in there?
  27. }
  28. auto depth = p.depth + 1;
  29. // increase depth array if not large enouth
  30. if(_entity.length < depth) {
  31. _entity.length = depth;
  32. }
  33. // assign values
  34. append(depth, entity, position);
  35. }
  36. private:
  37. void _appendNode(size_t depth, Entity entity, Position position) {
  38. _entity[depth].append(entity);
  39. _position[depth].x.append(position.x);
  40. _position[depth].y.append(position.y);
  41. _position[depth].z.append(position.z);
  42. _internalMapping[entity] = InternalId(depth, _entity[depth].length];
  43. }
  44. }
  45. struct SoAEntity() {
  46. Entity[] entity;
  47. }
  48. struct SoAVec3() {
  49. float[] x;
  50. float[] y;
  51. float[] z;
  52. }
  53. struct SoAQuat() {
  54. float[] x;
  55. float[] y;
  56. float[] z;
  57. float[] w;
  58. }
  59. struct LocalTransformCM {
  60. private:
  61. size_t _length = 0;
  62. size_t _capacity = 0;
  63. ubyte* _data;
  64. mixin Vec3 _position;
  65. mixin Quat _orientation;
  66. mixin Vec3 _scale;
  67. //~ mixin Vec3 _velocity;
  68. //~ mixin Vec3 _acceleration;
  69. }
  70. class DebugNameCM {
  71. void setDebugName(Entity e, string name);
  72. string debugName(Entity e);
  73. }
  74. void vector(string op)(ref Vectors r, Vectors a, Vectors b) {
  75. mixin("r.x[] = a.x[]" ~ op ~ "b.x[];");
  76. mixin("r.y[] = a.y[]" ~ op ~ "b.y[];");
  77. mixin("r.z[] = a.z[]" ~ op ~ "b.z[];");
  78. }
  79. void quat_mul(ref Quaternions r, Quaternions a, Quaternions b) {
  80. r.x[] = a.w[] * b.x[] + a.x[] * b.w[] + b.y[] * b.z[] - a.z[] * b.y[];
  81. r.y[] = a.w[] * b.y[] - a.x[] * b.z[] + a.y[] * b.w[] + a.z[] * b.x[];
  82. r.z[] = a.w[] * b.z[] + a.x[] * b.y[] - a.y[] * b.x[] + a.z[] * b.w[];
  83. r.w[] = a.w[] * b.w[] - a.x[] * b.x[] - a.y[] * b.y[] - a.z[] * b.z[];
  84. }
  85. struct Positions {
  86. float[] x;
  87. float[] z;
  88. float[] x;
  89. }
  90. struct Orientations {
  91. float[] x;
  92. float[] z;
  93. float[] x;
  94. float[] w;
  95. }
  96. struct Scales {
  97. float[] x;
  98. float[] z;
  99. float[] x;
  100. }
  101. struct Position {
  102. float x;
  103. float z;
  104. float x;
  105. }
  106. struct Orientation {
  107. float x;
  108. float z;
  109. float x;
  110. float w;
  111. }
  112. struct Scale {
  113. float x;
  114. float z;
  115. float x;
  116. }
  117. struct Scene {
  118. Entity _entities;
  119. Positions _positions;
  120. Orientations _orientations;
  121. Scales _scales;
  122. int[] _parents;
  123. /// keep them sorted by tree depth, so we iterate the tree breath first when iterating the array
  124. void insert(Entity entity, int parent, Position position, Orientation orientation, Scale scale) {
  125. auto idx = parents.countUntil!(a=>a>b)(parent);
  126. if(idx == -1) { // insert at end
  127. _entities.append(entity);
  128. _parents.append(parent);
  129. _positions.x.append(position.x);
  130. _positions.y.append(position.y);
  131. _positions.z.append(position.z);
  132. _orientations.x.append(orientation.x);
  133. _orientations.y.append(orientation.y);
  134. _orientations.z.append(orientation.z);
  135. _orientations.w.append(orientation.w);
  136. _scales.x.append(scale.x);
  137. _scales.y.append(scale.y);
  138. _scales.z.append(scale.z);
  139. }
  140. else { // insert at idx and move all right of idx
  141. _entities.insertInPlace(idx, entity);
  142. _parents.insertInPlace(idx, parent);
  143. _positions.x.insertInPlace(idx, position.x);
  144. _positions.y.insertInPlace(idx, position.y);
  145. _positions.z.insertInPlace(idx, position.z);
  146. _orientations.x.insertInPlace(idx, orientation.x);
  147. _orientations.y.insertInPlace(idx, orientation.y);
  148. _orientations.z.insertInPlace(idx, orientation.z);
  149. _orientations.w.insertInPlace(idx, orientation.w);
  150. _scales.x.insertInPlace(idx, scale.x);
  151. _scales.y.insertInPlace(idx, scale.y);
  152. _scales.z.insertInPlace(idx, scale.z);
  153. }
  154. }
  155. }
  156. // 4
  157. // 2 3 5 6
  158. // ^x
  159. //
  160. // 1
  161. // 2 3 5 6
  162. //^x
  163. //
  164. // 9
  165. // 2 3 5 6
  166. // ^x
  167. //
  168. +++/