Guest User

Untitled

a guest
Aug 27th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. // NOTE(Constantine):
  2. // Reconstructing 3D point from 2D UV coordinates of a 3D triangle using barycentric coordinates in Game Script C
  3. // https://discord.gg/D7pKPw4kFf
  4. // https://discord.com/channels/908452801678561291/908452802165112885/1410190619644788797
  5.  
  6. #include "gamescriptc.h"
  7. #include <stdio.h> // For printf
  8. #include <math.h> // for sqrt
  9.  
  10. typedef struct {
  11. float x, y, z;
  12. } Vec3;
  13.  
  14. typedef struct {
  15. float u, v;
  16. } UV;
  17.  
  18. Vec3 reconstruct3DPosition(Vec3 v1, Vec3 v2, Vec3 v3, UV uv) {
  19. float w = 1.0f - uv.u - uv.v;
  20.  
  21. Vec3 point;
  22. point.x = w * v1.x + uv.u * v2.x + uv.v * v3.x;
  23. point.y = w * v1.y + uv.u * v2.y + uv.v * v3.y;
  24. point.z = w * v1.z + uv.u * v2.z + uv.v * v3.z;
  25.  
  26. return point;
  27. }
  28.  
  29. cfn frame() {
  30. framestart();
  31.  
  32. UV uv = {0.0f, 0.0f};
  33.  
  34. if (getCurrentFrame() == 0) {
  35. Number inputU = globalArrayPersistentNew8Bit("inputU", 16);
  36. Number inputV = globalArrayPersistentNew8Bit("inputV", 16);
  37. pointerSetString(inputU, 0, "0.2");
  38. pointerSetString(inputV, 0, "0.3");
  39. }
  40. imguiBegin();
  41. if (imguiWindowBegin("UV coordinates", 1, 0) == true) {
  42. Number inputU = globalArrayPersistentNew8Bit("inputU", 16);
  43. Number inputV = globalArrayPersistentNew8Bit("inputV", 16);
  44. imguiInputText("U", inputU, 0, 16);
  45. imguiInputText("V", inputV, 0, 16);
  46. String stringU = pointerGetString(inputU);
  47. String stringV = pointerGetString(inputV);
  48. uv.u = interpretStringToFloat(stringU);
  49. uv.v = interpretStringToFloat(stringV);
  50. }
  51. imguiWindowEnd();
  52. imguiEnd();
  53.  
  54. Vec3 v1 = {1.0f, 0.0f, 0.0f};
  55. Vec3 v2 = {0.0f, 0.0f, 1.0f};
  56. Vec3 v3 = {0.0f, 1.0f, 0.0f};
  57.  
  58. if (getCurrentFrame() == 0) {
  59. gizmoSetVector("v1", v1.x, v1.y, v1.z);
  60. gizmoSetVector("v2", v2.x, v2.y, v2.z);
  61. gizmoSetVector("v3", v3.x, v3.y, v3.z);
  62. }
  63.  
  64. NumberArray gv1 = gizmoGetVector("v1");
  65. NumberArray gv2 = gizmoGetVector("v2");
  66. NumberArray gv3 = gizmoGetVector("v3");
  67.  
  68. v1.x = gv1[0];
  69. v1.y = gv1[1];
  70. v1.z = gv1[2];
  71.  
  72. v2.x = gv2[0];
  73. v2.y = gv2[1];
  74. v2.z = gv2[2];
  75.  
  76. v3.x = gv3[0];
  77. v3.y = gv3[1];
  78. v3.z = gv3[2];
  79.  
  80. Vec3 reconstructedPoint = reconstruct3DPosition(v1, v2, v3, uv);
  81.  
  82. printf("Reconstructed 3D Position: (%f, %f, %f)\n", reconstructedPoint.x, reconstructedPoint.y, reconstructedPoint.z);
  83.  
  84. drawTriangle("", v1.x,v1.y,v1.z, v2.x,v2.y,v2.z, v3.x,v3.y,v3.z, 255,45,85,255);
  85. drawPoint("", reconstructedPoint.x, reconstructedPoint.y, reconstructedPoint.z, 0.5f, 255,0,255,255);
  86.  
  87. // Gizmo
  88. Number currentMode = globalArrayPersistentNew8Bit("gizmoCurrentMode", 1 * 4);
  89. {
  90. if (pointerGetUnsignedInteger(currentMode, 0) == 0) {
  91. // https://discord.gg/D7pKPw4kFf, https://discord.com/channels/908452801678561291/908452802165112885/918489067543466054
  92. if (windowIsFocused() && windowIsHovered()) {
  93. defaultCameraDefaultControlEnable();
  94. } else {
  95. defaultCameraDefaultControlDisable();
  96. }
  97. } else {
  98. defaultCameraDefaultControlDisable();
  99. }
  100. if (keyboardGetGlfwKeyEvent(258) == 0 && mouseGetButtonIsPressed(2)) { // Change current modelling mode: right mouse button
  101. Number v = pointerGetUnsignedInteger(currentMode, 0);
  102. v = v == 0 ? 1 : 0;
  103. pointerSetUnsignedInteger(currentMode, 0, v);
  104. gizmoSetMode("v1", v);
  105. gizmoSetMode("v2", v);
  106. gizmoSetMode("v3", v);
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment