random_node.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. type random_node_t = {
  2. base?: logic_node_t;
  3. };
  4. let random_node_a: i32;
  5. let random_node_b: i32;
  6. let random_node_c: i32;
  7. let random_node_d: i32 = -1;
  8. function random_node_create(arg: any): random_node_t {
  9. let n: random_node_t = {};
  10. n.base = logic_node_create();
  11. n.base.get = random_node_get;
  12. return n;
  13. }
  14. function random_node_get(self: random_node_t, from: i32): logic_node_value_t {
  15. let min: f32 = logic_node_input_get(self.base.inputs[0])._f32;
  16. let max: f32 = logic_node_input_get(self.base.inputs[1])._f32;
  17. let v: logic_node_value_t = { _f32: min + random_node_get_float() * (max - min) };
  18. return v;
  19. }
  20. function random_node_get_int(): i32 {
  21. if (random_node_d == -1) {
  22. random_node_set_seed(352124);
  23. }
  24. // Courtesy of https://github.com/Kode/Kha/blob/main/Sources/kha/math/Random.hx
  25. let t: i32 = (random_node_a + random_node_b | 0) + random_node_d | 0;
  26. random_node_d = random_node_d + 1 | 0;
  27. random_node_a = random_node_b ^ random_node_b >>> 9;
  28. random_node_b = random_node_c + (random_node_c << 3) | 0;
  29. random_node_c = random_node_c << 21 | random_node_c >>> 11;
  30. random_node_c = random_node_c + t | 0;
  31. return t & 0x7fffffff;
  32. }
  33. function random_node_set_seed(seed: i32) {
  34. random_node_d = seed;
  35. random_node_a = 0x36aef51a;
  36. random_node_b = 0x21d4b3eb;
  37. random_node_c = 0xf2517abf;
  38. // Immediately skip a few possibly poor results the easy way
  39. for (let i: i32 = 0; i < 15; ++i) {
  40. random_node_get_int();
  41. }
  42. }
  43. function random_node_get_seed(): i32 {
  44. return random_node_d;
  45. }
  46. function random_node_get_float(): f32 {
  47. return random_node_get_int() / 0x7fffffff;
  48. }
  49. let random_node_def: zui_node_t = {
  50. id: 0,
  51. name: _tr("Random"),
  52. type: "random_node",
  53. x: 0,
  54. y: 0,
  55. color: 0xffb34f5a,
  56. inputs: [
  57. {
  58. id: 0,
  59. node_id: 0,
  60. name: _tr("Min"),
  61. type: "VALUE",
  62. color: 0xffa1a1a1,
  63. default_value: f32_array_create_x(0.0),
  64. min: 0.0,
  65. max: 1.0,
  66. precision: 100,
  67. display: 0
  68. },
  69. {
  70. id: 0,
  71. node_id: 0,
  72. name: _tr("Max"),
  73. type: "VALUE",
  74. color: 0xffa1a1a1,
  75. default_value: f32_array_create_x(1.0),
  76. min: 0.0,
  77. max: 1.0,
  78. precision: 100,
  79. display: 0
  80. }
  81. ],
  82. outputs: [
  83. {
  84. id: 0,
  85. node_id: 0,
  86. name: _tr("Value"),
  87. type: "VALUE",
  88. color: 0xffa1a1a1,
  89. default_value: f32_array_create_x(0.5),
  90. min: 0.0,
  91. max: 1.0,
  92. precision: 100,
  93. display: 0
  94. }
  95. ],
  96. buttons: [],
  97. width: 0
  98. };