-
Notifications
You must be signed in to change notification settings - Fork 63
Fix some general issues with Buoyant Objects (GerstnerWavesJobs.Registry Editor, physics-related) #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zallist
wants to merge
5
commits into
Unity-Technologies:main
Choose a base branch
from
Zallist:bonuses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix some general issues with Buoyant Objects (GerstnerWavesJobs.Registry Editor, physics-related) #11
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca3cdb8
Make Buoyant objects update on FixedUpdate if that's where they're do…
Zallist a5b7e57
Update BuoyantObject.cs to sync transforms during voxel slice.
Zallist a51859b
Revert unintended material update
Zallist 114465a
GerstnerWavesJobs didn't clear out Registry on cleanup, so Init never…
Zallist 04cfefa
Revert samplePoints.Dispose() change since it's done by the LocalToWo…
Zallist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,9 @@ public class BuoyantObject : MonoBehaviour | |
| private DebugDrawing[] _debugInfo; // For drawing force gizmos | ||
| [NonSerialized] public float PercentSubmerged; | ||
|
|
||
| private bool IsPhysicsBased => _buoyancyType == BuoyancyType.Physical || _buoyancyType == BuoyancyType.PhysicalVoxel; | ||
| private bool IsVoxelBased => _buoyancyType == BuoyancyType.NonPhysicalVoxel || _buoyancyType == BuoyancyType.PhysicalVoxel; | ||
|
|
||
| [ContextMenu("Initialize")] | ||
| private void Init() | ||
| { | ||
|
|
@@ -82,7 +85,7 @@ private void Init() | |
|
|
||
| private void SetupVoxels() | ||
| { | ||
| if (_buoyancyType == BuoyancyType.NonPhysicalVoxel || _buoyancyType == BuoyancyType.PhysicalVoxel) | ||
| if (IsVoxelBased) | ||
| { | ||
| SliceIntoVoxels(); | ||
| } | ||
|
|
@@ -106,6 +109,8 @@ private void OnEnable() | |
| _guid = gameObject.GetInstanceID(); | ||
| Init(); | ||
| LocalToWorldConversion(); | ||
|
|
||
| StartCoroutine(LateFixedUpdate()); | ||
| } | ||
|
|
||
| private void SetupColliders() | ||
|
|
@@ -121,35 +126,31 @@ private void SetupColliders() | |
|
|
||
| private void Update() | ||
| { | ||
| if (IsPhysicsBased) | ||
| return; | ||
|
|
||
| #if STATIC_EVERYTHING | ||
| var dt = 0.0f; | ||
| #else | ||
| var dt = Time.deltaTime; | ||
| #endif | ||
|
|
||
| switch (_buoyancyType) | ||
| { | ||
| case BuoyancyType.NonPhysical: | ||
| { | ||
| _samplePoints[0] = transform.position; | ||
| var t = transform; | ||
| var vec = t.position; | ||
| vec.y = Heights[0].y + waterLevelOffset; | ||
| t.position = vec; | ||
| var up = t.up; | ||
| t.up = Vector3.Slerp(up, _normals[0], dt); | ||
| break; | ||
| } | ||
| { | ||
| _samplePoints[0] = transform.position; | ||
| var t = transform; | ||
| var vec = t.position; | ||
| vec.y = Heights[0].y + waterLevelOffset; | ||
| t.position = vec; | ||
| var up = t.up; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know you didn't write the original, but the assignment to up isn't needed. t.up will be evaluated first in the next line before overwriting it. t.up = Vector3.Slerp(t.up, _normals[0], dt) |
||
| t.up = Vector3.Slerp(up, _normals[0], dt); | ||
| break; | ||
| } | ||
| case BuoyancyType.NonPhysicalVoxel: | ||
| // do the voxel non-physical | ||
| break; | ||
| case BuoyancyType.Physical: | ||
| LocalToWorldJob.CompleteJob(_guid); | ||
| GetVelocityPoints(); | ||
| break; | ||
| case BuoyancyType.PhysicalVoxel: | ||
| LocalToWorldJob.CompleteJob(_guid); | ||
| GetVelocityPoints(); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(); | ||
| } | ||
|
|
@@ -160,38 +161,51 @@ private void Update() | |
|
|
||
| private void FixedUpdate() | ||
| { | ||
| if (!IsPhysicsBased) | ||
| return; | ||
|
|
||
| var submergedAmount = 0f; | ||
|
|
||
|
|
||
| LocalToWorldJob.CompleteJob(_guid); | ||
| GetVelocityPoints(); | ||
| GerstnerWavesJobs.UpdateSamplePoints(ref _samplePoints, _guid); | ||
| GerstnerWavesJobs.GetData(_guid, ref Heights, ref _normals); | ||
|
|
||
| switch (_buoyancyType) | ||
| { | ||
| case BuoyancyType.PhysicalVoxel: | ||
| { | ||
| LocalToWorldJob.CompleteJob(_guid); | ||
| //Debug.Log("new pass: " + gameObject.name); | ||
| UnityPhysics.autoSyncTransforms = false; | ||
|
|
||
| for (var i = 0; i < _voxels.Length; i++) | ||
| BuoyancyForce(_samplePoints[i], _velocity[i], Heights[i].y + waterLevelOffset, ref submergedAmount, ref _debugInfo[i]); | ||
| UnityPhysics.SyncTransforms(); | ||
| UnityPhysics.autoSyncTransforms = true; | ||
| UpdateDrag(submergedAmount); | ||
| break; | ||
| } | ||
| { | ||
| var autoSyncTransforms = UnityPhysics.autoSyncTransforms; | ||
| UnityPhysics.autoSyncTransforms = false; | ||
|
|
||
| for (var i = 0; i < _voxels.Length; i++) | ||
| BuoyancyForce(_samplePoints[i], _velocity[i], Heights[i].y + waterLevelOffset, ref submergedAmount, ref _debugInfo[i]); | ||
|
|
||
| UnityPhysics.SyncTransforms(); | ||
| UnityPhysics.autoSyncTransforms = autoSyncTransforms; | ||
|
|
||
| UpdateDrag(submergedAmount); | ||
| break; | ||
| } | ||
| case BuoyancyType.Physical: | ||
| //LocalToWorldJob.CompleteJob(_guid); | ||
| BuoyancyForce(Vector3.zero, _velocity[0], Heights[0].y + waterLevelOffset, ref submergedAmount, ref _debugInfo[0]); | ||
| //UpdateDrag(submergedAmount); | ||
| break; | ||
| case BuoyancyType.NonPhysical: | ||
| break; | ||
| case BuoyancyType.NonPhysicalVoxel: | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(); | ||
| } | ||
| } | ||
|
|
||
| private void LateUpdate() { LocalToWorldConversion(); } | ||
| private static readonly WaitForFixedUpdate YieldLateFixedUpdate = new WaitForFixedUpdate(); | ||
| private System.Collections.IEnumerator LateFixedUpdate() | ||
| { | ||
| while (true) | ||
| { | ||
| yield return YieldLateFixedUpdate; | ||
|
|
||
| LocalToWorldConversion(); | ||
| } | ||
| } | ||
|
|
||
| private void OnDestroy() | ||
| { | ||
|
|
@@ -200,7 +214,9 @@ private void OnDestroy() | |
|
|
||
| void CleanUp() | ||
| { | ||
| if (_buoyancyType == BuoyancyType.Physical || _buoyancyType == BuoyancyType.PhysicalVoxel) | ||
| StopAllCoroutines(); | ||
|
|
||
| if (IsPhysicsBased) | ||
| { | ||
| LocalToWorldJob.Cleanup(_guid); | ||
| } | ||
|
|
@@ -212,7 +228,7 @@ void CleanUp() | |
|
|
||
| private void LocalToWorldConversion() | ||
| { | ||
| if (_buoyancyType != BuoyancyType.Physical && _buoyancyType != BuoyancyType.PhysicalVoxel) return; | ||
| if (!IsPhysicsBased) return; | ||
|
|
||
| var transformMatrix = transform.localToWorldMatrix; | ||
| LocalToWorldJob.ScheduleJob(_guid, transformMatrix); | ||
|
|
@@ -258,6 +274,7 @@ private void SliceIntoVoxels() | |
| var size = t.localScale; | ||
| t.SetPositionAndRotation(Vector3.zero, Quaternion.identity); | ||
| t.localScale = Vector3.one; | ||
| UnityPhysics.SyncTransforms(); | ||
|
|
||
| _voxels = null; | ||
| var points = new List<Vector3>(); | ||
|
|
@@ -294,6 +311,8 @@ private void SliceIntoVoxels() | |
| _voxels = points.ToArray(); | ||
| t.SetPositionAndRotation(pos, rot); | ||
| t.localScale = size; | ||
| UnityPhysics.SyncTransforms(); | ||
|
|
||
| var voxelVolume = Mathf.Pow(voxelResolution, 3f) * _voxels.Length; | ||
| var rawVolume = rawBounds.size.x * rawBounds.size.y * rawBounds.size.z; | ||
| volume = Mathf.Min(rawVolume, voxelVolume); | ||
|
|
@@ -387,7 +406,7 @@ private void OnDrawGizmosSelected() | |
| water.y = debug.WaterHeight; | ||
| Gizmos.DrawLine(debug.Position, water); // draw the water line | ||
| Gizmos.DrawSphere(water, gizmoSize * 4f); | ||
| if(_buoyancyType == BuoyancyType.Physical || _buoyancyType == BuoyancyType.PhysicalVoxel) | ||
| if(IsPhysicsBased) | ||
| { | ||
| Gizmos.color = Color.red; | ||
| Gizmos.DrawRay(debug.Position, debug.Force / _rb.mass); // draw force | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does performance matter in this script? There's some overhead added by using a property getter, plus I'm pretty sure it will do the cast each time this is accessed.