Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##################GenerateSmoothChunks#######################
- public void GenerateSmoothChunks(Rigidbody[] positions, uint smoothingLevels)
- {
- using (m_GenerateSmoothChunksPerfMarker.Auto())
- {
- smoothChunks.Clear();
- smoothSections = 0;
- smoothLength = 0;
- //if (!Application.isPlaying)
- // actor.RebuildElementsFromConstraints();
- AllocateRawChunks(positions);
- //w2l = actor.transform.worldToLocalMatrix;
- //w2lRotation = w2l.rotation;
- // keep track of the first element of each chunk
- int chunkStart = 0;
- ObiPathFrame frame_0 = new ObiPathFrame(); // "next" frame
- ObiPathFrame frame_1 = new ObiPathFrame(); // current frame
- ObiPathFrame frame_2 = new ObiPathFrame(); // previous frame
- // generate curve for each rope chunk:
- for (int i = 0; i < rawChunks.Count; ++i)
- {
- int elementCount = rawChunks[i].Count - 1;
- // Initialize frames:
- frame_0.Reset();
- frame_1.Reset();
- frame_2.Reset();
- PathFrameFromParticle(positions, ref frame_1, chunkStart, false);
- frame_2 = frame_1;
- for (int m = 1; m <= rawChunks[i].Count; ++m)
- {
- int index;
- if (m >= elementCount)
- // second particle of last element in the chunk.
- index = chunkStart + elementCount - 1;
- else
- //first particle of current element.
- index = chunkStart + m;
- // generate curve frame from particle:
- PathFrameFromParticle(positions, ref frame_0, index);
- //if (actor.usesOrientedParticles)
- //{
- // // copy frame directly.
- // frame_2 = frame_1;
- //}
- //else
- {
- // perform parallel transport, using forward / backward average to calculate tangent.
- frame_1.tangent = ((frame_1.position - frame_2.position) + (frame_0.position - frame_1.position)).normalized;
- frame_2.Transport(frame_1, twist);
- }
- // in case we wrapped around the rope, average first and last frames:
- if (chunkStart + m > positions.Length)
- {
- frame_2 = rawChunks[0][0] = 0.5f * frame_2 + 0.5f * rawChunks[0][0];
- }
- frame_1 = frame_0;
- rawChunks[i][m - 1] = frame_2;
- }
- // increment chunkStart by the amount of elements in this chunk:
- chunkStart += elementCount;
- // adaptive curvature-based decimation:
- if (Decimate(rawChunks[i], smoothChunks[i], decimation))
- {
- // if any decimation took place, swap raw and smooth chunks:
- var aux = rawChunks[i];
- rawChunks[i] = smoothChunks[i];
- smoothChunks[i] = aux;
- }
- // get smooth curve points:
- Chaikin(rawChunks[i], smoothChunks[i], smoothingLevels);
- // count total curve sections and total curve length:
- smoothSections += smoothChunks[i].Count;
- smoothLength += CalculateChunkLength(smoothChunks[i]);
- }
- }
- OnCurveGenerated(null);
- }
- #######################PathFrameFromParticle###############################
- private void PathFrameFromParticle(Rigidbody[] positions, ref ObiPathFrame frame, int arrayIndex, bool interpolateOrientation = true)
- {
- // Update current frame values from particles:
- frame.position = positions[arrayIndex].worldCenterOfMass;
- //frame.thickness = actor.GetParticleMaxRadius(particleIndex);
- frame.thickness = 0.1f;
- //frame.color = actor.GetParticleColor(particleIndex);
- frame.color = Color.red;
- // Use particle orientation if possible:
- //if (actor.usesOrientedParticles)
- //{
- //Quaternion current = actor.GetParticleOrientation(particleIndex);
- Quaternion current = positions[arrayIndex].rotation;
- //Quaternion previous = actor.GetParticleOrientation(Mathf.Max(0, particleIndex - 1));
- Quaternion previous = positions[Mathf.Max(0, arrayIndex - 1)].rotation;
- Quaternion average = w2lRotation * (interpolateOrientation ? Quaternion.SlerpUnclamped(current, previous, 0.5f) : current);
- frame.normal = average * Vector3.up;
- frame.binormal = average * Vector3.right;
- frame.tangent = average * Vector3.forward;
- //}
- }
- #############################AllocateRawChunks#####################################
- private void AllocateRawChunks(Rigidbody[] positions)
- {
- using (m_AllocateRawChunksPerfMarker.Auto())
- {
- rawChunks.Clear();
- AllocateChunk(positions.Length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement