Advertisement
Kesta

NiTriShape Apply Transform

Sep 2nd, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. const
  2. PI = 3.14159265359;
  3.  
  4. procedure ApplyTransform(aTriShape: TwbNifBlock);
  5. var
  6. scale: single; // Transform scale.
  7. translation: array[1..3] of single; // Transform translation.
  8. rotation: array[1..9] of single; // Transform rotation matrix.
  9. y, p, r: single; // Yaw / Pitch / Roll for rotation matrix.
  10.  
  11. sinX, cosX, sinY, cosY, sinZ, cosZ: single; // temp trigonometry variables.
  12. pX, pY, pZ: single; // point coordinates.
  13. tX, tY, tZ: single; // transformed point coordinates.
  14.  
  15. TriShapeData: TwbNifBlock; // Shape Data to transform point.
  16.  
  17. // stuff to iterate over things.
  18. ref: TdfElement;
  19. i, n: integer;
  20. arr: TdfElement;
  21.  
  22. sl: TStringList;
  23. begin
  24.  
  25. scale := aTriShape.NativeValues['Transform\Scale'];
  26.  
  27. translation[1] := aTriShape.NativeValues['Transform\Translation\X'];
  28. translation[2] := aTriShape.NativeValues['Transform\Translation\Y'];
  29. translation[3] := aTriShape.NativeValues['Transform\Translation\Z'];
  30.  
  31. sl := TStringList.Create;
  32. sl.Delimiter := ' ';
  33. sl.StrictDelimiter := false;
  34. sl.DelimitedText := aTriShape.EditValues['Transform\Rotation'];
  35. y := StrToFloat(sl[0]) * PI / 180;
  36. p := StrToFloat(sl[1]) * PI / 180;
  37. r := StrToFloat(sl[2]) * PI / 180;
  38. sl.Free;
  39.  
  40. sinX := sin(y);
  41. cosX := cos(y);
  42. sinY := sin(p);
  43. cosY := cos(p);
  44. sinZ := sin(r);
  45. cosZ := cos(r);
  46.  
  47. rotation[1] := cosY * cosZ;
  48. rotation[2] := -cosY * sinZ;
  49. rotation[3] := sinY;
  50. rotation[4] := sinX * sinY * cosZ + sinZ * cosX;
  51. rotation[5] := cosX * cosZ - sinX * sinY * sinZ;
  52. rotation[6] := -sinX * cosY;
  53. rotation[7] := sinX * sinZ - cosX * sinY * cosZ;
  54. rotation[8] := cosX * sinY * sinZ + sinX * cosZ;
  55. rotation[9] := cosX * cosY;
  56.  
  57. for i := 0 to Pred(aTriShape.RefsCount) do begin
  58. ref := aTriShape.Refs[i];
  59. TriShapeData := TwbNifBlock(ref.LinksTo);
  60. if Assigned(TriShapeData) then begin
  61. if SameText(TriShapeData.BlockType, 'NiTriShapeData') then begin
  62. n := TriShapeData.NativeValues['Num Vertices'];
  63. arr := TriShapeData.Elements['Vertices'];
  64. for i := 0 to Pred(n) do begin
  65. pX := arr[i].NativeValues['X'];
  66. pY := arr[i].NativeValues['Y'];
  67. pZ := arr[i].NativeValues['Z'];
  68.  
  69. tX := (pX * rotation[1] + pY * rotation[2] + pZ * rotation[3]) * scale + translation[1];
  70. tY := (pX * rotation[4] + pY * rotation[5] + pZ * rotation[6]) * scale + translation[2];
  71. tZ := (pX * rotation[7] + pY * rotation[8] + pZ * rotation[9]) * scale + translation[3];
  72.  
  73. arr[i].EditValue := FloatToStr(tX) + ' ' + FloatToStr(tY) + ' '+ FloatToStr(tZ);
  74. end;
  75. end;
  76. end;
  77. end;
  78.  
  79. aTriShape.EditValues['Transform\Translation'] := '0.000000 0.000000 0.000000';
  80. aTriShape.EditValues['Transform\Rotation'] := '0.000000 0.000000 0.000000';
  81. aTriShape.EditValues['Transform\Scale'] := '1.000000';
  82. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement