Advertisement
Guest User

Untitled

a guest
Feb 27th, 2023
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. ##################GenerateSmoothChunks#######################
  2. public void GenerateSmoothChunks(Rigidbody[] positions, uint smoothingLevels)
  3. {
  4. using (m_GenerateSmoothChunksPerfMarker.Auto())
  5. {
  6. smoothChunks.Clear();
  7. smoothSections = 0;
  8. smoothLength = 0;
  9.  
  10. //if (!Application.isPlaying)
  11. // actor.RebuildElementsFromConstraints();
  12.  
  13. AllocateRawChunks(positions);
  14.  
  15. //w2l = actor.transform.worldToLocalMatrix;
  16. //w2lRotation = w2l.rotation;
  17.  
  18. // keep track of the first element of each chunk
  19. int chunkStart = 0;
  20.  
  21. ObiPathFrame frame_0 = new ObiPathFrame(); // "next" frame
  22. ObiPathFrame frame_1 = new ObiPathFrame(); // current frame
  23. ObiPathFrame frame_2 = new ObiPathFrame(); // previous frame
  24.  
  25. // generate curve for each rope chunk:
  26. for (int i = 0; i < rawChunks.Count; ++i)
  27. {
  28. int elementCount = rawChunks[i].Count - 1;
  29.  
  30. // Initialize frames:
  31. frame_0.Reset();
  32. frame_1.Reset();
  33. frame_2.Reset();
  34.  
  35. PathFrameFromParticle(positions, ref frame_1, chunkStart, false);
  36.  
  37. frame_2 = frame_1;
  38.  
  39. for (int m = 1; m <= rawChunks[i].Count; ++m)
  40. {
  41.  
  42. int index;
  43. if (m >= elementCount)
  44. // second particle of last element in the chunk.
  45. index = chunkStart + elementCount - 1;
  46. else
  47. //first particle of current element.
  48. index = chunkStart + m;
  49.  
  50. // generate curve frame from particle:
  51. PathFrameFromParticle(positions, ref frame_0, index);
  52.  
  53. //if (actor.usesOrientedParticles)
  54. //{
  55. // // copy frame directly.
  56. // frame_2 = frame_1;
  57. //}
  58. //else
  59. {
  60. // perform parallel transport, using forward / backward average to calculate tangent.
  61. frame_1.tangent = ((frame_1.position - frame_2.position) + (frame_0.position - frame_1.position)).normalized;
  62. frame_2.Transport(frame_1, twist);
  63. }
  64.  
  65. // in case we wrapped around the rope, average first and last frames:
  66. if (chunkStart + m > positions.Length)
  67. {
  68. frame_2 = rawChunks[0][0] = 0.5f * frame_2 + 0.5f * rawChunks[0][0];
  69. }
  70.  
  71. frame_1 = frame_0;
  72.  
  73. rawChunks[i][m - 1] = frame_2;
  74. }
  75.  
  76. // increment chunkStart by the amount of elements in this chunk:
  77. chunkStart += elementCount;
  78.  
  79. // adaptive curvature-based decimation:
  80. if (Decimate(rawChunks[i], smoothChunks[i], decimation))
  81. {
  82. // if any decimation took place, swap raw and smooth chunks:
  83. var aux = rawChunks[i];
  84. rawChunks[i] = smoothChunks[i];
  85. smoothChunks[i] = aux;
  86. }
  87.  
  88. // get smooth curve points:
  89. Chaikin(rawChunks[i], smoothChunks[i], smoothingLevels);
  90.  
  91. // count total curve sections and total curve length:
  92. smoothSections += smoothChunks[i].Count;
  93. smoothLength += CalculateChunkLength(smoothChunks[i]);
  94. }
  95. }
  96.  
  97. OnCurveGenerated(null);
  98. }
  99.  
  100. #######################PathFrameFromParticle###############################
  101. private void PathFrameFromParticle(Rigidbody[] positions, ref ObiPathFrame frame, int arrayIndex, bool interpolateOrientation = true)
  102. {
  103. // Update current frame values from particles:
  104. frame.position = positions[arrayIndex].worldCenterOfMass;
  105. //frame.thickness = actor.GetParticleMaxRadius(particleIndex);
  106. frame.thickness = 0.1f;
  107. //frame.color = actor.GetParticleColor(particleIndex);
  108. frame.color = Color.red;
  109.  
  110. // Use particle orientation if possible:
  111. //if (actor.usesOrientedParticles)
  112. //{
  113. //Quaternion current = actor.GetParticleOrientation(particleIndex);
  114. Quaternion current = positions[arrayIndex].rotation;
  115. //Quaternion previous = actor.GetParticleOrientation(Mathf.Max(0, particleIndex - 1));
  116. Quaternion previous = positions[Mathf.Max(0, arrayIndex - 1)].rotation;
  117. Quaternion average = w2lRotation * (interpolateOrientation ? Quaternion.SlerpUnclamped(current, previous, 0.5f) : current);
  118. frame.normal = average * Vector3.up;
  119. frame.binormal = average * Vector3.right;
  120. frame.tangent = average * Vector3.forward;
  121. //}
  122. }
  123.  
  124. #############################AllocateRawChunks#####################################
  125. private void AllocateRawChunks(Rigidbody[] positions)
  126. {
  127. using (m_AllocateRawChunksPerfMarker.Auto())
  128. {
  129. rawChunks.Clear();
  130.  
  131. AllocateChunk(positions.Length);
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement