1
1

screen.d 4.2 KB

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