├── .gitignore ├── LICENSE ├── README.md ├── img ├── contact-with-friction.gif └── point-constraint-with-rotation.gif ├── slides ├── 01-constraint-basics-chinese.odp ├── 01-constraint-basics-english.odp ├── 02-contact-constraints-chinese.odp ├── 02-contact-constraints-english.odp ├── 03-soft-constraints-chinese.odp └── 03-soft-constraints-english.odp └── src ├── .gitignore └── Physics Constraints ├── Assets ├── CjLib.meta ├── CjLib │ ├── Script.meta │ ├── Script │ │ ├── Debug.meta │ │ ├── Debug │ │ │ ├── Components.meta │ │ │ ├── Components │ │ │ │ ├── DrawArc.cs │ │ │ │ ├── DrawArc.cs.meta │ │ │ │ ├── DrawArrow.cs │ │ │ │ ├── DrawArrow.cs.meta │ │ │ │ ├── DrawArrowEditor.cs │ │ │ │ ├── DrawArrowEditor.cs.meta │ │ │ │ ├── DrawBase.cs │ │ │ │ ├── DrawBase.cs.meta │ │ │ │ ├── DrawBox.cs │ │ │ │ ├── DrawBox.cs.meta │ │ │ │ ├── DrawCircle.cs │ │ │ │ ├── DrawCircle.cs.meta │ │ │ │ ├── DrawLine.cs │ │ │ │ ├── DrawLine.cs.meta │ │ │ │ ├── DrawLineEditor.cs │ │ │ │ ├── DrawLineEditor.cs.meta │ │ │ │ ├── DrawSphere.cs │ │ │ │ └── DrawSphere.cs.meta │ │ │ ├── DebugUtil.cs │ │ │ ├── DebugUtil.cs.meta │ │ │ ├── GizmosUtil.cs │ │ │ ├── GizmosUtil.cs.meta │ │ │ ├── PrimitiveMeshFactory.cs │ │ │ └── PrimitiveMeshFactory.cs.meta │ │ ├── Math.meta │ │ ├── Math │ │ │ ├── MathUtil.cs │ │ │ ├── MathUtil.cs.meta │ │ │ ├── NumericSpring.cs │ │ │ ├── NumericSpring.cs.meta │ │ │ ├── QuaternionUtil.cs │ │ │ ├── QuaternionUtil.cs.meta │ │ │ ├── VectorUtil.cs │ │ │ └── VectorUtil.cs.meta │ │ ├── Noise.meta │ │ ├── Noise │ │ │ ├── ClassicNoise.cs │ │ │ ├── ClassicNoise.cs.meta │ │ │ ├── ClassicNoisePeriodic.cs │ │ │ ├── ClassicNoisePeriodic.cs.meta │ │ │ ├── NoiseCommon.cs │ │ │ ├── NoiseCommon.cs.meta │ │ │ ├── RandomNoise.cs │ │ │ ├── RandomNoise.cs.meta │ │ │ ├── RandomNoiseVector.cs │ │ │ ├── RandomNoiseVector.cs.meta │ │ │ ├── SimplexNoise.cs │ │ │ ├── SimplexNoise.cs.meta │ │ │ ├── SimplexNoiseGradient.cs │ │ │ └── SimplexNoiseGradient.cs.meta │ │ ├── Physics.meta │ │ ├── Physics │ │ │ ├── Collision.cs │ │ │ └── Collision.cs.meta │ │ ├── Util.meta │ │ └── Util │ │ │ ├── ConditionalFieldAttribute.cs │ │ │ ├── ConditionalFieldAttribute.cs.meta │ │ │ ├── ConditionalFieldAttributeDrawer.cs │ │ │ └── ConditionalFieldAttributeDrawer.cs.meta │ ├── Shader.meta │ └── Shader │ │ ├── Debug.meta │ │ ├── Debug │ │ ├── Primitive.shader │ │ ├── Primitive.shader.meta │ │ ├── PrimitiveCore.cginc │ │ ├── PrimitiveCore.cginc.meta │ │ ├── PrimitiveNoZTest.shader │ │ └── PrimitiveNoZTest.shader.meta │ │ ├── Math.meta │ │ ├── Math │ │ ├── CatmullRom.cginc │ │ ├── CatmullRom.cginc.meta │ │ ├── Color.cginc │ │ ├── Color.cginc.meta │ │ ├── Math.cginc │ │ ├── Math.cginc.meta │ │ ├── Quaternion.cginc │ │ ├── Quaternion.cginc.meta │ │ ├── Vector.cginc │ │ └── Vector.cginc.meta │ │ ├── Noise.meta │ │ ├── Noise │ │ ├── ClassicNoise2D.cginc │ │ ├── ClassicNoise2D.cginc.meta │ │ ├── ClassicNoise3D.cginc │ │ ├── ClassicNoise3D.cginc.meta │ │ ├── Noise.cginc │ │ ├── Noise.cginc.meta │ │ ├── NoiseCommon.cginc │ │ ├── NoiseCommon.cginc.meta │ │ ├── RandomNoise.cginc │ │ ├── RandomNoise.cginc.meta │ │ ├── Resources.meta │ │ ├── Resources │ │ │ ├── ClassicNoiseCs.compute │ │ │ ├── ClassicNoiseCs.compute.meta │ │ │ ├── ClassicNoisePeriodicCs.compute │ │ │ ├── ClassicNoisePeriodicCs.compute.meta │ │ │ ├── RandomNoiseCs.compute │ │ │ ├── RandomNoiseCs.compute.meta │ │ │ ├── RandomNoiseVectorCs.compute │ │ │ ├── RandomNoiseVectorCs.compute.meta │ │ │ ├── SimplexNoiseCs.compute │ │ │ ├── SimplexNoiseCs.compute.meta │ │ │ ├── SimplexNoiseGradientCs.compute │ │ │ └── SimplexNoiseGradientCs.compute.meta │ │ ├── SimplexNoise2D.cginc │ │ ├── SimplexNoise2D.cginc.meta │ │ ├── SimplexNoise3D.cginc │ │ └── SimplexNoise3D.cginc.meta │ │ ├── Physics.meta │ │ ├── Physics │ │ ├── Physics.cginc │ │ └── Physics.cginc.meta │ │ ├── Render.meta │ │ └── Render │ │ ├── Forward.cginc │ │ └── Forward.cginc.meta ├── Examples.meta ├── Examples │ ├── 01a - Inline Ground Constraint.meta │ ├── 01a - Inline Ground Constraint │ │ ├── Inline Ground Constraint.unity │ │ ├── Inline Ground Constraint.unity.meta │ │ ├── InlineGroundConstraintMain.cs │ │ └── InlineGroundConstraintMain.cs.meta │ ├── 01b - Inline Point Constraint.meta │ ├── 01b - Inline Point Constraint │ │ ├── Inline Point Constraint.unity │ │ ├── Inline Point Constraint.unity.meta │ │ ├── InlinePointConstraintMain.cs │ │ └── InlinePointConstraintMain.cs.meta │ ├── 01c - Inline Point Constraint with Rotation.meta │ ├── 01c - Inline Point Constraint with Rotation │ │ ├── Inline Point Constraint with Rotation.unity │ │ ├── Inline Point Constraint with Rotation.unity.meta │ │ ├── InlinePointConstraintWithRotationMain.cs │ │ └── InlinePointConstraintWithRotationMain.cs.meta │ ├── 02a - Systemtic Ground Constraint.meta │ ├── 02a - Systemtic Ground Constraint │ │ ├── Systemic Ground Constraint.unity │ │ ├── Systemic Ground Constraint.unity.meta │ │ ├── SystemicGroundConstraintMain.cs │ │ └── SystemicGroundConstraintMain.cs.meta │ ├── 02b - Systemtic Point Constraint.meta │ ├── 02b - Systemtic Point Constraint │ │ ├── Systemic Point Constraint.unity │ │ └── Systemic Point Constraint.unity.meta │ ├── 02c - Systemic Point Constraint with Rotation.meta │ ├── 02c - Systemic Point Constraint with Rotation │ │ ├── Systemic Point Constraint with Rotation.unity │ │ ├── Systemic Point Constraint with Rotation.unity.meta │ │ ├── SystemicPointConstraintWithRotationMain.cs │ │ └── SystemicPointConstraintWithRotationMain.cs.meta │ ├── 03a - Contact Constraint.meta │ ├── 03a - Contact Constraint │ │ ├── Contact Constraint.unity │ │ └── Contact Constraint.unity.meta │ ├── Common.meta │ └── Common │ │ ├── Blue.mat │ │ ├── Blue.mat.meta │ │ ├── Checker.mat │ │ ├── Checker.mat.meta │ │ ├── Orange.mat │ │ └── Orange.mat.meta ├── Physics Constraints.meta └── Physics Constraints │ ├── Collision.meta │ ├── Collision │ ├── Collision.cs │ ├── Collision.cs.meta │ ├── Contact.cs │ ├── Contact.cs.meta │ ├── NSquared.cs │ ├── NSquared.cs.meta │ ├── PhysicsCollider.cs │ ├── PhysicsCollider.cs.meta │ ├── PlaneCollider.cs │ ├── PlaneCollider.cs.meta │ ├── SphereCollider.cs │ └── SphereCollider.cs.meta │ ├── Constraints.meta │ ├── Constraints │ ├── MouseConstraint.cs │ ├── MouseConstraint.cs.meta │ ├── PlaneConstraint.cs │ ├── PlaneConstraint.cs.meta │ ├── PointConstraint.cs │ ├── PointConstraint.cs.meta │ ├── PointConstraintBase.cs │ └── PointConstraintBase.cs.meta │ ├── Core.meta │ ├── Core │ ├── ConstraintUtil.cs │ ├── ConstraintUtil.cs.meta │ ├── IBroadphase.cs │ ├── IBroadphase.cs.meta │ ├── IPhysicsConstraint.cs │ ├── IPhysicsConstraint.cs.meta │ ├── Inertia.cs │ ├── Inertia.cs.meta │ ├── Matrix3x3.cs │ ├── Matrix3x3.cs.meta │ ├── PhysicsBody.cs │ ├── PhysicsBody.cs.meta │ ├── SoftConstraintParams.cs │ ├── SoftConstraintParams.cs.meta │ ├── World.cs │ └── World.cs.meta │ ├── Util.meta │ └── Util │ ├── Pool.cs │ └── Pool.cs.meta └── Packages └── manifest.json /.gitignore: -------------------------------------------------------------------------------- 1 | slides/.~lock* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Ming-Lun "Allen" Chou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Physics Constraints 2 | by **Ming-Lun "Allen" Chou** / [AllenChou.net](http://AllenChou.net) / [@TheAllenChou](http://twitter.com/TheAllenChou) / [Patreon](https://www.patreon.com/TheAllenChou) 3 | 4 | A minimalistic educational constraint-based physics framework, accompanying a series of talks on physics constraints in games at Unity Frontier meetups in Taiwan on 02/06/2020, 02/14/2020, and 02/21/2020. 5 | 6 | The examples are made with Unity, but the underlying math & phsyics are platform independent and can be applied anywhere. 7 | 8 | If you like my tutorials, please consider supporting me on [Patreon](https://www.patreon.com/join/TheAllenChou). 9 | 10 | ![](/img/point-constraint-with-rotation.gif) ![](/img/contact-with-friction.gif) 11 | 12 | ### Re-Recordings of The Talks 13 | 14 | #### English 15 | * [Part 1 - Constraint Basics](https://youtu.be/MTVdBgQY9LY) 16 | * [Part 2 - Contact Constraints](https://youtu.be/pmdYzNF9x34) 17 | * [Part 3 - Soft Constraints](https://youtu.be/UUt4Lko2wFI) 18 | 19 | #### 中文 20 | * [Part 1 - 約束基礎](https://youtu.be/5zxaToMXidg) 21 | * [Part 2 - 接觸約束](https://youtu.be/a0aqbRt5nAo) 22 | * [Part 3 - 軟性約束](https://youtu.be/6QuK-9fUKio) 23 | -------------------------------------------------------------------------------- /img/contact-with-friction.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/img/contact-with-friction.gif -------------------------------------------------------------------------------- /img/point-constraint-with-rotation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/img/point-constraint-with-rotation.gif -------------------------------------------------------------------------------- /slides/01-constraint-basics-chinese.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/01-constraint-basics-chinese.odp -------------------------------------------------------------------------------- /slides/01-constraint-basics-english.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/01-constraint-basics-english.odp -------------------------------------------------------------------------------- /slides/02-contact-constraints-chinese.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/02-contact-constraints-chinese.odp -------------------------------------------------------------------------------- /slides/02-contact-constraints-english.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/02-contact-constraints-english.odp -------------------------------------------------------------------------------- /slides/03-soft-constraints-chinese.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/03-soft-constraints-chinese.odp -------------------------------------------------------------------------------- /slides/03-soft-constraints-english.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheAllenChou/unity-physics-constraints/92f205aa6fcd5982ebf4dcb820d25956f6ba4cbc/slides/03-soft-constraints-english.odp -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | PhysicS Constraints/[Ll]ibrary/ 2 | PhysicS Constraints/[Dd]ebug/ 3 | PhysicS Constraints/[Tt]emp/ 4 | PhysicS Constraints/[Oo]bj/ 5 | PhysicS Constraints/[Bb]uild/ 6 | PhysicS Constraints/Assets/AssetStoreTools* 7 | Physics Constraints/Logs/ 8 | PhysicS Constraints/ProjectSettings 9 | PhysicS Constraints/.vs/ 10 | bin/ 11 | 12 | # Autogenerated VS/MD/Consulo solution and project files 13 | ExportedObj/ 14 | .consulo/ 15 | *.csproj 16 | *.unityproj 17 | *.sln 18 | *.suo 19 | *.tmp 20 | *.user 21 | *.userprefs 22 | *.pidb 23 | *.booproj 24 | *.svd 25 | *.pdb 26 | 27 | # Unity3D generated meta files 28 | *.pidb.meta 29 | 30 | # Unity3D Generated File On Crash Reports 31 | sysinfo.txt 32 | 33 | # Builds 34 | *.unitypackage 35 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 770321fc11013254cb4457418bb8d4cf 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e3ef1ad5044532742919696ed4e7aa23 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 27f07aa86dbcfde46ae8de5e73de0fc7 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: efd0a739fe209304aa81c05ec99b018f 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArc.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawArc : DrawBase 18 | { 19 | public float Radius = 1.0f; 20 | public int NumSegments = 64; 21 | 22 | public float StartAngle = 0.0f; 23 | public float ArcAngle = 60.0f; 24 | 25 | private void OnValidate() 26 | { 27 | Wireframe = true; 28 | Style = DebugUtil.Style.Wireframe; 29 | 30 | Radius = Mathf.Max(0.0f, Radius); 31 | NumSegments = Mathf.Max(0, NumSegments); 32 | } 33 | 34 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 35 | { 36 | Quaternion startRot = QuaternionUtil.AxisAngle(Vector3.forward, StartAngle * MathUtil.Deg2Rad); 37 | DebugUtil.DrawArc 38 | ( 39 | transform.position, 40 | transform.rotation * startRot * Vector3.right, 41 | transform.rotation * Vector3.forward, 42 | ArcAngle * MathUtil.Deg2Rad, Radius, NumSegments, 43 | color, depthTest 44 | ); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArc.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b83df268a65a4ae45b606ef7cf326a72 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArrow.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawArrow : DrawBase 18 | { 19 | public Vector3 LocalEndVector = Vector3.right; 20 | 21 | public float ConeRadius = 0.05f; 22 | public float ConeHeight = 0.1f; 23 | public float StemThickness = 0.05f; 24 | public int NumSegments = 8; 25 | 26 | private void OnValidate() 27 | { 28 | ConeRadius = Mathf.Max(0.0f, ConeRadius); 29 | ConeHeight = Mathf.Max(0.0f, ConeHeight); 30 | StemThickness = Mathf.Max(0.0f, StemThickness); 31 | NumSegments = Mathf.Max(4, NumSegments); 32 | } 33 | 34 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 35 | { 36 | DebugUtil.DrawArrow 37 | ( 38 | transform.position, 39 | transform.position + transform.TransformVector(LocalEndVector), 40 | ConeRadius, ConeHeight, NumSegments, StemThickness, 41 | color, depthTest, style 42 | ); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArrow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f14b51060c9846b4e893bef665a462cd 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArrowEditor.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #if UNITY_EDITOR 13 | using UnityEditor; 14 | using UnityEngine; 15 | 16 | namespace CjLib 17 | { 18 | [CustomEditor(typeof(DrawArrow)), CanEditMultipleObjects] 19 | public class DrawArrowEditor : Editor 20 | { 21 | private void OnSceneGUI() 22 | { 23 | var drawLine = (DrawArrow) target; 24 | 25 | Vector3 oldLineEnd = drawLine.transform.position + drawLine.transform.TransformVector(drawLine.LocalEndVector); 26 | 27 | Vector3 newLineEnd = 28 | Handles.PositionHandle 29 | ( 30 | oldLineEnd, 31 | Tools.pivotRotation == PivotRotation.Global 32 | ? Quaternion.identity 33 | : drawLine.transform.rotation 34 | ) ; 35 | 36 | Vector3 delta = newLineEnd - oldLineEnd; 37 | 38 | if (delta.sqrMagnitude <= 0.0f) 39 | return; 40 | 41 | drawLine.LocalEndVector += drawLine.transform.InverseTransformVector(delta); 42 | 43 | EditorApplication.QueuePlayerLoopUpdate(); 44 | } 45 | } 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawArrowEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c1bc679e63c970549819aafeb740033a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawBase.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public abstract class DrawBase : MonoBehaviour 17 | { 18 | public Color WireframeColor = Color.white; 19 | public Color ShadededColor = Color.gray; 20 | 21 | public bool Wireframe = false; 22 | public DebugUtil.Style Style = DebugUtil.Style.FlatShaded; 23 | 24 | public bool DepthTest = true; 25 | 26 | private void Update() 27 | { 28 | if (Style != DebugUtil.Style.Wireframe) 29 | Draw(ShadededColor, Style, DepthTest); 30 | 31 | if (Style == DebugUtil.Style.Wireframe || Wireframe) 32 | Draw(WireframeColor, DebugUtil.Style.Wireframe, DepthTest); 33 | } 34 | 35 | protected abstract void Draw(Color color, DebugUtil.Style style, bool depthTest); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawBase.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7a2903a055739d24a906186039dc7550 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawBox.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawBox : DrawBase 18 | { 19 | public float Radius = 1.0f; 20 | public int NumSegments = 64; 21 | 22 | public float StartAngle = 0.0f; 23 | public float ArcAngle = 60.0f; 24 | 25 | private void OnValidate() 26 | { 27 | Radius = Mathf.Max(0.0f, Radius); 28 | NumSegments = Mathf.Max(0, NumSegments); 29 | } 30 | 31 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 32 | { 33 | Quaternion startRot = QuaternionUtil.AxisAngle(Vector3.forward, StartAngle * MathUtil.Deg2Rad); 34 | DebugUtil.DrawArc 35 | ( 36 | transform.position, 37 | transform.rotation * startRot * Vector3.right, 38 | transform.rotation * Vector3.forward, 39 | ArcAngle * MathUtil.Deg2Rad, Radius, NumSegments, 40 | color, depthTest 41 | ); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawBox.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b9c471870a8d6d94d8ced3169854ab80 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawCircle.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawCircle : DrawBase 18 | { 19 | public float Radius = 1.0f; 20 | public int NumSegments = 64; 21 | 22 | private void OnValidate() 23 | { 24 | Radius = Mathf.Max(0.0f, Radius); 25 | NumSegments = Mathf.Max(0, NumSegments); 26 | } 27 | 28 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 29 | { 30 | DebugUtil.DrawCircle(transform.position, transform.rotation * Vector3.back, Radius, NumSegments, color, depthTest, style); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawCircle.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ab47c729609ea7e4ca91a53a1b648aa0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawLine.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawLine : DrawBase 18 | { 19 | public Vector3 LocalEndVector = Vector3.right; 20 | 21 | private void OnValidate() 22 | { 23 | Wireframe = true; 24 | Style = DebugUtil.Style.Wireframe; 25 | } 26 | 27 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 28 | { 29 | DebugUtil.DrawLine 30 | ( 31 | transform.position, 32 | transform.position + transform.TransformVector(LocalEndVector), 33 | color, depthTest 34 | ); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawLine.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 38a130bb4d340a345b48f57fb67035ff 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawLineEditor.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #if UNITY_EDITOR 13 | using UnityEditor; 14 | using UnityEngine; 15 | 16 | namespace CjLib 17 | { 18 | [CustomEditor(typeof(DrawLine)), CanEditMultipleObjects] 19 | public class DrawLineEditor : Editor 20 | { 21 | private void OnSceneGUI() 22 | { 23 | var drawLine = (DrawLine) target; 24 | 25 | Vector3 oldLineEnd = drawLine.transform.position + drawLine.transform.TransformVector(drawLine.LocalEndVector); 26 | 27 | Vector3 newLineEnd = 28 | Handles.PositionHandle 29 | ( 30 | oldLineEnd, 31 | Tools.pivotRotation == PivotRotation.Global 32 | ? Quaternion.identity 33 | : drawLine.transform.rotation 34 | ) ; 35 | 36 | Vector3 delta = newLineEnd - oldLineEnd; 37 | 38 | if (delta.sqrMagnitude <= 0.0f) 39 | return; 40 | 41 | drawLine.LocalEndVector += drawLine.transform.InverseTransformVector(delta); 42 | 43 | EditorApplication.QueuePlayerLoopUpdate(); 44 | } 45 | } 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawLineEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0acc817dd8164f74b83641278885cb4a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawSphere.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | [ExecuteInEditMode] 17 | public class DrawSphere : DrawBase 18 | { 19 | public float Radius = 1.0f; 20 | public int NumSegments = 64; 21 | 22 | public float StartAngle = 0.0f; 23 | public float ArcAngle = 60.0f; 24 | 25 | private void OnValidate() 26 | { 27 | Radius = Mathf.Max(0.0f, Radius); 28 | NumSegments = Mathf.Max(0, NumSegments); 29 | } 30 | 31 | protected override void Draw(Color color, DebugUtil.Style style, bool depthTest) 32 | { 33 | Quaternion startRot = QuaternionUtil.AxisAngle(Vector3.forward, StartAngle * MathUtil.Deg2Rad); 34 | DebugUtil.DrawArc 35 | ( 36 | transform.position, 37 | transform.rotation * startRot * Vector3.right, 38 | transform.rotation * Vector3.forward, 39 | ArcAngle * MathUtil.Deg2Rad, Radius, NumSegments, 40 | color, depthTest 41 | ); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/Components/DrawSphere.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3c92e38eec6d399438276095697c66c2 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/DebugUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bcd12943196f1194c8243f1153874ca9 3 | timeCreated: 1505627213 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/GizmosUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c8d8fc51a789c3f40bcd3e88a9b20031 3 | timeCreated: 1509865761 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Debug/PrimitiveMeshFactory.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63fe9a59410702a4d9db5da90b504001 3 | timeCreated: 1506843182 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f5d22176eeb35cf4f83e1cc7acd4d954 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/MathUtil.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class MathUtil 17 | { 18 | 19 | public static readonly float Pi = Mathf.PI; 20 | public static readonly float TwoPi = 2.0f * Mathf.PI; 21 | public static readonly float HalfPi = Mathf.PI / 2.0f; 22 | public static readonly float ThirdPi = Mathf.PI / 3.0f; 23 | public static readonly float QuarterPi = Mathf.PI / 4.0f; 24 | public static readonly float FifthPi = Mathf.PI / 5.0f; 25 | public static readonly float SixthPi = Mathf.PI / 6.0f; 26 | 27 | public static readonly float Sqrt2 = Mathf.Sqrt(2.0f); 28 | public static readonly float Sqrt2Inv = 1.0f / Mathf.Sqrt(2.0f); 29 | public static readonly float Sqrt3 = Mathf.Sqrt(3.0f); 30 | public static readonly float Sqrt3Inv = 1.0f / Mathf.Sqrt(3.0f); 31 | 32 | public static readonly float Epsilon = 1.0e-9f; 33 | public static readonly float EpsilonComp = 1.0f - Epsilon; 34 | public static readonly float Rad2Deg = 180.0f / Mathf.PI; 35 | public static readonly float Deg2Rad = Mathf.PI / 180.0f; 36 | 37 | public static float AsinSafe(float x) 38 | { 39 | return Mathf.Asin(Mathf.Clamp(x, -1.0f, 1.0f)); 40 | } 41 | 42 | public static float AcosSafe(float x) 43 | { 44 | return Mathf.Acos(Mathf.Clamp(x, -1.0f, 1.0f)); 45 | } 46 | 47 | public static float CatmullRom(float p0, float p1, float p2, float p3, float t) 48 | { 49 | float tt = t * t; 50 | return 51 | 0.5f 52 | * ((2.0f * p1) 53 | + (-p0 + p2) * t 54 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 55 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 56 | ); 57 | } 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/MathUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 293ef8804f152764c9b62fead2675ab0 3 | timeCreated: 1505630917 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/NumericSpring.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fd0d307d689a8ac4580a4a4c56bf0742 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/QuaternionUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3e3573306d728414c851bf9fc2fbc23f 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/VectorUtil.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class VectorUtil 17 | { 18 | 19 | public static Vector3 Rotate2D(Vector3 v, float deg) 20 | { 21 | Vector3 results = v; 22 | float cos = Mathf.Cos(MathUtil.Deg2Rad * deg); 23 | float sin = Mathf.Sin(MathUtil.Deg2Rad * deg); 24 | results.x = cos * v.x - sin * v.y; 25 | results.y = sin * v.x + cos * v.y; 26 | return results; 27 | } 28 | 29 | public static Vector3 NormalizeSafe(Vector3 v, Vector3 fallback) 30 | { 31 | return 32 | v.sqrMagnitude > MathUtil.Epsilon 33 | ? v.normalized 34 | : fallback; 35 | } 36 | 37 | // Returns a vector orthogonal to given vector. 38 | // If the given vector is a unit vector, the returned vector will also be a unit vector. 39 | public static Vector3 FindOrthogonal(Vector3 v) 40 | { 41 | if (Mathf.Abs(v.x) >= MathUtil.Sqrt3Inv) 42 | return Vector3.Normalize(new Vector3(v.y, -v.x, 0.0f)); 43 | else 44 | return Vector3.Normalize(new Vector3(0.0f, v.z, -v.y)); 45 | } 46 | 47 | // Yields two extra vectors that form an orthogonal basis with the given vector. 48 | // If the given vector is a unit vector, the returned vectors will also be unit vectors. 49 | public static void FormOrthogonalBasis(Vector3 v, out Vector3 a, out Vector3 b) 50 | { 51 | a = FindOrthogonal(v); 52 | b = Vector3.Cross(a, v); 53 | } 54 | 55 | public static Vector3 Integrate(Vector3 x, Vector3 v, float dt) 56 | { 57 | return x + v * dt; 58 | } 59 | 60 | // Both vectors must be unit vectors. 61 | public static Vector3 Slerp(Vector3 a, Vector3 b, float t) 62 | { 63 | float dot = Vector3.Dot(a, b); 64 | 65 | if (dot > 0.99999f) 66 | { 67 | // singularity: two vectors point in the same direction 68 | return Vector3.Lerp(a, b, t); 69 | } 70 | else if (dot < -0.99999f) 71 | { 72 | // singularity: two vectors point in the opposite direction 73 | Vector3 axis = FindOrthogonal(a); 74 | return Quaternion.AngleAxis(180.0f * t, axis) * a; 75 | } 76 | 77 | float rad = MathUtil.AcosSafe(dot); 78 | return (Mathf.Sin((1.0f - t) * rad) * a + Mathf.Sin(t * rad) * b) / Mathf.Sin(rad); 79 | } 80 | 81 | public static Vector3 CatmullRom(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) 82 | { 83 | float tt = t * t; 84 | return 85 | 0.5f 86 | * ((2.0f * p1) 87 | + (-p0 + p2) * t 88 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 89 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 90 | ); 91 | } 92 | 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Math/VectorUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a0c20a61328a4bb4ba0334465ca09061 3 | timeCreated: 1505973953 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d2fdee2e45961c242a6ecced78a385e4 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/ClassicNoise.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class ClassicNoise 17 | { 18 | // common 19 | //------------------------------------------------------------------------- 20 | 21 | private static bool s_classicInit = false; 22 | private static ComputeShader s_classic; 23 | private static int s_classicGrid2KernelId; 24 | private static int s_classicGrid3KernelId; 25 | private static int s_classicCustom2KernelId; 26 | private static int s_classicCustom3KernelId; 27 | private static void InitClassic() 28 | { 29 | if (s_classicInit) 30 | return; 31 | 32 | s_classic = (ComputeShader) Resources.Load("ClassicNoiseCs"); 33 | s_classicGrid2KernelId = s_classic.FindKernel("ClassicGrid2"); 34 | s_classicGrid3KernelId = s_classic.FindKernel("ClassicGrid3"); 35 | s_classicCustom2KernelId = s_classic.FindKernel("ClassicCustom2"); 36 | s_classicCustom3KernelId = s_classic.FindKernel("ClassicCustom3"); 37 | } 38 | 39 | private static void GetClassicGrid2(out ComputeShader shader, out int kernelId) 40 | { 41 | InitClassic(); 42 | shader = s_classic; 43 | kernelId = s_classicGrid2KernelId; 44 | } 45 | 46 | private static void GetClassicGrid3(out ComputeShader shader, out int kernelId) 47 | { 48 | InitClassic(); 49 | shader = s_classic; 50 | kernelId = s_classicGrid3KernelId; 51 | } 52 | 53 | private static void GetClassicCustom2(out ComputeShader shader, out int kernelId) 54 | { 55 | InitClassic(); 56 | shader = s_classic; 57 | kernelId = s_classicCustom2KernelId; 58 | } 59 | 60 | private static void GetClassicCustom3(out ComputeShader shader, out int kernelId) 61 | { 62 | InitClassic(); 63 | shader = s_classic; 64 | kernelId = s_classicCustom3KernelId; 65 | } 66 | 67 | //------------------------------------------------------------------------- 68 | // end: common 69 | 70 | 71 | // GPU compute / grid samples 72 | //------------------------------------------------------------------------- 73 | 74 | public static void Compute(FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 75 | { 76 | ComputeShader shader; 77 | int kernelId; 78 | GetClassicGrid2(out shader, out kernelId); 79 | NoiseCommon.Compute(output, shader, kernelId, scale.GetArray(), offset, numOctaves, octaveOffsetFactor); 80 | } 81 | 82 | //------------------------------------------------------------------------- 83 | // end: GPU compute / grid samples 84 | 85 | 86 | // GPU compute / custom samples 87 | //------------------------------------------------------------------------- 88 | 89 | public static void Compute(Vector2Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 90 | { 91 | ComputeShader shader; 92 | int kernelId; 93 | GetClassicCustom2(out shader, out kernelId); 94 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 95 | } 96 | 97 | public static void Compute(Vector3Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 98 | { 99 | ComputeShader shader; 100 | int kernelId; 101 | GetClassicCustom3(out shader, out kernelId); 102 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 103 | } 104 | 105 | //------------------------------------------------------------------------- 106 | // end: GPU compute / custom samples 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/ClassicNoise.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b3bb9ab06bd7c784e8a7e8b5b8001170 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/ClassicNoisePeriodic.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class ClassicNoisePeriodic 17 | { 18 | // common 19 | //------------------------------------------------------------------------- 20 | 21 | private static bool s_classicPeriodicInit = false; 22 | private static ComputeShader s_classicPeriodic; 23 | private static int s_classicPeriodicGrid2KernelId; 24 | private static int s_classicPeriodicGrid3KernelId; 25 | private static int s_classicPeriodicCustom2KernelId; 26 | private static int s_classicPeriodicCustom3KernelId; 27 | private static void InitClassicPeriodic() 28 | { 29 | if (s_classicPeriodicInit) 30 | return; 31 | 32 | s_classicPeriodic = (ComputeShader) Resources.Load("ClassicNoisePeriodicCs"); 33 | s_classicPeriodicGrid2KernelId = s_classicPeriodic.FindKernel("ClassicPeriodicGrid2"); 34 | s_classicPeriodicGrid3KernelId = s_classicPeriodic.FindKernel("ClassicPeriodicGrid3"); 35 | s_classicPeriodicCustom2KernelId = s_classicPeriodic.FindKernel("ClassicPeriodicGrid2"); 36 | s_classicPeriodicCustom3KernelId = s_classicPeriodic.FindKernel("ClassicPeriodicGrid3"); 37 | } 38 | 39 | private static void GetClassicPeriodicGrid2(out ComputeShader shader, out int kernelId) 40 | { 41 | InitClassicPeriodic(); 42 | shader = s_classicPeriodic; 43 | kernelId = s_classicPeriodicGrid2KernelId; 44 | } 45 | 46 | private static void GetClassicPeriodicGrid3(out ComputeShader shader, out int kernelId) 47 | { 48 | InitClassicPeriodic(); 49 | shader = s_classicPeriodic; 50 | kernelId = s_classicPeriodicGrid3KernelId; 51 | } 52 | 53 | private static void GetClassicPeriodicCustom2(out ComputeShader shader, out int kernelId) 54 | { 55 | InitClassicPeriodic(); 56 | shader = s_classicPeriodic; 57 | kernelId = s_classicPeriodicCustom2KernelId; 58 | } 59 | 60 | private static void GetClassicPeriodicCustom3(out ComputeShader shader, out int kernelId) 61 | { 62 | InitClassicPeriodic(); 63 | shader = s_classicPeriodic; 64 | kernelId = s_classicPeriodicCustom3KernelId; 65 | } 66 | 67 | //------------------------------------------------------------------------- 68 | // end: common 69 | 70 | 71 | // GPU compute / grid samples 72 | //------------------------------------------------------------------------- 73 | 74 | public static void Compute(FloatArray output, NoiseScale scale, NoiseOffset offset, NoisePeriod period, int numOctaves, float octaveOffsetFactor) 75 | { 76 | ComputeShader shader; 77 | int kernelId; 78 | GetClassicPeriodicGrid2(out shader, out kernelId); 79 | NoiseCommon.Compute(output, shader, kernelId, scale, offset, period, numOctaves, octaveOffsetFactor); 80 | } 81 | 82 | //------------------------------------------------------------------------- 83 | // end: GPU compute / grid samples 84 | 85 | 86 | // GPU compute / custom samples 87 | //------------------------------------------------------------------------- 88 | 89 | public static void Compute(Vector2Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, NoisePeriod period, int numOctaves, float octaveOffsetFactor) 90 | { 91 | ComputeShader shader; 92 | int kernelId; 93 | GetClassicPeriodicCustom2(out shader, out kernelId); 94 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, period, numOctaves, octaveOffsetFactor); 95 | } 96 | 97 | public static void Compute(Vector3Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, NoisePeriod period, int numOctaves, float octaveOffsetFactor) 98 | { 99 | ComputeShader shader; 100 | int kernelId; 101 | GetClassicPeriodicCustom3(out shader, out kernelId); 102 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, period, numOctaves, octaveOffsetFactor); 103 | } 104 | 105 | //------------------------------------------------------------------------- 106 | // end: GPU compute / custom samples 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/ClassicNoisePeriodic.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0cff445fac36ff743af64c8e420e935a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/NoiseCommon.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4e25413c92d3b2d4499e6af30623b523 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/RandomNoise.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class RandomNoise 17 | { 18 | // common 19 | //------------------------------------------------------------------------- 20 | 21 | private static bool s_randomInit = false; 22 | private static ComputeShader s_random; 23 | private static int s_random1KernelId; 24 | private static int s_random2KernelId; 25 | private static int s_random3KernelId; 26 | private static void InitRandom() 27 | { 28 | if (s_randomInit) 29 | return; 30 | 31 | s_random = (ComputeShader) Resources.Load("RandomNoiseCs"); 32 | s_random1KernelId = s_random.FindKernel("RandomGrid1"); 33 | s_random2KernelId = s_random.FindKernel("RandomGrid2"); 34 | s_random3KernelId = s_random.FindKernel("RandomGrid3"); 35 | } 36 | 37 | private static void GetRandom1(out ComputeShader shader, out int kernelId) 38 | { 39 | InitRandom(); 40 | shader = s_random; 41 | kernelId = s_random1KernelId; 42 | } 43 | 44 | private static void GetRandom2(out ComputeShader shader, out int kernelId) 45 | { 46 | InitRandom(); 47 | shader = s_random; 48 | kernelId = s_random2KernelId; 49 | } 50 | 51 | private static void GetRandom3(out ComputeShader shader, out int kernelId) 52 | { 53 | InitRandom(); 54 | shader = s_random; 55 | kernelId = s_random3KernelId; 56 | } 57 | 58 | //------------------------------------------------------------------------- 59 | // end: common 60 | 61 | 62 | // GPU compute / grid samples 63 | //------------------------------------------------------------------------- 64 | 65 | public static void Compute(FloatArray output, int seed = 0) 66 | { 67 | ComputeShader shader; 68 | int kernelId; 69 | GetRandom1(out shader, out kernelId); 70 | NoiseCommon.Compute(output, shader, kernelId, seed); 71 | } 72 | 73 | //------------------------------------------------------------------------- 74 | // end: GPU compute / grid samples 75 | 76 | 77 | // CPU 78 | //------------------------------------------------------------------------- 79 | 80 | public static float Get(float v, int seed = 0) 81 | { 82 | // return frac(sin(mod(s, 6.2831853)) * 43758.5453123); 83 | float s = NoiseCommon.JumbleSeed(seed); 84 | float r = Mathf.Sin(Mathf.Repeat(s + v, 6.2831853f)) * 43758.5453123f; 85 | r = (r >= 0.0f) ? r : -r; 86 | r -= Mathf.Floor(r); 87 | return r; 88 | } 89 | 90 | public static float Get(Vector2 v, int seed = 0) 91 | { 92 | // float d = dot(s + 0.1234567, float2(1111112.9819837, 78.237173)); 93 | float s = NoiseCommon.JumbleSeed(seed); 94 | float d = 95 | (s + v.x + 0.1234567f) * 1111112.9819837f 96 | + (s + v.y + 0.1234567f) * 78.237173f; 97 | 98 | // return frac(sin(m) * 43758.5453123); 99 | float r = Mathf.Sin(d) * 43758.5453123f; 100 | r = (r >= 0.0f) ? r : -r; 101 | r -= Mathf.Floor(r); 102 | return r; 103 | } 104 | 105 | public static float Get(Vector3 v, int seed = 0) 106 | { 107 | // float d = dot(s + 0.1234567, float3(11112.9819837, 378.237173, 3971977.9173179)); 108 | float s = NoiseCommon.JumbleSeed(seed); 109 | float d = 110 | (s + v.x + 0.1234567f) * 1111112.9819837f 111 | + (s + v.y + 0.1234567f) * 378.237173f 112 | + (s + v.z + 0.1234567f) * 3971977.9173179f; 113 | 114 | // return frac(sin(m) * 43758.5453123); 115 | float r = Mathf.Sin(d) * 43758.5453123f; 116 | r = (r >= 0.0f) ? r : -r; 117 | r -= Mathf.Floor(r); 118 | return r; 119 | } 120 | 121 | //------------------------------------------------------------------------- 122 | // end: CPU 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/RandomNoise.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 28b2d82f214efb541b5eaf2403235d8e 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/RandomNoiseVector.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | // GPU compute 17 | //------------------------------------------------------------------------- 18 | 19 | public class RandomNoiseVector 20 | { 21 | private static bool s_randomVecInit = false; 22 | private static ComputeShader s_randomVec; 23 | private static int s_randomVec2KernelId; 24 | private static int s_randomVec3KernelId; 25 | private static void InitRandomVec() 26 | { 27 | if (s_randomVecInit) 28 | return; 29 | 30 | s_randomVec = (ComputeShader) Resources.Load("RandomNoiseVectorCs"); 31 | s_randomVec2KernelId = s_randomVec.FindKernel("RandomVec2"); 32 | s_randomVec3KernelId = s_randomVec.FindKernel("RandomVec3"); 33 | } 34 | 35 | private static void GetRandomVec2(out ComputeShader shader, out int kernelId) 36 | { 37 | InitRandomVec(); 38 | shader = s_randomVec; 39 | kernelId = s_randomVec2KernelId; 40 | } 41 | 42 | private static void GetRandomVec3(out ComputeShader shader, out int kernelId) 43 | { 44 | InitRandomVec(); 45 | shader = s_randomVec; 46 | kernelId = s_randomVec3KernelId; 47 | } 48 | 49 | public static void Compute(Vector2Array output, int seed = 0) 50 | { 51 | ComputeShader shader; 52 | int kernelId; 53 | GetRandomVec2(out shader, out kernelId); 54 | NoiseCommon.Compute(output, shader, kernelId, seed); 55 | } 56 | 57 | public static void Compute(Vector3Array output, int seed = 0) 58 | { 59 | ComputeShader shader; 60 | int kernelId; 61 | GetRandomVec3(out shader, out kernelId); 62 | NoiseCommon.Compute(output, shader, kernelId, seed); 63 | } 64 | 65 | //------------------------------------------------------------------------- 66 | // end: GPU compute 67 | 68 | 69 | // CPU 70 | //------------------------------------------------------------------------- 71 | 72 | public static Vector2 GetVector2(float v, int seed = 0) 73 | { 74 | // TODO 75 | return new Vector2(); 76 | } 77 | 78 | public static Vector2 GetVector2(Vector2 v, int seed = 0) 79 | { 80 | // TODO 81 | return new Vector2(); 82 | } 83 | 84 | public static Vector3 GetVector3(float v, int seed = 0) 85 | { 86 | // TODO 87 | return new Vector3(); 88 | } 89 | 90 | public static Vector3 GetVector3(Vector2 v, int seed = 0) 91 | { 92 | // TODO 93 | return new Vector3(); 94 | } 95 | 96 | public static Vector3 GetVector3(Vector3 v, int seed = 0) 97 | { 98 | // TODO 99 | return new Vector3(); 100 | } 101 | 102 | //------------------------------------------------------------------------- 103 | // end: CPU 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/RandomNoiseVector.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cffd641eedd68b344ae1955908e45ea2 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/SimplexNoise.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class SimplexNoise 17 | { 18 | // common 19 | //------------------------------------------------------------------------- 20 | 21 | private static bool s_simplexInit = false; 22 | private static ComputeShader s_simplex; 23 | private static int s_simplexGrid2KernelId; 24 | private static int s_simplexGrid3KernelId; 25 | private static int s_simplexCustom2KernelId; 26 | private static int s_simplexCustom3KernelId; 27 | private static void InitSimplex() 28 | { 29 | if (s_simplexInit) 30 | return; 31 | 32 | s_simplex = (ComputeShader) Resources.Load("SimplexNoiseCs"); 33 | s_simplexGrid2KernelId = s_simplex.FindKernel("SimplexGrid2"); 34 | s_simplexGrid3KernelId = s_simplex.FindKernel("SimplexGrid3"); 35 | s_simplexCustom2KernelId = s_simplex.FindKernel("SimplexCustom2"); 36 | s_simplexCustom3KernelId = s_simplex.FindKernel("SimplexCustom3"); 37 | } 38 | 39 | private static void GetSimplexGrid2(out ComputeShader shader, out int kernelId) 40 | { 41 | InitSimplex(); 42 | shader = s_simplex; 43 | kernelId = s_simplexGrid2KernelId; 44 | } 45 | 46 | private static void GetSimplexGrid3(out ComputeShader shader, out int kernelId) 47 | { 48 | InitSimplex(); 49 | shader = s_simplex; 50 | kernelId = s_simplexGrid3KernelId; 51 | } 52 | 53 | private static void GetSimplexCustom2(out ComputeShader shader, out int kernelId) 54 | { 55 | InitSimplex(); 56 | shader = s_simplex; 57 | kernelId = s_simplexCustom2KernelId; 58 | } 59 | 60 | private static void GetSimplexCustom3(out ComputeShader shader, out int kernelId) 61 | { 62 | InitSimplex(); 63 | shader = s_simplex; 64 | kernelId = s_simplexCustom3KernelId; 65 | } 66 | 67 | //------------------------------------------------------------------------- 68 | // end: common 69 | 70 | 71 | // GPU compute / grid samples 72 | //------------------------------------------------------------------------- 73 | 74 | public static void Compute(FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 75 | { 76 | ComputeShader shader; 77 | int kernelId; 78 | GetSimplexGrid2(out shader, out kernelId); 79 | NoiseCommon.Compute(output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 80 | } 81 | 82 | //------------------------------------------------------------------------- 83 | // end: GPU compute / grid samples 84 | 85 | 86 | // GPU compute / custom samples 87 | //------------------------------------------------------------------------- 88 | 89 | public static void Compute(Vector2Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 90 | { 91 | ComputeShader shader; 92 | int kernelId; 93 | GetSimplexCustom2(out shader, out kernelId); 94 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 95 | } 96 | 97 | public static void Compute(Vector3Array input, FloatArray output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 98 | { 99 | ComputeShader shader; 100 | int kernelId; 101 | GetSimplexCustom3(out shader, out kernelId); 102 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 103 | } 104 | 105 | //------------------------------------------------------------------------- 106 | // end: GPU compute / custom samples 107 | 108 | 109 | // CPU 110 | //------------------------------------------------------------------------- 111 | 112 | 113 | 114 | //------------------------------------------------------------------------- 115 | // end: CPU 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/SimplexNoise.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 41dbc06144f95e84a90d192821544cf7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/SimplexNoiseGradient.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | public class SimplexNoiseGradient 17 | { 18 | // common 19 | //------------------------------------------------------------------------- 20 | 21 | private static bool s_simplexGradientInit = false; 22 | private static ComputeShader s_simplexGradient; 23 | private static int s_simplexGradientGrid2KernelId; 24 | private static int s_simplexGradientGrid3KernelId; 25 | private static int s_simplexGradientCustom2KernelId; 26 | private static int s_simplexGradientCustum3KernelId; 27 | private static void InitSimplex() 28 | { 29 | if (s_simplexGradientInit) 30 | return; 31 | 32 | s_simplexGradient = (ComputeShader) Resources.Load("SimplexNoiseGradientCs"); 33 | s_simplexGradientGrid2KernelId = s_simplexGradient.FindKernel("SimplexGradientGrid2"); 34 | s_simplexGradientGrid3KernelId = s_simplexGradient.FindKernel("SimplexGradientGrid3"); 35 | s_simplexGradientCustom2KernelId = s_simplexGradient.FindKernel("SimplexGradientCustom2"); 36 | s_simplexGradientCustum3KernelId = s_simplexGradient.FindKernel("SimplexGradientCustom3"); 37 | } 38 | 39 | private static void GetSimplexGradientGrid2(out ComputeShader shader, out int kernelId) 40 | { 41 | InitSimplex(); 42 | shader = s_simplexGradient; 43 | kernelId = s_simplexGradientGrid2KernelId; 44 | } 45 | 46 | private static void GetSimplexGradientGrid3(out ComputeShader shader, out int kernelId) 47 | { 48 | InitSimplex(); 49 | shader = s_simplexGradient; 50 | kernelId = s_simplexGradientGrid3KernelId; 51 | } 52 | 53 | private static void GetSimplexGradientCustom2(out ComputeShader shader, out int kernelId) 54 | { 55 | InitSimplex(); 56 | shader = s_simplexGradient; 57 | kernelId = s_simplexGradientCustom2KernelId; 58 | } 59 | 60 | private static void GetSimplexGradientCustom3(out ComputeShader shader, out int kernelId) 61 | { 62 | InitSimplex(); 63 | shader = s_simplexGradient; 64 | kernelId = s_simplexGradientCustum3KernelId; 65 | } 66 | 67 | //------------------------------------------------------------------------- 68 | // end: common 69 | 70 | 71 | // GPU compute / grid samples 72 | //------------------------------------------------------------------------- 73 | 74 | public static void Compute(Vector2Array output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 75 | { 76 | ComputeShader shader; 77 | int kernelId; 78 | GetSimplexGradientGrid2(out shader, out kernelId); 79 | NoiseCommon.Compute(output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 80 | } 81 | 82 | public static void Compute(Vector3Array output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 83 | { 84 | ComputeShader shader; 85 | int kernelId; 86 | GetSimplexGradientGrid3(out shader, out kernelId); 87 | NoiseCommon.Compute(output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 88 | } 89 | 90 | //------------------------------------------------------------------------- 91 | // end: GPU compute / grid samples 92 | 93 | 94 | // GPU compute / custom samples 95 | //------------------------------------------------------------------------- 96 | 97 | public static void Compute(Vector2Array input, Vector2Array output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 98 | { 99 | ComputeShader shader; 100 | int kernelId; 101 | GetSimplexGradientCustom2(out shader, out kernelId); 102 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 103 | } 104 | 105 | public static void Compute(Vector3Array input, Vector3Array output, NoiseScale scale, NoiseOffset offset, int numOctaves, float octaveOffsetFactor) 106 | { 107 | ComputeShader shader; 108 | int kernelId; 109 | GetSimplexGradientCustom3(out shader, out kernelId); 110 | NoiseCommon.Compute(input, output, shader, kernelId, scale, offset, numOctaves, octaveOffsetFactor); 111 | } 112 | 113 | //------------------------------------------------------------------------- 114 | // end: GPU compute / custom samples 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Noise/SimplexNoiseGradient.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 83a5e45027611394794e3fdd86c74020 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Physics.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1ec10a4b4edb34b47bc708be75e4183d 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Physics/Collision.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace CjLib 15 | { 16 | // push vector separates A from B 17 | public class Collision 18 | { 19 | public static bool SphereSphere(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB) 20 | { 21 | Vector3 vec = centerA - centerB; 22 | float dd = vec.sqrMagnitude; 23 | float r = radiusA + radiusB; 24 | 25 | return dd <= r * r; 26 | } 27 | 28 | public static bool SphereSphere(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB, out Vector3 push) 29 | { 30 | push = Vector3.zero; 31 | 32 | Vector3 vec = centerA - centerB; 33 | float dd = vec.sqrMagnitude; 34 | float r = radiusA + radiusB; 35 | 36 | if (dd > r * r) 37 | { 38 | return false; 39 | } 40 | 41 | float d = Mathf.Sqrt(dd); 42 | 43 | push = VectorUtil.NormalizeSafe(vec, Vector3.zero) * (r - d); 44 | return true; 45 | } 46 | 47 | public static bool SphereCapsule(Vector3 centerA, float radiusA, Vector3 headB, Vector3 tailB, float radiusB) 48 | { 49 | Vector3 segVec = tailB - headB; 50 | float segLenSqr = segVec.sqrMagnitude; 51 | if (segLenSqr < MathUtil.Epsilon) 52 | return SphereSphere(centerA, radiusA, 0.5f * (headB + tailB), radiusB); 53 | 54 | float segLenInv = 1.0f / Mathf.Sqrt(segLenSqr); 55 | Vector3 segDir = segVec * segLenInv; 56 | Vector3 headToA = centerA - headB; 57 | float t = Mathf.Clamp01(Vector3.Dot(headToA, segDir) * segLenInv); 58 | Vector3 closestB = Vector3.Lerp(headB, tailB, t); 59 | 60 | return SphereSphere(centerA, radiusA, closestB, radiusB); 61 | } 62 | 63 | public static bool SphereCapsule(Vector3 centerA, float radiusA, Vector3 headB, Vector3 tailB, float radiusB, out Vector3 push) 64 | { 65 | push = Vector3.zero; 66 | 67 | Vector3 segVec = tailB - headB; 68 | float segLenSqr = segVec.sqrMagnitude; 69 | if (segLenSqr < MathUtil.Epsilon) 70 | return SphereSphere(centerA, radiusA, 0.5f * (headB + tailB), radiusB, out push); 71 | 72 | float segLenInv = 1.0f / Mathf.Sqrt(segLenSqr); 73 | Vector3 segDir = segVec * segLenInv; 74 | Vector3 headToA = centerA - headB; 75 | float t = Mathf.Clamp01(Vector3.Dot(headToA, segDir) * segLenInv); 76 | Vector3 closestB = Vector3.Lerp(headB, tailB, t); 77 | 78 | return SphereSphere(centerA, radiusA, closestB, radiusB, out push); 79 | } 80 | 81 | public static bool SphereBox(Vector3 centerOffsetA, float radiusA, Vector3 halfExtentB) 82 | { 83 | Vector3 closestOnB = 84 | new Vector3 85 | ( 86 | Mathf.Clamp(centerOffsetA.x, -halfExtentB.x, halfExtentB.x), 87 | Mathf.Clamp(centerOffsetA.y, -halfExtentB.y, halfExtentB.y), 88 | Mathf.Clamp(centerOffsetA.z, -halfExtentB.z, halfExtentB.z) 89 | ); 90 | 91 | Vector3 vec = centerOffsetA - closestOnB; 92 | float dd = vec.sqrMagnitude; 93 | 94 | return dd <= radiusA * radiusA; 95 | } 96 | 97 | public static bool SphereBox(Vector3 centerOffsetA, float radiusA, Vector3 halfExtentB, out Vector3 push) 98 | { 99 | push = Vector3.zero; 100 | 101 | Vector3 closestOnB = 102 | new Vector3 103 | ( 104 | Mathf.Clamp(centerOffsetA.x, -halfExtentB.x, halfExtentB.x), 105 | Mathf.Clamp(centerOffsetA.y, -halfExtentB.y, halfExtentB.y), 106 | Mathf.Clamp(centerOffsetA.z, -halfExtentB.z, halfExtentB.z) 107 | ); 108 | 109 | Vector3 vec = centerOffsetA - closestOnB; 110 | float dd = vec.sqrMagnitude; 111 | 112 | if (dd > radiusA * radiusA) 113 | { 114 | return false; 115 | } 116 | 117 | int numInBoxAxes = 118 | ((centerOffsetA.x < -halfExtentB.x || centerOffsetA.x > halfExtentB.x) ? 0 : 1) 119 | + ((centerOffsetA.y < -halfExtentB.y || centerOffsetA.y > halfExtentB.y) ? 0 : 1) 120 | + ((centerOffsetA.z < -halfExtentB.z || centerOffsetA.z > halfExtentB.z) ? 0 : 1); 121 | 122 | switch (numInBoxAxes) 123 | { 124 | case 0: // hit corner 125 | case 1: // hit edge 126 | case 2: // hit face 127 | { 128 | push = VectorUtil.NormalizeSafe(vec, Vector3.right) * (radiusA - Mathf.Sqrt(dd)); 129 | } 130 | break; 131 | case 3: // inside 132 | { 133 | Vector3 penetration = 134 | new Vector3 135 | ( 136 | halfExtentB.x - Mathf.Abs(centerOffsetA.x) + radiusA, 137 | halfExtentB.y - Mathf.Abs(centerOffsetA.y) + radiusA, 138 | halfExtentB.z - Mathf.Abs(centerOffsetA.z) + radiusA 139 | ); 140 | 141 | if (penetration.x < penetration.y) 142 | { 143 | if (penetration.x < penetration.z) 144 | push = new Vector3(Mathf.Sign(centerOffsetA.x) * penetration.x, 0.0f, 0.0f); 145 | else 146 | push = new Vector3(0.0f, 0.0f, Mathf.Sign(centerOffsetA.z) * penetration.z); 147 | } 148 | else 149 | { 150 | if (penetration.y < penetration.z) 151 | push = new Vector3(0.0f, Mathf.Sign(centerOffsetA.y) * penetration.y, 0.0f); 152 | else 153 | push = new Vector3(0.0f, 0.0f, Mathf.Sign(centerOffsetA.z) * penetration.z); 154 | } 155 | } 156 | break; 157 | } 158 | 159 | return true; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Physics/Collision.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 17c0889bf211a0149aa94f299bd9d883 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Util.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f0f8bfad67d52ff42ae0a8a11af4a391 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Util/ConditionalFieldAttribute.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | http://AllenChou.net 8 | 9 | Modified from project "MyBox" by Andrew Rumak. 10 | License : Copyright (C) 2018 Andrew Rumak. 11 | Distributed under the MIT License. See LICENSE file. 12 | https://github.com/Deadcows/MyBox 13 | */ 14 | /******************************************************************************/ 15 | 16 | using System; 17 | using UnityEngine; 18 | 19 | namespace CjLib 20 | { 21 | [AttributeUsage(AttributeTargets.Field)] 22 | public class ConditionalFieldAttribute : PropertyAttribute 23 | { 24 | public bool ShowRange { get { return Min != Max; } } 25 | 26 | public string PropertyToCheck; 27 | public object CompareValue; 28 | public object CompareValue2; 29 | public object CompareValue3; 30 | public object CompareValue4; 31 | public object CompareValue5; 32 | public object CompareValue6; 33 | public string Label; 34 | public string Tooltip; 35 | public float Min; 36 | public float Max; 37 | 38 | public ConditionalFieldAttribute 39 | ( 40 | string propertyToCheck = null, 41 | object compareValue = null, 42 | object compareValue2 = null, 43 | object compareValue3 = null, 44 | object compareValue4 = null, 45 | object compareValue5 = null, 46 | object compareValue6 = null 47 | ) 48 | { 49 | PropertyToCheck = propertyToCheck; 50 | CompareValue = compareValue; 51 | CompareValue2 = compareValue2; 52 | CompareValue3 = compareValue3; 53 | CompareValue4 = compareValue4; 54 | CompareValue5 = compareValue5; 55 | CompareValue6 = compareValue6; 56 | Label = ""; 57 | Tooltip = ""; 58 | Min = 0.0f; 59 | Max = 0.0f; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Util/ConditionalFieldAttribute.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9bb73dc1b0f1a044cb1deb9a88cfc67d 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Script/Util/ConditionalFieldAttributeDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b9e53b729e687884fb1ec3241468ad3e 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: af1136683704bda4aad9ba53863f5cd8 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 700846c65cb10354eba710117d2cd8eb 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/Primitive.shader: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | Shader "CjLib/Primitive" 13 | { 14 | SubShader 15 | { 16 | Tags { "Queue"="Transparent" "RenderType"="Transparent" "DisableBatching"="true" } 17 | LOD 100 18 | 19 | Pass 20 | { 21 | Blend SrcAlpha OneMinusSrcAlpha 22 | 23 | CGPROGRAM 24 | #pragma shader_feature NORMAL_ON 25 | #pragma shader_feature CAP_SHIFT_SCALE 26 | #pragma vertex vert 27 | #pragma fragment frag 28 | #include "PrimitiveCore.cginc" 29 | ENDCG 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/Primitive.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 034880ed17763504c8c134022d45077a 3 | timeCreated: 1506841290 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/PrimitiveCore.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | Author - Ming-Lun "Allen" Chou 11 | Web - http://AllenChou.net 12 | Twitter - @TheAllenChou 13 | */ 14 | /******************************************************************************/ 15 | 16 | #ifndef CJ_LIB_PRIMITIVE_CORE 17 | #define CJ_LIB_PRIMITIVE_CORE 18 | 19 | #include "UnityCG.cginc" 20 | 21 | struct appdata 22 | { 23 | float4 vertex : POSITION; 24 | 25 | #ifdef NORMAL_ON 26 | float3 normal : NORMAL; 27 | #endif 28 | }; 29 | 30 | struct v2f 31 | { 32 | float4 vertex : SV_POSITION; 33 | 34 | #ifdef NORMAL_ON 35 | float3 normal : NORMAL; 36 | #endif 37 | }; 38 | 39 | float4 _Color; 40 | 41 | // (x, y, z) == (dimensionX, dimensionY, dimensionZ) 42 | // w == capShiftScale (shift 0.5 towards X-Z plane, scale by dimensions, and then shoft back 0.5 * capShiftScale) 43 | float4 _Dimensions; 44 | 45 | float _ZBias; 46 | 47 | sampler2D _MainTex; 48 | float4 _MainTex_ST; 49 | 50 | v2f vert (appdata v) 51 | { 52 | v2f o; 53 | 54 | #ifdef CAP_SHIFT_SCALE 55 | const float ySign = sign(v.vertex.y); 56 | v.vertex.y -= ySign * 0.5f; 57 | #endif 58 | 59 | v.vertex.xyz *= _Dimensions.xyz; 60 | 61 | #ifdef CAP_SHIFT_SCALE 62 | v.vertex.y += ySign * 0.5f * _Dimensions.w; 63 | #endif 64 | 65 | o.vertex = UnityObjectToClipPos(v.vertex); 66 | o.vertex.z += _ZBias; 67 | 68 | #ifdef NORMAL_ON 69 | float4x4 scaleInverseTranspose = float4x4 70 | ( 71 | 1.0f / _Dimensions.x, 0.0f, 0.0f, 0.0f, 72 | 0.0f, 1.0f / _Dimensions.y, 0.0f, 0.0f, 73 | 0.0f, 0.0f, 1.0f / _Dimensions.z, 0.0f, 74 | 0.0f, 0.0f, 0.0f, 1.0f 75 | ); 76 | o.normal = mul(mul(UNITY_MATRIX_IT_MV, scaleInverseTranspose), float4(v.normal, 0.0f)).xyz; 77 | #endif 78 | 79 | return o; 80 | } 81 | 82 | fixed4 frag (v2f i) : SV_Target 83 | { 84 | fixed4 color = _Color; 85 | 86 | #ifdef NORMAL_ON 87 | i.normal = normalize(i.normal); 88 | color.rgb *= 0.7f * i.normal.z + 0.3f; // darkest at 0.3f 89 | #endif 90 | 91 | return color; 92 | } 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/PrimitiveCore.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5addce54e9089824fb95ae41f409cfcf 3 | timeCreated: 1506925965 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/PrimitiveNoZTest.shader: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | Shader "CjLib/PrimitiveNoZTest" 13 | { 14 | SubShader 15 | { 16 | Tags { "Queue"="Overlay" "RenderType"="Transparent" "DisableBatching"="true" } 17 | LOD 100 18 | 19 | Pass 20 | { 21 | Blend SrcAlpha OneMinusSrcAlpha 22 | ZWrite Off ZTest Always 23 | 24 | CGPROGRAM 25 | #pragma shader_feature NORMAL_ON 26 | #pragma shader_feature CAP_SHIFT_SCALE 27 | #pragma vertex vert 28 | #pragma fragment frag 29 | #include "PrimitiveCore.cginc" 30 | ENDCG 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Debug/PrimitiveNoZTest.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: aa5e61c16eee1db4e85f6ef4483fea6d 3 | timeCreated: 1506845184 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9254b444dab1c474ea0ef5b3c7932238 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/CatmullRom.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_CATMULL_ROM 13 | #define CJ_LIB_CATMULL_ROM 14 | 15 | inline float catmullRom(float p0, float p1, float p2, float p3, float t) 16 | { 17 | float tt = t * t; 18 | return 19 | 0.5f 20 | * ((2.0f * p1) 21 | + (-p0 + p2) * t 22 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 23 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 24 | ); 25 | } 26 | 27 | inline float2 catmullRom(float2 p0, float2 p1, float2 p2, float2 p3, float t) 28 | { 29 | float tt = t * t; 30 | return 31 | 0.5f 32 | * ((2.0f * p1) 33 | + (-p0 + p2) * t 34 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 35 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 36 | ); 37 | } 38 | 39 | inline float3 catmullRom(float3 p0, float3 p1, float3 p2, float3 p3, float t) 40 | { 41 | float tt = t * t; 42 | return 43 | 0.5f 44 | * ((2.0f * p1) 45 | + (-p0 + p2) * t 46 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 47 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 48 | ); 49 | } 50 | 51 | inline float4 catmullRom(float4 p0, float4 p1, float4 p2, float4 p3, float t) 52 | { 53 | float tt = t * t; 54 | return 55 | 0.5f 56 | * ((2.0f * p1) 57 | + (-p0 + p2) * t 58 | + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * tt 59 | + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * tt * t 60 | ); 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/CatmullRom.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8faf7cc0acb5fcb4f8f2344c07918c16 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Color.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_COLOR 13 | #define CJ_LIB_COLOR 14 | 15 | float3 hsv2rgb(float3 hsv) 16 | { 17 | hsv.x = hsv.x - floor(hsv.x); 18 | int h = ((int) (hsv.x * 6)); 19 | float f = hsv.x * 6.0 - h; 20 | float p = hsv.z * (1.0 - hsv.y); 21 | float q = hsv.z * (1.0 - f * hsv.y); 22 | float t = hsv.z * (1.0 - (1.0 - f) * hsv.y); 23 | 24 | switch (h) 25 | { 26 | default: 27 | case 0: return float3(hsv.z, t, p); 28 | case 1: return float3(q, hsv.z, p); 29 | case 2: return float3(p, hsv.z, t); 30 | case 3: return float3(p, q, hsv.z); 31 | case 4: return float3(t, p, hsv.z); 32 | case 5: return float3(hsv.z, p, q); 33 | } 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Color.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d29168bdf7611fb43ae31c32f099264e 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Math.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_MATH 13 | #define CJ_LIB_MATH 14 | 15 | #define kPi (3.1415926535) 16 | #define kTwoPi (6.2831853071) 17 | #define kHalfPi (1.5707963267) 18 | #define kThirdPi (1.0471975511) 19 | #define kQuaterPi (0.7853981633) 20 | #define kFifthPi (0.6283185307) 21 | #define kSixthPi (0.5235987755) 22 | 23 | #define kSqrt2 (1.4142135623) 24 | #define kSqrt3 (1.7320508075) 25 | #define kSqrt2Inv (0.7071067811) 26 | #define kSqrt3Inv (0.5773502691) 27 | 28 | #define kEpsilon (0.0000000001) 29 | #define kEpsilonComp (0.9999999999) 30 | 31 | #define kRad2Deg (57.295779513) 32 | #define kDeg2Rad (0.0174532925) 33 | 34 | #include "Color.cginc" 35 | #include "Quaternion.cginc" 36 | #include "Vector.cginc" 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Math.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cbc2d2d27ca839e4a9a2e5662fd714ad 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Quaternion.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_QUATERNION 13 | #define CJ_LIB_QUATERNION 14 | 15 | #include "Math.cginc" 16 | #include "Vector.cginc" 17 | 18 | inline float4 quat_identity() 19 | { 20 | return float4(0.0f, 0.0f, 0.0f, 1.0f); 21 | } 22 | 23 | inline float4 quat_conj(float4 q) 24 | { 25 | return float4(-q.xyz, q.w); 26 | } 27 | 28 | // q must be unit quaternion 29 | inline float4 quat_pow(float4 q, float p) 30 | { 31 | float r = length(q.xyz); 32 | if (r < kEpsilon) 33 | return quat_identity(); 34 | 35 | float t = p * atan2(q.w, r); 36 | 37 | return float4(sin(t) * q.xyz / r, cos(t)); 38 | } 39 | 40 | inline float3 quat_rot(float4 q, float3 v) 41 | { 42 | return 43 | dot(q.xyz, v) * q.xyz 44 | + q.w * q.w * v 45 | + 2.0 * q.w * cross(q.xyz, v) 46 | - cross(cross(q.xyz, v), q.xyz); 47 | } 48 | 49 | inline float4 quat_axis_angle(float3 v, float a) 50 | { 51 | float h = 0.5 * a; 52 | return float4(sin(h) * normalize(v), cos(h)); 53 | } 54 | 55 | inline float4 quat_from_to(float3 from, float3 to) 56 | { 57 | float3 c = cross(from, to); 58 | float cc = dot(c, c); 59 | 60 | if (cc < kEpsilon) 61 | return quat_identity(); 62 | 63 | float3 axis = c / sqrt(cc); 64 | float angle = acos(clamp(dot(from, to), -1.0f, 1.0f)); 65 | return quat_axis_angle(axis, angle); 66 | } 67 | 68 | inline float3 quat_get_axis(float4 q) 69 | { 70 | float d = dot(q.xyz, q.xyz); 71 | return 72 | d > kEpsilon 73 | ? q.xyz / sqrt(d) 74 | : float3(0.0f, 0.0f, 1.0f); 75 | } 76 | 77 | inline float3 quat_get_angle(float4 q) 78 | { 79 | return 2.0f * acos(clamp(q.w, -1.0f, 1.0f)); 80 | } 81 | 82 | inline float4 quat_concat(float4 q1, float4 q2) 83 | { 84 | return 85 | float4 86 | ( 87 | q1.w * q2.xyz + q2.w * q1.xyz + cross(q1.xyz, q2.xyz), 88 | q1.w * q2.w - dot(q1.xyz, q2.xyz) 89 | ); 90 | } 91 | 92 | inline float4 quat_mat(float3x3 m) 93 | { 94 | float tr = m._m00 + m._m11 + m._m22; 95 | if (tr > 0.0f) { 96 | float s = sqrt(tr + 1.0f) * 2.0f; 97 | float sInv = 1.0f / s; 98 | return 99 | float4 100 | ( 101 | (m._m21 - m._m12) * sInv, 102 | (m._m02 - m._m20) * sInv, 103 | (m._m10 - m._m01) * sInv, 104 | 0.25 * s 105 | ); 106 | } 107 | else if ((m._m00 > m._m11) && (m._m00 > m._m22)) 108 | { 109 | float s = sqrt(1.0f + m._m00 - m._m11 - m._m22) * 2.0f; 110 | float sInv = 1.0f / s; 111 | return 112 | float4 113 | ( 114 | 0.25f * s, 115 | (m._m01 + m._m10) * sInv, 116 | (m._m02 + m._m20) * sInv, 117 | (m._m21 - m._m12) * sInv 118 | ); 119 | } 120 | else if (m._m11 > m._m22) 121 | { 122 | float s = sqrt(1.0f + m._m11 - m._m00 - m._m22) * 2.0f; 123 | float sInv = 1.0f / s; 124 | return 125 | float4 126 | ( 127 | (m._m01 + m._m10) * sInv, 128 | 0.25 * s, 129 | (m._m12 + m._m21) * sInv, 130 | (m._m02 - m._m20) * sInv 131 | ); 132 | } 133 | else { 134 | float s = sqrt(1.0f + m._m22 - m._m00 - m._m11) * 2.0f; 135 | float sInv = 1.0f / s; 136 | return 137 | float4 138 | ( 139 | (m._m02 + m._m20) * sInv, 140 | (m._m12 + m._m21) * sInv, 141 | 0.25 * s, 142 | (m._m10 - m._m01) * sInv 143 | ); 144 | } 145 | } 146 | 147 | inline float4 quat_look_at(float3 dir, float3 up) 148 | { 149 | return quat_mat(mat_look_at(dir, up)); 150 | } 151 | 152 | inline float4 slerp(float4 a, float4 b, float t) 153 | { 154 | float d = dot(normalize(a), normalize(b)); 155 | if (d > kEpsilonComp) 156 | { 157 | return lerp(a, b, t); 158 | } 159 | 160 | float r = acos(clamp(d, -1.0f, 1.0f)); 161 | return (sin((1.0 - t) * r) * a + sin(t * r) * b) / sin(r); 162 | } 163 | 164 | inline float4 nlerp(float4 a, float b, float t) 165 | { 166 | return normalize(lerp(a, b, t)); 167 | } 168 | 169 | #endif 170 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Quaternion.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4acf9f8acb3a9b408dcca8bc0f064ff 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Vector.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_VECTOR 13 | #define CJ_LIB_VECTOR 14 | 15 | #include "Math.cginc" 16 | 17 | inline float3 unit_x() { return float3(1.0f, 0.0f, 0.0f); } 18 | inline float3 unit_y() { return float3(0.0f, 1.0f, 0.0f); } 19 | inline float3 unit_z() { return float3(0.0f, 0.0f, 1.0f); } 20 | 21 | inline float3 normalize_safe(float3 v, float3 fallback) 22 | { 23 | float vv = dot(v, v); 24 | return vv > kEpsilon ? v / sqrt(vv) : fallback; 25 | } 26 | 27 | inline float3 normalize_safe(float3 v) 28 | { 29 | return normalize_safe(v, unit_z()); 30 | } 31 | 32 | inline float3 project_vec(float3 v, float3 onto) 33 | { 34 | onto = normalize(onto); 35 | return dot(v, onto) * onto; 36 | } 37 | 38 | inline float3 project_plane(float3 v, float3 n) 39 | { 40 | return v - project_vec(v, n); 41 | } 42 | 43 | inline float3 find_ortho(float3 v) 44 | { 45 | if (v.x >= kSqrt3Inv) 46 | return float3(v.y, -v.x, 0.0); 47 | else 48 | return float3(0.0, v.z, -v.y); 49 | } 50 | 51 | inline float3 slerp(float3 a, float3 b, float t) 52 | { 53 | float d = dot(normalize(a), normalize(b)); 54 | if (d > kEpsilonComp) 55 | { 56 | return lerp(a, b, t); 57 | } 58 | 59 | float r = acos(clamp(d, -1.0f, 1.0f)); 60 | return (sin((1.0 - t) * r) * a + sin(t * r) * b) / sin(r); 61 | } 62 | 63 | inline float3 nlerp(float3 a, float b, float t) 64 | { 65 | return normalize(lerp(a, b, t)); 66 | } 67 | 68 | inline float3x3 mat_basis(float3 xAxis, float3 yAxis, float3 zAxis) 69 | { 70 | return transpose(float3x3(xAxis, yAxis, zAxis)); 71 | } 72 | 73 | inline float3x3 mat_look_at(float3 dir, float3 up) 74 | { 75 | float3 zAxis = normalize_safe(dir, unit_z()); 76 | float3 xAxis = normalize_safe(cross(up, zAxis), unit_x()); 77 | float3 yAxis = cross(zAxis, xAxis); 78 | return mat_basis(xAxis, yAxis, zAxis); 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Math/Vector.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fa89081643259a543bf0f9f2dc1300cc 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 29d5af0e6172a384bb50c6831f2dbd1d 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/ClassicNoise2D.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | Based on project "webgl-noise" by Ashima Arts. 11 | Description : Array and textureless GLSL 2D simplex noise function. 12 | Author : Ian McEwan, Ashima Arts. 13 | Maintainer : ijm 14 | Lastmod : 20110822 (ijm) 15 | License : Copyright (C) 2011 Ashima Arts. All rights reserved. 16 | Distributed under the MIT License. See LICENSE file. 17 | https://github.com/ashima/webgl-noise 18 | */ 19 | /******************************************************************************/ 20 | 21 | 22 | #ifndef CJ_LIB_CLASSIC_NOISE_2D 23 | #define CJ_LIB_CLASSIC_NOISE_2D 24 | 25 | #include "NoiseCommon.cginc" 26 | 27 | // classic Perlin noise 28 | // single octave 29 | float cnoise(float2 P) 30 | { 31 | float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0); 32 | float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0); 33 | Pi = mod289(Pi); // To avoid truncation effects in permutation 34 | float4 ix = Pi.xzxz; 35 | float4 iy = Pi.yyww; 36 | float4 fx = Pf.xzxz; 37 | float4 fy = Pf.yyww; 38 | 39 | float4 i = permute(permute(ix) + iy); 40 | 41 | float4 gx = frac(i / 41.0) * 2.0 - 1.0 ; 42 | float4 gy = abs(gx) - 0.5 ; 43 | float4 tx = floor(gx + 0.5); 44 | gx = gx - tx; 45 | 46 | float2 g00 = float2(gx.x,gy.x); 47 | float2 g10 = float2(gx.y,gy.y); 48 | float2 g01 = float2(gx.z,gy.z); 49 | float2 g11 = float2(gx.w,gy.w); 50 | 51 | float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 52 | g00 *= norm.x; 53 | g01 *= norm.y; 54 | g10 *= norm.z; 55 | g11 *= norm.w; 56 | 57 | float n00 = dot(g00, float2(fx.x, fy.x)); 58 | float n10 = dot(g10, float2(fx.y, fy.y)); 59 | float n01 = dot(g01, float2(fx.z, fy.z)); 60 | float n11 = dot(g11, float2(fx.w, fy.w)); 61 | 62 | float2 fade_xy = fade(Pf.xy); 63 | float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x); 64 | float n_xy = lerp(n_x.x, n_x.y, fade_xy.y); 65 | return 2.3 * n_xy; 66 | } 67 | 68 | // multiple octaves 69 | DEFINE_NOISE_FUNC_MULTIPLE_OCTAVES(cnoise, float, float2, 0.5); 70 | 71 | // classic Perlin noise, periodic variant 72 | // single octave 73 | float pnoise(float2 P, float2 rep) 74 | { 75 | float4 Pi = floor(P.xyxy) + float4(0.0, 0.0, 1.0, 1.0); 76 | float4 Pf = frac (P.xyxy) - float4(0.0, 0.0, 1.0, 1.0); 77 | Pi = mod(Pi, rep.xyxy); // To create noise with explicit period 78 | Pi = mod289(Pi); // To avoid truncation effects in permutation 79 | float4 ix = Pi.xzxz; 80 | float4 iy = Pi.yyww; 81 | float4 fx = Pf.xzxz; 82 | float4 fy = Pf.yyww; 83 | 84 | float4 i = permute(permute(ix) + iy); 85 | 86 | float4 gx = frac(i / 41.0) * 2.0 - 1.0 ; 87 | float4 gy = abs(gx) - 0.5 ; 88 | float4 tx = floor(gx + 0.5); 89 | gx = gx - tx; 90 | 91 | float2 g00 = float2(gx.x,gy.x); 92 | float2 g10 = float2(gx.y,gy.y); 93 | float2 g01 = float2(gx.z,gy.z); 94 | float2 g11 = float2(gx.w,gy.w); 95 | 96 | float4 norm = taylorInvSqrt(float4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11))); 97 | g00 *= norm.x; 98 | g01 *= norm.y; 99 | g10 *= norm.z; 100 | g11 *= norm.w; 101 | 102 | float n00 = dot(g00, float2(fx.x, fy.x)); 103 | float n10 = dot(g10, float2(fx.y, fy.y)); 104 | float n01 = dot(g01, float2(fx.z, fy.z)); 105 | float n11 = dot(g11, float2(fx.w, fy.w)); 106 | 107 | float2 fade_xy = fade(Pf.xy); 108 | float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x); 109 | float n_xy = lerp(n_x.x, n_x.y, fade_xy.y); 110 | return 2.3 * n_xy; 111 | } 112 | 113 | // multiple octave 114 | DEFINE_PERIODIC_NOISE_FUNC_MULTIPLE_OCTAVES(pnoise, float, float2); 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/ClassicNoise2D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8a0a039eee7b0d048a94b088878a737a 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/ClassicNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3bb8e11abde62a34e9b3c23f1b163708 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Noise.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | */ 11 | /******************************************************************************/ 12 | 13 | #ifndef NOISE 14 | #define NOISE 15 | 16 | #include "ClassicNoise2D.cginc" 17 | #include "ClassicNoise3D.cginc" 18 | #include "SimplexNoise2D.cginc" 19 | #include "SimplexNoise3D.cginc" 20 | 21 | #include "RandomNoise.cginc" 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Noise.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5428bc95309813640956652494e668d1 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/NoiseCommon.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d888eae3bfbc1b44dadb3c139a77443a 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/RandomNoise.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | Based on an Andy Gryc's version of the common one-line shader random noise 11 | http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand/ 12 | 13 | */ 14 | /******************************************************************************/ 15 | 16 | 17 | #ifndef CJ_LIB_RANDOM_NOISE 18 | #define CJ_LIB_RANDOM_NOISE 19 | 20 | #include "NoiseCommon.cginc" 21 | 22 | float rand(float s) 23 | { 24 | return frac(sin(mod(s, 6.2831853)) * 43758.5453123); 25 | } 26 | 27 | 28 | float rand(float2 s) 29 | { 30 | float d = dot(s + 0.1234567, float2(1111112.9819837, 78.237173)); 31 | float m = mod(d, 6.2831853); 32 | return frac(sin(m) * 43758.5453123); 33 | } 34 | 35 | float rand(float3 s) 36 | { 37 | float d = dot(s + 0.1234567, float3(11112.9819837, 378.237173, 3971977.9173179)); 38 | float m = mod(d, 6.2831853); 39 | return frac(sin(m) * 43758.5453123); 40 | } 41 | 42 | float rand_range(float s, float a, float b) 43 | { 44 | return a + (b - a) * rand(s); 45 | } 46 | 47 | float2 rand_range(float2 s, float2 a, float2 b) 48 | { 49 | return a + (b - a) * rand(s); 50 | } 51 | 52 | float3 rand_range(float3 s, float3 a, float3 b) 53 | { 54 | return a + (b - a) * rand(s); 55 | } 56 | 57 | float2 rand_uvec(float2 s) 58 | { 59 | return normalize(float2(rand(s), rand(s * 1.23456789)) - 0.5); 60 | } 61 | 62 | float3 rand_uvec(float3 s) 63 | { 64 | return normalize(float3(rand(s), rand(s * 1.23456789), rand(s * 9876.54321)) - 0.5); 65 | } 66 | 67 | float2 rand_vec(float2 s) 68 | { 69 | return rand_uvec(s) * rand(s * 9876.54321); 70 | } 71 | 72 | float3 rand_vec(float3 s) 73 | { 74 | return rand_uvec(s) * rand(s * 1357975.31313); 75 | } 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/RandomNoise.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d6d41152c5085d749b5d5d890fa0dc81 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1fd5ac2c904801a478dcc3481cdc9ce4 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/ClassicNoiseCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #pragma kernel ClassicGrid2 13 | #pragma kernel ClassicGrid3 14 | #pragma kernel ClassicCustom2 15 | #pragma kernel ClassicCustom3 16 | 17 | #include "../ClassicNoise2D.cginc" 18 | #include "../ClassicNoise3D.cginc" 19 | #include "../RandomNoise.cginc" 20 | 21 | RWStructuredBuffer input2; 22 | RWStructuredBuffer input3; 23 | RWStructuredBuffer output; 24 | 25 | int3 dimension; 26 | float3 scale; 27 | float3 offset; 28 | int numOctaves; 29 | float octaveOffsetFactor; 30 | 31 | [numthreads(1, 1, 1)] 32 | void ClassicGrid2(uint3 id : SV_DispatchThreadID) 33 | { 34 | float2 s = id.xy / scale.xy; 35 | output[index(id, dimension)] = 36 | cnoise(s, offset.xy, numOctaves, octaveOffsetFactor); 37 | } 38 | 39 | [numthreads(1, 1, 1)] 40 | void ClassicGrid3(uint3 id : SV_DispatchThreadID) 41 | { 42 | float3 s = id / scale; 43 | output[index(id, dimension)] = 44 | cnoise(s, offset, numOctaves, octaveOffsetFactor); 45 | } 46 | 47 | [numthreads(1, 1, 1)] 48 | void ClassicCustom2(uint3 id : SV_DispatchThreadID) 49 | { 50 | float2 s = input2[id.x] / scale.xy; 51 | output[id.x] = 52 | cnoise(s, offset.xy, numOctaves, octaveOffsetFactor); 53 | } 54 | 55 | [numthreads(1, 1, 1)] 56 | void ClassicCustom3(uint3 id : SV_DispatchThreadID) 57 | { 58 | float3 s = input3[id.x] / scale; 59 | output[id.x] = 60 | cnoise(s, offset, numOctaves, octaveOffsetFactor); 61 | } 62 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/ClassicNoiseCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2d47a6b2a48ecf647875c4bedbc8d0c2 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/ClassicNoisePeriodicCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #pragma kernel ClassicPeriodicGrid2 13 | #pragma kernel ClassicPeriodicGrid3 14 | #pragma kernel ClassicPeriodicCustom2 15 | #pragma kernel ClassicPeriodicCustom3 16 | 17 | #include "../ClassicNoise2D.cginc" 18 | #include "../ClassicNoise3D.cginc" 19 | 20 | RWStructuredBuffer input2; 21 | RWStructuredBuffer input3; 22 | RWStructuredBuffer output; 23 | 24 | int3 dimension; 25 | float3 scale; 26 | float3 offset; 27 | float3 period; 28 | int numOctaves; 29 | float octaveOffsetFactor; 30 | 31 | [numthreads(1, 1, 1)] 32 | void ClassicPeriodicGrid2(uint3 id : SV_DispatchThreadID) 33 | { 34 | float2 s = id.xy / scale.xy; 35 | output[index(id, dimension)] = 36 | pnoise(s, offset.xy, period.xy, numOctaves, octaveOffsetFactor); 37 | } 38 | 39 | [numthreads(1, 1, 1)] 40 | void ClassicPeriodicGrid3(uint3 id : SV_DispatchThreadID) 41 | { 42 | float3 s = id / scale; 43 | output[index(id, dimension)] = 44 | pnoise(s, offset, period, numOctaves, octaveOffsetFactor); 45 | } 46 | 47 | [numthreads(1, 1, 1)] 48 | void ClassicPeriodicCustom2(uint3 id : SV_DispatchThreadID) 49 | { 50 | float2 s = input2[id.x] / scale.xy; 51 | output[id.x] = 52 | pnoise(s, offset.xy, period.xy, numOctaves, octaveOffsetFactor); 53 | } 54 | 55 | [numthreads(1, 1, 1)] 56 | void ClassicPeriodicCustom3(uint3 id : SV_DispatchThreadID) 57 | { 58 | float3 s = input3[id.x] / scale; 59 | output[id.x] = 60 | pnoise(s, offset, period, numOctaves, octaveOffsetFactor); 61 | } 62 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/ClassicNoisePeriodicCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: afe6add88b0b89d448b26e6e3dc5f59b 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/RandomNoiseCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #pragma kernel RandomGrid1 13 | #pragma kernel RandomGrid2 14 | #pragma kernel RandomGrid3 15 | 16 | #include "../RandomNoise.cginc" 17 | 18 | RWStructuredBuffer output; 19 | 20 | float seed; 21 | int3 dimension; 22 | 23 | [numthreads(1, 1, 1)] 24 | void RandomGrid1(uint3 id : SV_DispatchThreadID) 25 | { 26 | output[index(id, dimension)] = rand(seed + id.x); 27 | } 28 | 29 | [numthreads(1, 1, 1)] 30 | void RandomGrid2(uint3 id : SV_DispatchThreadID) 31 | { 32 | output[index(id, dimension)] = rand(seed + id.xy); 33 | } 34 | 35 | [numthreads(1, 1, 1)] 36 | void RandomGrid3(uint3 id : SV_DispatchThreadID) 37 | { 38 | output[index(id, dimension)] = rand(seed + id.xyz); 39 | } 40 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/RandomNoiseCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 17f67342dcf81694fbd8e72def0acced 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/RandomNoiseVectorCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #pragma kernel RandomVec2 13 | #pragma kernel RandomVec3 14 | 15 | #include "../RandomNoise.cginc" 16 | 17 | RWStructuredBuffer output2; 18 | RWStructuredBuffer output3; 19 | 20 | float seed; 21 | int3 dimension; 22 | 23 | [numthreads(1, 1, 1)] 24 | void RandomVec2(uint3 id : SV_DispatchThreadID) 25 | { 26 | output2[index(id, dimension)] = rand_vec(seed + id.xy); 27 | } 28 | 29 | [numthreads(1, 1, 1)] 30 | void RandomVec3(uint3 id : SV_DispatchThreadID) 31 | { 32 | output3[index(id, dimension)] = rand_vec(seed + id.xyz); 33 | } -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/RandomNoiseVectorCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: db6578c993355594ba41b3c1335a9adf 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/SimplexNoiseCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #pragma kernel SimplexGrid2 13 | #pragma kernel SimplexGrid3 14 | #pragma kernel SimplexCustom2 15 | #pragma kernel SimplexCustom3 16 | 17 | #include "../SimplexNoise2D.cginc" 18 | #include "../SimplexNoise3D.cginc" 19 | #include "../RandomNoise.cginc" 20 | 21 | RWStructuredBuffer input2; 22 | RWStructuredBuffer input3; 23 | RWStructuredBuffer output; 24 | 25 | int3 dimension; 26 | float3 scale; 27 | float3 offset; 28 | int numOctaves; 29 | float octaveOffsetFactor; 30 | 31 | [numthreads(1, 1, 1)] 32 | void SimplexGrid2(uint3 id : SV_DispatchThreadID) 33 | { 34 | float2 s = id.xy / scale.xy; 35 | output[index(id, dimension)] = 36 | snoise(s, offset.xy, numOctaves, octaveOffsetFactor); 37 | } 38 | 39 | [numthreads(1, 1, 1)] 40 | void SimplexGrid3(uint3 id : SV_DispatchThreadID) 41 | { 42 | float3 s = id / scale; 43 | output[index(id, dimension)] = 44 | snoise(s, offset, numOctaves, octaveOffsetFactor); 45 | } 46 | 47 | [numthreads(1, 1, 1)] 48 | void SimplexCustom2(uint3 id : SV_DispatchThreadID) 49 | { 50 | float2 s = input2[id.x] / scale.xy; 51 | output[id.x] = 52 | snoise(s, offset.xy, numOctaves, octaveOffsetFactor); 53 | } 54 | 55 | [numthreads(1, 1, 1)] 56 | void SimplexCustom3(uint3 id : SV_DispatchThreadID) 57 | { 58 | float3 s = input3[id.x] / scale; 59 | output[id.x] = 60 | snoise(s, offset, numOctaves, octaveOffsetFactor); 61 | } 62 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/SimplexNoiseCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4eb8152e697df05409038674dbc8bb4b 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/SimplexNoiseGradientCs.compute: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | 13 | #pragma kernel SimplexGradientGrid2 14 | #pragma kernel SimplexGradientGrid3 15 | #pragma kernel SimplexGradientCustom2 16 | #pragma kernel SimplexGradientCustom3 17 | 18 | #include "../SimplexNoise2D.cginc" 19 | #include "../SimplexNoise3D.cginc" 20 | 21 | RWStructuredBuffer input2; 22 | RWStructuredBuffer input3; 23 | RWStructuredBuffer output2; 24 | RWStructuredBuffer output3; 25 | 26 | int3 dimension; 27 | float3 scale; 28 | float3 offset; 29 | int numOctaves; 30 | float octaveOffsetFactor; 31 | 32 | // grid sample points 33 | [numthreads(1, 1, 1)] 34 | void SimplexGradientGrid2(uint3 id : SV_DispatchThreadID) 35 | { 36 | float2 s = id.xy / scale.xy; 37 | output2[index(id, dimension)] = 38 | snoise_grad(s, offset.xy, numOctaves, octaveOffsetFactor).xy; 39 | } 40 | 41 | // grid sample points 42 | [numthreads(1, 1, 1)] 43 | void SimplexGradientGrid3(uint3 id : SV_DispatchThreadID) 44 | { 45 | float3 s = id / scale; 46 | output3[index(id, dimension)] = 47 | snoise_grad(s, offset, numOctaves, octaveOffsetFactor).xyz; 48 | } 49 | 50 | // custom sample points 51 | [numthreads(1, 1, 1)] 52 | void SimplexGradientCustom2(uint3 id : SV_DispatchThreadID) 53 | { 54 | float2 s = input2[id.x] / scale.xy; 55 | output2[id.x] = 56 | snoise_grad(s, offset.xy, numOctaves, octaveOffsetFactor).xy; 57 | } 58 | 59 | // custom sample points 60 | [numthreads(1, 1, 1)] 61 | void SimplexGradientCustom3(uint3 id : SV_DispatchThreadID) 62 | { 63 | float3 s = input3[id.x] / scale; 64 | output3[id.x] = 65 | snoise_grad(s, offset, numOctaves, octaveOffsetFactor).xyz; 66 | } 67 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/Resources/SimplexNoiseGradientCs.compute.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7957dd6eaa0f4074383bdba2b668fe69 3 | ComputeShaderImporter: 4 | externalObjects: {} 5 | currentAPIMask: 4 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/SimplexNoise2D.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | Based on project "webgl-noise" by Ashima Arts. 11 | Description : Array and textureless GLSL 2D simplex noise function. 12 | Author : Ian McEwan, Ashima Arts. 13 | Maintainer : ijm 14 | Lastmod : 20110822 (ijm) 15 | License : Copyright (C) 2011 Ashima Arts. All rights reserved. 16 | Distributed under the MIT License. See LICENSE file. 17 | https://github.com/ashima/webgl-noise 18 | */ 19 | /******************************************************************************/ 20 | 21 | #ifndef CJ_LIB_SIMPLEX_NOISE_2D 22 | #define CJ_LIB_SIMPLEX_NOISE_2D 23 | 24 | #include "NoiseCommon.cginc" 25 | 26 | // single octave 27 | float snoise(float2 v) 28 | { 29 | const float4 C = float4( 0.211324865405187, // (3.0-sqrt(3.0))/6.0 30 | 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) 31 | -0.577350269189626, // -1.0 + 2.0 * C.x 32 | 0.024390243902439); // 1.0 / 41.0 33 | // First corner 34 | float2 i = floor(v + dot(v, C.yy)); 35 | float2 x0 = v - i + dot(i, C.xx); 36 | 37 | // Other corners 38 | float2 i1; 39 | i1.x = step(x0.y, x0.x); 40 | i1.y = 1.0 - i1.x; 41 | 42 | // x1 = x0 - i1 + 1.0 * C.xx; 43 | // x2 = x0 - 1.0 + 2.0 * C.xx; 44 | float2 x1 = x0 + C.xx - i1; 45 | float2 x2 = x0 + C.zz; 46 | 47 | // Permutations 48 | i = mod289(i); // Avoid truncation effects in permutation 49 | float3 p = 50 | permute(permute(i.y + float3(0.0, i1.y, 1.0)) 51 | + i.x + float3(0.0, i1.x, 1.0)); 52 | 53 | float3 m = max(0.5 - float3(dot(x0, x0), dot(x1, x1), dot(x2, x2)), 0.0); 54 | m = m * m; 55 | m = m * m; 56 | 57 | // Gradients: 41 points uniformly over a line, mapped onto a diamond. 58 | // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) 59 | float3 x = 2.0 * frac(p * C.www) - 1.0; 60 | float3 h = abs(x) - 0.5; 61 | float3 ox = floor(x + 0.5); 62 | float3 a0 = x - ox; 63 | 64 | // Normalise gradients implicitly by scaling m 65 | m *= taylorInvSqrt(a0 * a0 + h * h); 66 | 67 | // Compute final noise value at P 68 | float3 g; 69 | g.x = a0.x * x0.x + h.x * x0.y; 70 | g.y = a0.y * x1.x + h.y * x1.y; 71 | g.z = a0.z * x2.x + h.z * x2.y; 72 | return 130.0 * dot(m, g); 73 | } 74 | 75 | // multiple octaves 76 | DEFINE_NOISE_FUNC_MULTIPLE_OCTAVES(snoise, float, float2, 0.5); 77 | 78 | // single octave 79 | float3 snoise_grad(float2 v) 80 | { 81 | const float4 C = float4( 0.211324865405187, // (3.0-sqrt(3.0))/6.0 82 | 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) 83 | -0.577350269189626, // -1.0 + 2.0 * C.x 84 | 0.024390243902439); // 1.0 / 41.0 85 | // First corner 86 | float2 i = floor(v + dot(v, C.yy)); 87 | float2 x0 = v - i + dot(i, C.xx); 88 | 89 | // Other corners 90 | float2 i1; 91 | i1.x = step(x0.y, x0.x); 92 | i1.y = 1.0 - i1.x; 93 | 94 | // x1 = x0 - i1 + 1.0 * C.xx; 95 | // x2 = x0 - 1.0 + 2.0 * C.xx; 96 | float2 x1 = x0 + C.xx - i1; 97 | float2 x2 = x0 + C.zz; 98 | 99 | // Permutations 100 | i = mod289(i); // Avoid truncation effects in permutation 101 | float3 p = 102 | permute(permute(i.y + float3(0.0, i1.y, 1.0)) 103 | + i.x + float3(0.0, i1.x, 1.0)); 104 | 105 | float3 m = max(0.5 - float3(dot(x0, x0), dot(x1, x1), dot(x2, x2)), 0.0); 106 | float3 m2 = m * m; 107 | float3 m3 = m2 * m; 108 | float3 m4 = m2 * m2; 109 | 110 | // Gradients: 41 points uniformly over a line, mapped onto a diamond. 111 | // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) 112 | float3 x = 2.0 * frac(p * C.www) - 1.0; 113 | float3 h = abs(x) - 0.5; 114 | float3 ox = floor(x + 0.5); 115 | float3 a0 = x - ox; 116 | 117 | // Normalise gradients 118 | float3 norm = taylorInvSqrt(a0 * a0 + h * h); 119 | float2 g0 = float2(a0.x, h.x) * norm.x; 120 | float2 g1 = float2(a0.y, h.y) * norm.y; 121 | float2 g2 = float2(a0.z, h.z) * norm.z; 122 | 123 | // Compute noise and gradient at P 124 | float2 grad = 125 | -6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 + 126 | -6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 + 127 | -6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2; 128 | float3 px = float3(dot(x0, g0), dot(x1, g1), dot(x2, g2)); 129 | return 130.0 * float3(grad, dot(m4, px)); 130 | } 131 | 132 | // multiple octaves 133 | DEFINE_NOISE_FUNC_MULTIPLE_OCTAVES(snoise_grad, float3, float2, 0.0); 134 | 135 | #endif 136 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/SimplexNoise2D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c05b16f88a1d0ba419095712496e35e6 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/SimplexNoise3D.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | 10 | Based on project "webgl-noise" by Ashima Arts. 11 | Description : Array and textureless GLSL 2D simplex noise function. 12 | Author : Ian McEwan, Ashima Arts. 13 | Maintainer : ijm 14 | Lastmod : 20110822 (ijm) 15 | License : Copyright (C) 2011 Ashima Arts. All rights reserved. 16 | Distributed under the MIT License. See LICENSE file. 17 | https://github.com/ashima/webgl-noise 18 | */ 19 | /******************************************************************************/ 20 | 21 | #ifndef CJ_LIB_SIMPLEX_NOISE_3D 22 | #define CJ_LIB_SIMPLEX_NOISE_3D 23 | 24 | #include "NoiseCommon.cginc" 25 | 26 | // single octave 27 | float snoise(float3 v) 28 | { 29 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 30 | 31 | // First corner 32 | float3 i = floor(v + dot(v, C.yyy)); 33 | float3 x0 = v - i + dot(i, C.xxx); 34 | 35 | // Other corners 36 | float3 g = step(x0.yzx, x0.xyz); 37 | float3 l = 1.0 - g; 38 | float3 i1 = min(g.xyz, l.zxy); 39 | float3 i2 = max(g.xyz, l.zxy); 40 | 41 | // x1 = x0 - i1 + 1.0 * C.xxx; 42 | // x2 = x0 - i2 + 2.0 * C.xxx; 43 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 44 | float3 x1 = x0 - i1 + C.xxx; 45 | float3 x2 = x0 - i2 + C.yyy; 46 | float3 x3 = x0 - 0.5; 47 | 48 | // Permutations 49 | i = mod289(i); // Avoid truncation effects in permutation 50 | float4 p = 51 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 52 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 53 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 54 | 55 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 56 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 57 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 58 | 59 | float4 x_ = floor(j / 7.0); 60 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 61 | 62 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 63 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 64 | 65 | float4 h = 1.0 - abs(x) - abs(y); 66 | 67 | float4 b0 = float4(x.xy, y.xy); 68 | float4 b1 = float4(x.zw, y.zw); 69 | 70 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 71 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 72 | float4 s0 = floor(b0) * 2.0 + 1.0; 73 | float4 s1 = floor(b1) * 2.0 + 1.0; 74 | float4 sh = -step(h, 0.0); 75 | 76 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 77 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 78 | 79 | float3 g0 = float3(a0.xy, h.x); 80 | float3 g1 = float3(a0.zw, h.y); 81 | float3 g2 = float3(a1.xy, h.z); 82 | float3 g3 = float3(a1.zw, h.w); 83 | 84 | // Normalise gradients 85 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 86 | g0 *= norm.x; 87 | g1 *= norm.y; 88 | g2 *= norm.z; 89 | g3 *= norm.w; 90 | 91 | // Mix final noise value 92 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 93 | m = m * m; 94 | m = m * m; 95 | 96 | float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3)); 97 | return 42.0 * dot(m, px); 98 | } 99 | 100 | // multiple octaves 101 | DEFINE_NOISE_FUNC_MULTIPLE_OCTAVES(snoise, float, float3, 0.5); 102 | 103 | // single octave 104 | float4 snoise_grad(float3 v) 105 | { 106 | const float2 C = float2(1.0 / 6.0, 1.0 / 3.0); 107 | 108 | // First corner 109 | float3 i = floor(v + dot(v, C.yyy)); 110 | float3 x0 = v - i + dot(i, C.xxx); 111 | 112 | // Other corners 113 | float3 g = step(x0.yzx, x0.xyz); 114 | float3 l = 1.0 - g; 115 | float3 i1 = min(g.xyz, l.zxy); 116 | float3 i2 = max(g.xyz, l.zxy); 117 | 118 | // x1 = x0 - i1 + 1.0 * C.xxx; 119 | // x2 = x0 - i2 + 2.0 * C.xxx; 120 | // x3 = x0 - 1.0 + 3.0 * C.xxx; 121 | float3 x1 = x0 - i1 + C.xxx; 122 | float3 x2 = x0 - i2 + C.yyy; 123 | float3 x3 = x0 - 0.5; 124 | 125 | // Permutations 126 | i = mod289(i); // Avoid truncation effects in permutation 127 | float4 p = 128 | permute(permute(permute(i.z + float4(0.0, i1.z, i2.z, 1.0)) 129 | + i.y + float4(0.0, i1.y, i2.y, 1.0)) 130 | + i.x + float4(0.0, i1.x, i2.x, 1.0)); 131 | 132 | // Gradients: 7x7 points over a square, mapped onto an octahedron. 133 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) 134 | float4 j = p - 49.0 * floor(p / 49.0); // mod(p,7*7) 135 | 136 | float4 x_ = floor(j / 7.0); 137 | float4 y_ = floor(j - 7.0 * x_); // mod(j,N) 138 | 139 | float4 x = (x_ * 2.0 + 0.5) / 7.0 - 1.0; 140 | float4 y = (y_ * 2.0 + 0.5) / 7.0 - 1.0; 141 | 142 | float4 h = 1.0 - abs(x) - abs(y); 143 | 144 | float4 b0 = float4(x.xy, y.xy); 145 | float4 b1 = float4(x.zw, y.zw); 146 | 147 | //float4 s0 = float4(lessThan(b0, 0.0)) * 2.0 - 1.0; 148 | //float4 s1 = float4(lessThan(b1, 0.0)) * 2.0 - 1.0; 149 | float4 s0 = floor(b0) * 2.0 + 1.0; 150 | float4 s1 = floor(b1) * 2.0 + 1.0; 151 | float4 sh = -step(h, 0.0); 152 | 153 | float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 154 | float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 155 | 156 | float3 g0 = float3(a0.xy, h.x); 157 | float3 g1 = float3(a0.zw, h.y); 158 | float3 g2 = float3(a1.xy, h.z); 159 | float3 g3 = float3(a1.zw, h.w); 160 | 161 | // Normalise gradients 162 | float4 norm = taylorInvSqrt(float4(dot(g0, g0), dot(g1, g1), dot(g2, g2), dot(g3, g3))); 163 | g0 *= norm.x; 164 | g1 *= norm.y; 165 | g2 *= norm.z; 166 | g3 *= norm.w; 167 | 168 | // Compute noise and gradient at P 169 | float4 m = max(0.6 - float4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 170 | float4 m2 = m * m; 171 | float4 m3 = m2 * m; 172 | float4 m4 = m2 * m2; 173 | float3 grad = 174 | -6.0 * m3.x * x0 * dot(x0, g0) + m4.x * g0 + 175 | -6.0 * m3.y * x1 * dot(x1, g1) + m4.y * g1 + 176 | -6.0 * m3.z * x2 * dot(x2, g2) + m4.z * g2 + 177 | -6.0 * m3.w * x3 * dot(x3, g3) + m4.w * g3; 178 | float4 px = float4(dot(x0, g0), dot(x1, g1), dot(x2, g2), dot(x3, g3)); 179 | return 42.0 * float4(grad, dot(m4, px)); 180 | } 181 | 182 | // multiple octaves 183 | DEFINE_NOISE_FUNC_MULTIPLE_OCTAVES(snoise_grad, float4, float3, 0.0); 184 | 185 | #endif 186 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Noise/SimplexNoise3D.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c474d07a7f2949a45b86bf61f6a3fa29 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Physics.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3eaa3e9ee906aa0498047407a76af341 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Physics/Physics.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_PHYSICS 13 | #define CJ_LIB_PHYSICS 14 | 15 | 16 | #include "../Math/Math.cginc" 17 | 18 | 19 | // common 20 | //----------------------------------------------------------------------------- 21 | 22 | struct CollisionResult 23 | { 24 | float3 position; 25 | float3 velocity; 26 | }; 27 | 28 | inline CollisionResult resolveCollision(float3 pos, float3 norm, float3 vel, float penetration, float restitution, float friction) 29 | { 30 | float d = dot(vel, norm); // projected relative speed onto contact normal 31 | float f = -d / length(vel); // ratio of relative speed along contact normal 32 | float3 velN = d * norm; // normal relative velocity 33 | float3 velT = vel - velN; // tangential relative velocity 34 | float3 velResolution = -(1.0 + restitution) * velN - friction * f * velT; 35 | 36 | CollisionResult res; 37 | res.position = pos + penetration * norm; 38 | res.velocity = vel + step(kEpsilon, penetration) * velResolution; 39 | return res; 40 | } 41 | 42 | //----------------------------------------------------------------------------- 43 | // end: common 44 | 45 | 46 | // VS plane 47 | //----------------------------------------------------------------------------- 48 | 49 | inline CollisionResult pointVsPlane(float3 p, float4 plane, float3 vel, float restitution, float friction) 50 | { 51 | float penetration = max(0.0, -dot(float4(p, 1.0), plane)); 52 | float3 norm = plane.xyz; 53 | return resolveCollision(p, norm, vel, penetration, restitution, friction); 54 | } 55 | 56 | inline CollisionResult sphereVsPlane(float4 s, float4 plane, float3 vel, float restitution, float friction) 57 | { 58 | float penetration = max(0.0, s.w - dot(float4(s.xyz, 1.0), plane)); 59 | float3 norm = plane.xyz; 60 | return resolveCollision(s.xyz, norm, vel, penetration, restitution, friction); 61 | } 62 | 63 | //----------------------------------------------------------------------------- 64 | // end: VS plane 65 | 66 | 67 | // VS sphere 68 | //----------------------------------------------------------------------------- 69 | 70 | inline CollisionResult pointVsSphere(float3 p, float4 sphere, float3 vel, float restitution, float friction) 71 | { 72 | float3 centerDiff = p - sphere.xyz; 73 | float centerDiffLen = length(centerDiff); 74 | float penetration = max(0.0, sphere.w - centerDiffLen); 75 | float3 norm = centerDiff / centerDiffLen; 76 | return resolveCollision(p, norm, vel, penetration, restitution, friction); 77 | } 78 | 79 | inline CollisionResult sphereVsSphere(float4 s, float4 sphere, float3 vel, float restitution, float friction) 80 | { 81 | float3 centerDiff = s.xyz - sphere.xyz; 82 | float centerDiffLen = length(centerDiff); 83 | float penetration = max(0.0, s.w + sphere.w - centerDiffLen); 84 | float3 norm = centerDiff / centerDiffLen; 85 | return resolveCollision(s.xyz, norm, vel, penetration, restitution, friction); 86 | } 87 | 88 | //----------------------------------------------------------------------------- 89 | // end: VS sphere 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Physics/Physics.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7a57aa193bee1d6498620b4468cf25d7 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Render.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fd18b40526c4584bb9bcebda9c06062 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Render/Forward.cginc: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Unity CJ Lib 4 | https://github.com/TheAllenChou/unity-cj-lib 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | #ifndef CJ_LIB_FORWARD 13 | #define CJ_LIB_FORWARD 14 | 15 | #include "UnityStandardCore.cginc" 16 | 17 | #define CJ_LIB_SHADE_MAIN_LIGHT(v, posWs, normWs) \ 18 | ( \ 19 | dot(normWs, normalize(WorldSpaceLightDir(float4(posWs.xyz, 1.0)))) \ 20 | * LIGHT_ATTENUATION(v) \ 21 | ) 22 | 23 | #define CJ_LIB_SHADE_POINT_LIGHTS(posWs, normWs) \ 24 | ( \ 25 | Shade4PointLights \ 26 | ( \ 27 | unity_4LightPosX0, \ 28 | unity_4LightPosY0, \ 29 | unity_4LightPosZ0, \ 30 | unity_LightColor[0].rgb, \ 31 | unity_LightColor[1].rgb, \ 32 | unity_LightColor[2].rgb, \ 33 | unity_LightColor[3].rgb, \ 34 | unity_4LightAtten0, \ 35 | posWs.xyz, \ 36 | normWs.xyz \ 37 | ) \ 38 | ) 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/CjLib/Shader/Render/Forward.cginc.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 79c286afcc217ff498d2659c8c5d82b1 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 06060bd91cd5cfe4ebe50e843ad6605c 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01a - Inline Ground Constraint.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 538d575653eee4d4e890db61cd192ae3 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01a - Inline Ground Constraint/Inline Ground Constraint.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3e7c0e4f5986d884498d3431875e36c1 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01a - Inline Ground Constraint/InlineGroundConstraintMain.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | public class InlineGroundConstraintMain : MonoBehaviour 15 | { 16 | public float Gravity = -9.8f; 17 | public float Beta = 0.2f; 18 | 19 | public GameObject Ball; 20 | 21 | private float vy = 0.0f; 22 | 23 | private void FixedUpdate() 24 | { 25 | if (Ball == null) 26 | return; 27 | 28 | float dt = Time.deltaTime; 29 | float y = Ball.transform.position.y; 30 | 31 | vy += Gravity * dt; 32 | 33 | if (y <= 0.0f) 34 | { 35 | vy = -(Beta / dt) * y; 36 | } 37 | 38 | y += vy * dt; 39 | 40 | Vector3 pos = Ball.transform.position; 41 | pos.y = y; 42 | Ball.transform.position = pos; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01a - Inline Ground Constraint/InlineGroundConstraintMain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 699e055db013af24fafd64827caa18e8 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01b - Inline Point Constraint.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8ad485cfc83eb254bba50e39bff75896 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01b - Inline Point Constraint/Inline Point Constraint.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3d9c94a9a0b5e294bac5072e59db3c2a 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01b - Inline Point Constraint/InlinePointConstraintMain.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | public class InlinePointConstraintMain : MonoBehaviour 15 | { 16 | public float Beta = 0.02f; 17 | 18 | public GameObject Object; 19 | public GameObject Target; 20 | 21 | private Vector3 v = Vector3.zero; 22 | 23 | private void Update() 24 | { 25 | if (Object == null) 26 | return; 27 | 28 | if (Target == null) 29 | return; 30 | 31 | float dt = Time.deltaTime; 32 | Vector3 c = Object.transform.position - Target.transform.position; 33 | 34 | v += (-Beta / dt) * c; 35 | v *= 0.9f; // temp magic cheat 36 | Object.transform.position += v * dt; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01b - Inline Point Constraint/InlinePointConstraintMain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 068ebeb9e12d44d4a81afedfce43ccf0 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01c - Inline Point Constraint with Rotation.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 197c2c7d59b4535469abc3369d61f8fd 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01c - Inline Point Constraint with Rotation/Inline Point Constraint with Rotation.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 42e2aec91f431004a8bb8aae62e00e99 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01c - Inline Point Constraint with Rotation/InlinePointConstraintWithRotationMain.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | using PhysicsConstraints; 15 | using CjLib; 16 | 17 | public class InlinePointConstraintWithRotationMain : MonoBehaviour 18 | { 19 | public float Beta = 0.02f; 20 | 21 | public GameObject Object; 22 | public GameObject Target; 23 | 24 | public Vector3 Gravity = new Vector3(0.0f, -20.0f, 0.0f); 25 | 26 | private float mass; 27 | private float massInv; 28 | private Matrix3x3 inertiaLs; 29 | private Matrix3x3 inertiaInvLs; 30 | 31 | private Vector3 rLocal = Vector3.zero; // corner offset 32 | private Vector3 v = Vector3.zero; // linear velocity 33 | private Vector3 a = Vector3.zero; // angular velocity 34 | 35 | private void Start() 36 | { 37 | mass = 1.0f; 38 | massInv = 1.0f / mass; 39 | 40 | inertiaLs = Matrix3x3.Identity; // Inertia.SolidBox(mass, 1.0f * Vector3.one); 41 | inertiaInvLs = inertiaLs.Inverted; 42 | 43 | rLocal = 0.5f * Vector3.one; 44 | } 45 | 46 | private void FixedUpdate() 47 | { 48 | if (Object == null) 49 | return; 50 | 51 | if (Target == null) 52 | return; 53 | 54 | float dt = Time.fixedDeltaTime; 55 | 56 | Vector3 r = Object.transform.rotation * rLocal; 57 | 58 | var t = Object.transform; 59 | Matrix3x3 world2Local = 60 | Matrix3x3.FromRows 61 | ( 62 | t.TransformVector(new Vector3(1.0f, 0.0f, 0.0f)), 63 | t.TransformVector(new Vector3(0.0f, 1.0f, 0.0f)), 64 | t.TransformVector(new Vector3(0.0f, 0.0f, 1.0f)) 65 | ); 66 | Matrix3x3 inertiaInvWs = world2Local.Transposed * inertiaInvLs * world2Local; 67 | 68 | // gravity 69 | v += Gravity * dt; 70 | 71 | // constraint errors 72 | Vector3 cPos = (Object.transform.position + r) - Target.transform.position; 73 | Vector3 cVel = v + Vector3.Cross(a, r); 74 | 75 | // constraint resolution 76 | Matrix3x3 s = Matrix3x3.Skew(-r); 77 | Matrix3x3 k = massInv * Matrix3x3.Identity + s * inertiaInvWs * s.Transposed; 78 | Matrix3x3 effectiveMass = k.Inverted; 79 | Vector3 lambda = effectiveMass * (-(cVel + (Beta / dt) * cPos)); 80 | 81 | // velocity correction 82 | v += massInv * lambda; 83 | a += (inertiaInvWs * s.Transposed) * lambda; 84 | v *= 0.98f; // temp magic 85 | a *= 0.98f; // temp magic 86 | 87 | // integration 88 | Object.transform.position += v * dt; 89 | Object.transform.rotation = QuaternionUtil.Integrate(Object.transform.rotation, a, dt); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/01c - Inline Point Constraint with Rotation/InlinePointConstraintWithRotationMain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6eed0981d1360d7408d409138fc2ba46 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02a - Systemtic Ground Constraint.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 081caf031c9c26843943a11371724ee1 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02a - Systemtic Ground Constraint/Systemic Ground Constraint.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4c7d52b93e234f34593b33fb0c653a1a 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02a - Systemtic Ground Constraint/SystemicGroundConstraintMain.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | using PhysicsConstraints; 15 | 16 | public class SystemicGroundConstraintMain : MonoBehaviour 17 | { 18 | private void Start() 19 | { 20 | World.Gravity = 9.8f * Vector3.down; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02a - Systemtic Ground Constraint/SystemicGroundConstraintMain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1f122fa1513a23443ad8a7bc7bcbe097 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02b - Systemtic Point Constraint.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fc43d35afff215340979e514622f837d 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02b - Systemtic Point Constraint/Systemic Point Constraint.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 00ef9e128f6b2194392a83144c4f8240 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02c - Systemic Point Constraint with Rotation.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ff661c2a281d00a4e912407683b9f3fa 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02c - Systemic Point Constraint with Rotation/Systemic Point Constraint with Rotation.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6ad5013788175d14494a9aadfc92eef5 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02c - Systemic Point Constraint with Rotation/SystemicPointConstraintWithRotationMain.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | using PhysicsConstraints; 15 | 16 | public class SystemicPointConstraintWithRotationMain : MonoBehaviour 17 | { 18 | private void Start() 19 | { 20 | World.Gravity = 20.0f * Vector3.down; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/02c - Systemic Point Constraint with Rotation/SystemicPointConstraintWithRotationMain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 51658f7f11aa02b4e859b42d0fba6048 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/03a - Contact Constraint.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 071434b0e56174f4f8b9dc9d948fca3f 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/03a - Contact Constraint/Contact Constraint.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c8e605d78eb5f724a9bb566a8c2cc3cb 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 56e5d60c04364bc47915f9905a2151e3 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Blue.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_CorrespondingSourceObject: {fileID: 0} 8 | m_PrefabInstance: {fileID: 0} 9 | m_PrefabAsset: {fileID: 0} 10 | m_Name: Blue 11 | m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} 12 | m_ShaderKeywords: _EMISSION 13 | m_LightmapFlags: 2 14 | m_EnableInstancingVariants: 0 15 | m_DoubleSidedGI: 0 16 | m_CustomRenderQueue: -1 17 | stringTagMap: {} 18 | disabledShaderPasses: [] 19 | m_SavedProperties: 20 | serializedVersion: 3 21 | m_TexEnvs: 22 | - _BumpMap: 23 | m_Texture: {fileID: 0} 24 | m_Scale: {x: 1, y: 1} 25 | m_Offset: {x: 0, y: 0} 26 | - _DetailAlbedoMap: 27 | m_Texture: {fileID: 0} 28 | m_Scale: {x: 1, y: 1} 29 | m_Offset: {x: 0, y: 0} 30 | - _DetailMask: 31 | m_Texture: {fileID: 0} 32 | m_Scale: {x: 1, y: 1} 33 | m_Offset: {x: 0, y: 0} 34 | - _DetailNormalMap: 35 | m_Texture: {fileID: 0} 36 | m_Scale: {x: 1, y: 1} 37 | m_Offset: {x: 0, y: 0} 38 | - _EmissionMap: 39 | m_Texture: {fileID: 0} 40 | m_Scale: {x: 1, y: 1} 41 | m_Offset: {x: 0, y: 0} 42 | - _MainTex: 43 | m_Texture: {fileID: 0} 44 | m_Scale: {x: 1, y: 1} 45 | m_Offset: {x: 0, y: 0} 46 | - _MetallicGlossMap: 47 | m_Texture: {fileID: 0} 48 | m_Scale: {x: 1, y: 1} 49 | m_Offset: {x: 0, y: 0} 50 | - _OcclusionMap: 51 | m_Texture: {fileID: 0} 52 | m_Scale: {x: 1, y: 1} 53 | m_Offset: {x: 0, y: 0} 54 | - _ParallaxMap: 55 | m_Texture: {fileID: 0} 56 | m_Scale: {x: 1, y: 1} 57 | m_Offset: {x: 0, y: 0} 58 | m_Floats: 59 | - _BumpScale: 1 60 | - _Cutoff: 0.5 61 | - _DetailNormalMapScale: 1 62 | - _DstBlend: 0 63 | - _GlossMapScale: 1 64 | - _Glossiness: 0 65 | - _GlossyReflections: 1 66 | - _Metallic: 0 67 | - _Mode: 0 68 | - _OcclusionStrength: 1 69 | - _Parallax: 0.02 70 | - _SmoothnessTextureChannel: 0 71 | - _SpecularHighlights: 1 72 | - _SrcBlend: 1 73 | - _UVSec: 0 74 | - _ZWrite: 1 75 | m_Colors: 76 | - _Color: {r: 0, g: 0.5216656, b: 1, a: 1} 77 | - _EmissionColor: {r: 0, g: 0.06215309, b: 0.26415092, a: 1} 78 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Blue.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 855a078f0deb1bb41a13a45b9678892e 3 | NativeFormatImporter: 4 | externalObjects: {} 5 | mainObjectFileID: 0 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Checker.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_CorrespondingSourceObject: {fileID: 0} 8 | m_PrefabInstance: {fileID: 0} 9 | m_PrefabAsset: {fileID: 0} 10 | m_Name: Checker 11 | m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} 12 | m_ShaderKeywords: _EMISSION 13 | m_LightmapFlags: 2 14 | m_EnableInstancingVariants: 0 15 | m_DoubleSidedGI: 0 16 | m_CustomRenderQueue: -1 17 | stringTagMap: {} 18 | disabledShaderPasses: [] 19 | m_SavedProperties: 20 | serializedVersion: 3 21 | m_TexEnvs: 22 | - _BumpMap: 23 | m_Texture: {fileID: 0} 24 | m_Scale: {x: 1, y: 1} 25 | m_Offset: {x: 0, y: 0} 26 | - _DetailAlbedoMap: 27 | m_Texture: {fileID: 0} 28 | m_Scale: {x: 0, y: 0} 29 | m_Offset: {x: 0, y: 0} 30 | - _DetailMask: 31 | m_Texture: {fileID: 0} 32 | m_Scale: {x: 1, y: 1} 33 | m_Offset: {x: 0, y: 0} 34 | - _DetailNormalMap: 35 | m_Texture: {fileID: 0} 36 | m_Scale: {x: 1, y: 1} 37 | m_Offset: {x: 0, y: 0} 38 | - _EmissionMap: 39 | m_Texture: {fileID: 10309, guid: 0000000000000000f000000000000000, type: 0} 40 | m_Scale: {x: 2, y: 2} 41 | m_Offset: {x: 0, y: 0} 42 | - _MainTex: 43 | m_Texture: {fileID: 10309, guid: 0000000000000000f000000000000000, type: 0} 44 | m_Scale: {x: 2, y: 2} 45 | m_Offset: {x: 0, y: 0} 46 | - _MetallicGlossMap: 47 | m_Texture: {fileID: 0} 48 | m_Scale: {x: 1, y: 1} 49 | m_Offset: {x: 0, y: 0} 50 | - _OcclusionMap: 51 | m_Texture: {fileID: 0} 52 | m_Scale: {x: 1, y: 1} 53 | m_Offset: {x: 0, y: 0} 54 | - _ParallaxMap: 55 | m_Texture: {fileID: 0} 56 | m_Scale: {x: 1, y: 1} 57 | m_Offset: {x: 0, y: 0} 58 | m_Floats: 59 | - _BumpScale: 1 60 | - _Cutoff: 0.5 61 | - _DetailNormalMapScale: 1 62 | - _DstBlend: 0 63 | - _GlossMapScale: 1 64 | - _Glossiness: 0.5 65 | - _GlossyReflections: 1 66 | - _Metallic: 0 67 | - _Mode: 0 68 | - _OcclusionStrength: 1 69 | - _Parallax: 0.02 70 | - _SmoothnessTextureChannel: 0 71 | - _SpecularHighlights: 1 72 | - _SrcBlend: 1 73 | - _UVSec: 0 74 | - _ZWrite: 1 75 | m_Colors: 76 | - _Color: {r: 1, g: 1, b: 1, a: 1} 77 | - _EmissionColor: {r: 0.13207549, g: 0.13207549, b: 0.13207549, a: 1} 78 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Checker.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 02f2842ed350eac469f7be2a4f06fb08 3 | NativeFormatImporter: 4 | externalObjects: {} 5 | mainObjectFileID: 0 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Orange.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_CorrespondingSourceObject: {fileID: 0} 8 | m_PrefabInstance: {fileID: 0} 9 | m_PrefabAsset: {fileID: 0} 10 | m_Name: Orange 11 | m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} 12 | m_ShaderKeywords: _EMISSION 13 | m_LightmapFlags: 2 14 | m_EnableInstancingVariants: 0 15 | m_DoubleSidedGI: 0 16 | m_CustomRenderQueue: -1 17 | stringTagMap: {} 18 | disabledShaderPasses: [] 19 | m_SavedProperties: 20 | serializedVersion: 3 21 | m_TexEnvs: 22 | - _BumpMap: 23 | m_Texture: {fileID: 0} 24 | m_Scale: {x: 1, y: 1} 25 | m_Offset: {x: 0, y: 0} 26 | - _DetailAlbedoMap: 27 | m_Texture: {fileID: 0} 28 | m_Scale: {x: 1, y: 1} 29 | m_Offset: {x: 0, y: 0} 30 | - _DetailMask: 31 | m_Texture: {fileID: 0} 32 | m_Scale: {x: 1, y: 1} 33 | m_Offset: {x: 0, y: 0} 34 | - _DetailNormalMap: 35 | m_Texture: {fileID: 0} 36 | m_Scale: {x: 1, y: 1} 37 | m_Offset: {x: 0, y: 0} 38 | - _EmissionMap: 39 | m_Texture: {fileID: 0} 40 | m_Scale: {x: 1, y: 1} 41 | m_Offset: {x: 0, y: 0} 42 | - _MainTex: 43 | m_Texture: {fileID: 0} 44 | m_Scale: {x: 1, y: 1} 45 | m_Offset: {x: 0, y: 0} 46 | - _MetallicGlossMap: 47 | m_Texture: {fileID: 0} 48 | m_Scale: {x: 1, y: 1} 49 | m_Offset: {x: 0, y: 0} 50 | - _OcclusionMap: 51 | m_Texture: {fileID: 0} 52 | m_Scale: {x: 1, y: 1} 53 | m_Offset: {x: 0, y: 0} 54 | - _ParallaxMap: 55 | m_Texture: {fileID: 0} 56 | m_Scale: {x: 1, y: 1} 57 | m_Offset: {x: 0, y: 0} 58 | m_Floats: 59 | - _BumpScale: 1 60 | - _Cutoff: 0.5 61 | - _DetailNormalMapScale: 1 62 | - _DstBlend: 0 63 | - _GlossMapScale: 1 64 | - _Glossiness: 0.5 65 | - _GlossyReflections: 1 66 | - _Metallic: 0 67 | - _Mode: 0 68 | - _OcclusionStrength: 1 69 | - _Parallax: 0.02 70 | - _SmoothnessTextureChannel: 0 71 | - _SpecularHighlights: 1 72 | - _SrcBlend: 1 73 | - _UVSec: 0 74 | - _ZWrite: 1 75 | m_Colors: 76 | - _Color: {r: 1, g: 0.7263845, b: 0, a: 1} 77 | - _EmissionColor: {r: 0.103773594, g: 0.05046246, b: 0, a: 1} 78 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Examples/Common/Orange.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cfbdfbc4dbd8f4f49893d20851db2c57 3 | NativeFormatImporter: 4 | externalObjects: {} 5 | mainObjectFileID: 0 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 794f5eee0fd9d8d4c8ec1fbf6ac660b3 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 87c41feb624c6db49a5fae0d53ec7571 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/Collision.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | using CjLib; 15 | 16 | namespace PhysicsConstraints 17 | { 18 | public class Collision 19 | { 20 | public static bool DetectCollision(PhysicsCollider colliderA, PhysicsCollider colliderB, ref Contact contact) 21 | { 22 | var bodyA = colliderA.GetComponent(); 23 | var bodyB = colliderB.GetComponent(); 24 | var transformA = colliderA.transform; 25 | var transformB = colliderB.transform; 26 | switch (colliderA.Type) 27 | { 28 | case PhysicsCollider.ShapeType.Sphere: 29 | var sphereA = (SphereCollider)colliderA; 30 | switch (colliderB.Type) 31 | { 32 | case PhysicsCollider.ShapeType.Sphere: 33 | var sphereB = (SphereCollider)colliderB; 34 | return SphereVsSphere(bodyA, transformA.position, sphereA.radius, bodyB, transformB.position, sphereB.radius, ref contact); 35 | 36 | case PhysicsCollider.ShapeType.Plane: 37 | var planeB = (PlaneCollider)colliderB; 38 | return SphereVsPlane(bodyA, transformA.position, sphereA.radius, bodyB, transformB.position, planeB.Normal, ref contact); 39 | } 40 | break; 41 | 42 | case PhysicsCollider.ShapeType.Plane: 43 | var planeA = (PlaneCollider)colliderA; 44 | switch (colliderB.Type) 45 | { 46 | case PhysicsCollider.ShapeType.Sphere: 47 | var sphereB = (SphereCollider)colliderB; 48 | return SphereVsPlane(bodyB, transformB.position, sphereB.radius, bodyA, transformA.position, planeA.Normal, ref contact); 49 | 50 | case PhysicsCollider.ShapeType.Plane: 51 | // no plane-plane collision detection 52 | return false; 53 | } 54 | break; 55 | } 56 | 57 | return false; 58 | } 59 | 60 | public static bool SphereVsSphere 61 | ( 62 | PhysicsBody bodyA, 63 | Vector3 centerA, 64 | float radiusA, 65 | PhysicsBody bodyB, 66 | Vector3 centerB, 67 | float radiusB, 68 | ref Contact contactOut 69 | ) 70 | { 71 | Vector3 vec = centerB - centerA; 72 | float distSqr = vec.sqrMagnitude; 73 | float radiusSum = radiusA + radiusB; 74 | float radiusSumSqr = radiusSum * radiusSum; 75 | 76 | if (distSqr > radiusSumSqr) 77 | return false; 78 | 79 | Vector3 dir = VectorUtil.NormalizeSafe(vec, Vector3.zero); 80 | contactOut = Pool.Get(); 81 | contactOut.BodyA = bodyA; 82 | contactOut.BodyB = bodyB; 83 | contactOut.PositionA = centerA + radiusA * dir; 84 | contactOut.PositionB = centerB - radiusB * dir; 85 | contactOut.Normal = dir; 86 | contactOut.Penetration = radiusSum - Mathf.Sqrt(distSqr); 87 | 88 | return true; 89 | } 90 | 91 | public static bool SphereVsPlane 92 | ( 93 | PhysicsBody bodyA, 94 | Vector3 centerA, 95 | float radiusA, 96 | PhysicsBody bodyB, 97 | Vector3 pointB, 98 | Vector3 normalB, 99 | ref Contact contactOut 100 | ) 101 | { 102 | Vector3 r = centerA - pointB; 103 | float d = Vector3.Dot(r, normalB); 104 | if (d > radiusA) 105 | return false; 106 | 107 | contactOut = Pool.Get(); 108 | contactOut.BodyA = bodyA; 109 | contactOut.BodyB = bodyB; 110 | contactOut.PositionA = centerA - radiusA * normalB; 111 | contactOut.PositionB = centerA - d * normalB; 112 | contactOut.Normal = -normalB; 113 | contactOut.Penetration = (radiusA - d); 114 | 115 | return true; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/Collision.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c13ca8255980cd64194c7a04cfe9a18c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/Contact.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using CjLib; 13 | using UnityEngine; 14 | 15 | namespace PhysicsConstraints 16 | { 17 | [RequireComponent(typeof(PhysicsBody))] 18 | public class Contact : IPhysicsConstraint 19 | { 20 | public PhysicsBody BodyA; 21 | public PhysicsBody BodyB; 22 | public Vector3 PositionA; 23 | public Vector3 PositionB; 24 | public Vector3 Normal; 25 | public float Penetration; 26 | 27 | private Vector3 m_rA; 28 | private Vector3 m_rB; 29 | 30 | private Jacobian m_jN; // Jacobian for contact normal (collision resolution) 31 | private Jacobian m_jT; // Jacobian for contact tangent (friction) 32 | private Jacobian m_jB; // Jacobian for contact bi-tangent (friction) 33 | 34 | public Contact() 35 | { 36 | m_jN = new Jacobian(Jacobian.Type.Normal); 37 | m_jT = new Jacobian(Jacobian.Type.Tangent); 38 | m_jB = new Jacobian(Jacobian.Type.Tangent); 39 | } 40 | 41 | public void InitVelocityConstraint(float dt) 42 | { 43 | m_rA = PositionA - BodyA.CenterOfMassWs; 44 | m_rB = PositionB - BodyB.CenterOfMassWs; 45 | 46 | Vector3 tangent; 47 | Vector3 bitangent; 48 | VectorUtil.FormOrthogonalBasis(Normal, out tangent, out bitangent); 49 | 50 | m_jN.Init(this, Normal, dt); 51 | m_jT.Init(this, tangent, dt); 52 | m_jB.Init(this, bitangent, dt); 53 | } 54 | 55 | public void SolveVelocityConstraint(float dt) 56 | { 57 | m_jN.Resolve(this, dt); 58 | m_jT.Resolve(this, dt); 59 | m_jB.Resolve(this, dt); 60 | } 61 | 62 | // Jacobian for eliminating relative velocity along a specific direction 63 | // (normal, tangent, or bi-tangent) 64 | private struct Jacobian 65 | { 66 | public enum Type 67 | { 68 | Normal, 69 | Tangent, 70 | } 71 | 72 | Type m_type; 73 | 74 | private Vector3 m_va; // Jacobian components for linear velocity of body A 75 | private Vector3 m_wa; // Jacobian components for angular velocity of body A 76 | private Vector3 m_vb; // Jacobian components for linear velocity of body B 77 | private Vector3 m_wb; // Jacobian components for angular velocity of body B 78 | private float m_bias; 79 | private float m_effectiveMass; 80 | private float m_totalLambda; 81 | 82 | public Jacobian(Type type) 83 | { 84 | m_type = type; 85 | m_va = Vector3.zero; 86 | m_wa = Vector3.zero; 87 | m_vb = Vector3.zero; 88 | m_wb = Vector3.zero; 89 | m_bias = 0.0f; 90 | m_effectiveMass = 0.0f; 91 | m_totalLambda = 0.0f; 92 | } 93 | 94 | public void Init(Contact contact, Vector3 dir, float dt) 95 | { 96 | m_va = -dir; 97 | m_wa = -Vector3.Cross(contact.m_rA, dir); 98 | m_vb = dir; 99 | m_wb = Vector3.Cross(contact.m_rB, dir); 100 | 101 | m_bias = 0.0f; 102 | if (m_type == Type.Normal) 103 | { 104 | float beta = contact.BodyA.ContactBeta * contact.BodyB.ContactBeta; 105 | float restitution = contact.BodyA.Restitution * contact.BodyB.Restitution; 106 | Vector3 relativeVelocity = 107 | -contact.BodyA.LinearVelocity 108 | - Vector3.Cross(contact.BodyA.AngularVelocity, contact.m_rA) 109 | + contact.BodyB.LinearVelocity 110 | + Vector3.Cross(contact.BodyB.AngularVelocity, contact.m_rB); 111 | float closingVelocity = Vector3.Dot(relativeVelocity, dir); 112 | m_bias = -(beta / dt) * contact.Penetration + restitution * closingVelocity; 113 | } 114 | 115 | float k = 116 | contact.BodyA.InverseMass 117 | + Vector3.Dot(m_wa, contact.BodyA.InverseInertiaWs * m_wa) 118 | + contact.BodyB.InverseMass 119 | + Vector3.Dot(m_wb, contact.BodyB.InverseInertiaWs * m_wb); 120 | 121 | m_effectiveMass = 1.0f / k; 122 | m_totalLambda = 0.0f; 123 | } 124 | 125 | public void Resolve(Contact contact, float dt) 126 | { 127 | Vector3 dir = m_vb; 128 | 129 | // JV = Jacobian * velocity vector 130 | float jv = 131 | Vector3.Dot(m_va, contact.BodyA.LinearVelocity) 132 | + Vector3.Dot(m_wa, contact.BodyA.AngularVelocity) 133 | + Vector3.Dot(m_vb, contact.BodyB.LinearVelocity) 134 | + Vector3.Dot(m_wb, contact.BodyB.AngularVelocity); 135 | 136 | // raw lambda 137 | float lambda = m_effectiveMass * (-(jv + m_bias)); 138 | 139 | // clamped lambda 140 | // normal / contact resolution : lambda >= 0 141 | // tangent / friction : -maxFriction <= lambda <= maxFriction 142 | float oldTotalLambda = m_totalLambda; 143 | switch (m_type) 144 | { 145 | case Type.Normal: 146 | m_totalLambda = Mathf.Max(0.0f, m_totalLambda + lambda); 147 | break; 148 | 149 | case Type.Tangent: 150 | float friction = contact.BodyA.Friction * contact.BodyB.Friction; 151 | float maxFriction = friction * contact.m_jN.m_totalLambda; 152 | m_totalLambda = Mathf.Clamp(m_totalLambda + lambda, -maxFriction, maxFriction); 153 | break; 154 | } 155 | lambda = m_totalLambda - oldTotalLambda; 156 | 157 | // velocity correction 158 | contact.BodyA.LinearVelocity += contact.BodyA.InverseMass * m_va * lambda; 159 | contact.BodyA.AngularVelocity += contact.BodyA.InverseInertiaWs * m_wa * lambda; 160 | contact.BodyB.LinearVelocity += contact.BodyB.InverseMass * m_vb * lambda; 161 | contact.BodyB.AngularVelocity += contact.BodyB.InverseInertiaWs * m_wb * lambda; 162 | } 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/Contact.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9822aae48bab8bf47bc9f507ac550bb4 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/NSquared.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using System; 13 | using System.Collections.Generic; 14 | 15 | namespace PhysicsConstraints 16 | { 17 | public class NSquared : IBroadphase 18 | { 19 | private ICollection m_colliderPairs; 20 | 21 | // y u no clone IEnumerable?? >:( 22 | private ICollection m_outerLoopBuffer; 23 | 24 | public NSquared() 25 | { 26 | m_colliderPairs = new List(); 27 | m_outerLoopBuffer = new List(); 28 | } 29 | 30 | public ICollection GenerateColliderPairs(ICollection colliders) 31 | { 32 | m_colliderPairs.Clear(); 33 | var itColliderA = colliders.GetEnumerator(); 34 | while (itColliderA.MoveNext()) 35 | { 36 | var colliderA = itColliderA.Current; 37 | foreach (var colliderB in m_outerLoopBuffer) 38 | { 39 | m_colliderPairs.Add(new ColliderPair(colliderA, colliderB)); 40 | } 41 | m_outerLoopBuffer.Add(colliderA); 42 | } 43 | m_outerLoopBuffer.Clear(); 44 | 45 | return m_colliderPairs; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/NSquared.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d76d3dbba5f278046852103d1130c0d6 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/PhysicsCollider.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | [RequireComponent(typeof(PhysicsBody))] 17 | public abstract class PhysicsCollider : MonoBehaviour 18 | { 19 | public enum ShapeType 20 | { 21 | Sphere, 22 | Plane, 23 | } 24 | 25 | private ShapeType m_type; 26 | public ShapeType Type { get { return m_type; } } 27 | 28 | protected PhysicsCollider(ShapeType type) 29 | { 30 | m_type = type; 31 | } 32 | 33 | private void OnEnable() 34 | { 35 | World.Register(this); 36 | } 37 | 38 | private void OnDisable() 39 | { 40 | World.Unregister(this); 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/PhysicsCollider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae9950ef6921d9b47b095d07d01d402a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/PlaneCollider.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public class PlaneCollider : PhysicsCollider 17 | { 18 | public Vector3 LocalNormal = Vector3.up; 19 | public Vector2 Normal { get { return transform.TransformVector(LocalNormal); } } 20 | 21 | public PlaneCollider() 22 | : base(ShapeType.Plane) 23 | { } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/PlaneCollider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b5fb42389a2332459e681465f510099 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/SphereCollider.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public class SphereCollider : PhysicsCollider 17 | { 18 | [Min(0.0f)] 19 | public float radius = 0.5f; 20 | 21 | public SphereCollider() 22 | : base(ShapeType.Sphere) 23 | { } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Collision/SphereCollider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: afe4f72fc99c07d4c8e511271a56683b 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 45b8e354b96ac6546bf4eb67563288b4 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/MouseConstraint.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | [RequireComponent(typeof(PhysicsBody))] 17 | public class MouseConstraint : PointConstraintBase 18 | { 19 | public Transform Anchor; 20 | 21 | protected override Vector3 GetTarget() 22 | { 23 | // TODO 24 | return transform.position; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/MouseConstraint.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 40da9a4e4d51db9408df7af85f5e4519 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PlaneConstraint.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | [RequireComponent(typeof(PhysicsBody))] 17 | public class PlaneConstraint : MonoBehaviour, IPhysicsConstraint 18 | { 19 | public ConstraintParams ConstraintParams = new ConstraintParams(); 20 | 21 | public float Restitution = 0.8f; 22 | public float Offset = 0.0f; 23 | 24 | public Transform Plane; 25 | private Vector3 m_n; // plane normal 26 | private Vector3 m_p; // plane point 27 | private float m_d; // plane dot 28 | 29 | private Vector3 m_impulse; 30 | private float m_effectiveMass; 31 | private float sbc; 32 | private Vector3 m_positionErrorBias; 33 | 34 | private void OnEnable() 35 | { 36 | World.Register(this); 37 | } 38 | 39 | private void OnDisable() 40 | { 41 | World.Unregister(this); 42 | } 43 | 44 | public void InitVelocityConstraint(float dt) 45 | { 46 | var body = GetComponent(); 47 | 48 | float pbc; 49 | ConstraintUtil.VelocityConstraintBias(body.Mass, ConstraintParams, dt, out pbc, out sbc); 50 | 51 | m_n = (Plane != null) ? Plane.transform.up : Vector3.up; 52 | m_p = (Plane != null) ? Plane.transform.position : transform.position; 53 | m_d = Vector3.Dot(transform.position - m_p, m_n) - Offset; 54 | 55 | if (m_d > 0.0f) 56 | return; 57 | 58 | Vector3 cPos = m_d * m_n; 59 | m_positionErrorBias = pbc * cPos + Restitution * Vector3.Project(-body.LinearVelocity, m_n); 60 | m_effectiveMass = 1.0f / (body.InverseMass + sbc); 61 | 62 | // TODO: warm starting 63 | m_impulse = Vector3.zero; 64 | } 65 | 66 | public void SolveVelocityConstraint(float dt) 67 | { 68 | if (m_d > 0.0f) 69 | return; 70 | 71 | var body = GetComponent(); 72 | 73 | Vector3 cVel = Vector3.Project(body.LinearVelocity, m_n) + m_positionErrorBias + sbc * m_impulse; 74 | 75 | Vector3 impulse = m_effectiveMass * (-cVel); 76 | // TODO: max impulse 77 | m_impulse += impulse; 78 | 79 | body.LinearVelocity += body.InverseMass * impulse; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PlaneConstraint.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a14ac650e1c9fd4cb17d6826abcea26 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PointConstraint.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | [RequireComponent(typeof(PhysicsBody))] 17 | public class PointConstraint : PointConstraintBase 18 | { 19 | public Transform Target; 20 | public Vector3 LocalAnchor; 21 | 22 | protected override Vector3 GetTarget() 23 | { 24 | return 25 | (Target != null) 26 | ? Target.position 27 | : transform.position; 28 | } 29 | 30 | protected override Vector3 GetLocalAnchor() 31 | { 32 | return LocalAnchor; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PointConstraint.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7fc335fc82353c143b9dbb253c3fa4e8 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PointConstraintBase.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | [RequireComponent(typeof(PhysicsBody))] 17 | public abstract class PointConstraintBase : MonoBehaviour, IPhysicsConstraint 18 | { 19 | public ConstraintParams ConstraintParams = new ConstraintParams(); 20 | public bool EnableRotation = false; 21 | 22 | protected abstract Vector3 GetTarget(); 23 | protected virtual Vector3 GetLocalAnchor() { return Vector3.zero; } 24 | 25 | private Vector3 m_totalLambda; 26 | private Vector3 m_r; 27 | private Matrix3x3 m_effectiveMass; 28 | private Matrix3x3 m_cross; 29 | private float m_sbc; 30 | private Vector3 m_positionErrorBias; 31 | 32 | private void OnEnable() 33 | { 34 | World.Register(this); 35 | } 36 | 37 | private void OnDisable() 38 | { 39 | World.Unregister(this); 40 | } 41 | 42 | public void InitVelocityConstraint(float dt) 43 | { 44 | var body = GetComponent(); 45 | 46 | float pbc; 47 | ConstraintUtil.VelocityConstraintBias(body.Mass, ConstraintParams, dt, out pbc, out m_sbc); 48 | 49 | m_r = transform.rotation * (GetLocalAnchor() - body.CenterOfMassLs); 50 | 51 | Vector3 cPos = (transform.position + m_r) - GetTarget(); 52 | Vector3 cVel = body.LinearVelocity + Vector3.Cross(body.AngularVelocity, m_r); 53 | 54 | m_cross = Matrix3x3.Skew(-m_r); 55 | Matrix3x3 k = body.InverseMass * Matrix3x3.Identity; 56 | if (EnableRotation) 57 | k += m_cross * body.InverseInertiaWs * m_cross.Transposed; 58 | 59 | k += m_sbc * Matrix3x3.Identity; 60 | 61 | m_positionErrorBias = pbc * cPos; 62 | m_effectiveMass = k.Inverted; 63 | 64 | // TODO: warm starting 65 | m_totalLambda = Vector3.zero; 66 | } 67 | 68 | public void SolveVelocityConstraint(float dt) 69 | { 70 | var body = GetComponent(); 71 | 72 | Vector3 cVel = body.LinearVelocity + Vector3.Cross(body.AngularVelocity, m_r); 73 | Vector3 jvb = cVel + m_positionErrorBias + m_sbc * m_totalLambda; 74 | Vector3 lambda = m_effectiveMass * (-jvb); 75 | m_totalLambda += lambda; 76 | 77 | body.LinearVelocity += body.InverseMass * lambda; 78 | 79 | if (EnableRotation) 80 | body.AngularVelocity += body.InverseInertiaWs * m_cross.Transposed * lambda; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Constraints/PointConstraintBase.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7fa1756a0f64fc24cb342804429bc08d 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1fb93be9e4874a44088cc7f18e8ca357 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/ConstraintUtil.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public class ConstraintUtil 17 | { 18 | // h: delta time 19 | // d: damping coefficient 20 | // k: spring constant 21 | // omega: angular velocity 22 | // zeta: damping ratio 23 | // gamma: softness (force feedback factor) 24 | // beta: Baumgarte (positional error feedback factor) 25 | 26 | internal static readonly float Epsilon = 1.0e-10f; 27 | internal static readonly float TwoPi = 2.0f * Mathf.PI; 28 | 29 | private static float DampingCoefficient(float mass, float frequencyHz, float dampingRatio) 30 | { 31 | return 2.0f * mass * dampingRatio * (frequencyHz * TwoPi); 32 | } 33 | 34 | private static float SpringConstant(float mass, float frequencyHz) 35 | { 36 | float angularFrequency = frequencyHz * TwoPi; 37 | return mass * angularFrequency * angularFrequency; 38 | } 39 | 40 | private static void VelocityConstraintBias 41 | ( 42 | float dampingCoefficient, 43 | float springConstant, 44 | float dt, 45 | out float positionBiasCoefficient, 46 | out float softnessBiasCoefficient 47 | ) 48 | { 49 | float hk = dt * springConstant; 50 | float gamma = dampingCoefficient + hk; 51 | if (gamma > 0.0f) 52 | gamma = 1.0f / gamma; 53 | 54 | float dtInv = 1.0f / dt; 55 | float beta = hk * gamma; 56 | 57 | positionBiasCoefficient = beta * dtInv; 58 | softnessBiasCoefficient = gamma * dtInv; 59 | } 60 | 61 | public static void VelocityConstraintBias 62 | ( 63 | float mass, 64 | ConstraintParams p, 65 | float dt, 66 | out float positionBiasCoefficient, 67 | out float softnessBiasCoefficient 68 | ) 69 | { 70 | if (p.Mode == ConstraintParams.ParameterMode.Hard) 71 | { 72 | positionBiasCoefficient = 1.0f / dt; 73 | softnessBiasCoefficient = 0.0f; 74 | return; 75 | } 76 | 77 | float dampingCoefficient; 78 | float springConstant; 79 | p.GenerateVelocityConstraintParams(mass, out dampingCoefficient, out springConstant); 80 | VelocityConstraintBias 81 | ( 82 | dampingCoefficient, 83 | springConstant, 84 | dt, 85 | out positionBiasCoefficient, 86 | out softnessBiasCoefficient 87 | ); 88 | } 89 | 90 | public static void VelocityConstraintBias 91 | ( 92 | float mass, 93 | float frequencyHz, 94 | float dampingRatio, 95 | float dt, 96 | out float positionBiasCoefficient, 97 | out float softnessBiasCoefficient 98 | ) 99 | { 100 | VelocityConstraintBias 101 | ( 102 | DampingCoefficient(mass, frequencyHz, dampingRatio), 103 | SpringConstant(mass, frequencyHz), 104 | dt, 105 | out positionBiasCoefficient, 106 | out softnessBiasCoefficient 107 | ); 108 | } 109 | } 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/ConstraintUtil.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3c418ada3a3bcdf408238257c515d7c7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/IBroadphase.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using System.Collections.Generic; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public struct ColliderPair 17 | { 18 | public PhysicsCollider ColliderA; 19 | public PhysicsCollider ColliderB; 20 | 21 | public ColliderPair(PhysicsCollider colliderA, PhysicsCollider colliderB) 22 | { 23 | ColliderA = colliderA; 24 | ColliderB = colliderB; 25 | } 26 | } 27 | 28 | public interface IBroadphase 29 | { 30 | ICollection GenerateColliderPairs(ICollection colliders); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/IBroadphase.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2bf50d30c0451bb4190f2ccf93aebbe6 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/IPhysicsConstraint.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | namespace PhysicsConstraints 13 | { 14 | public interface IPhysicsConstraint 15 | { 16 | void InitVelocityConstraint(float dt); 17 | void SolveVelocityConstraint(float dt); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/IPhysicsConstraint.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3dd3f7c4ee15ac34e9daa84dad08b0ed 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/Inertia.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public struct Inertia 17 | { 18 | // https://en.wikipedia.org/wiki/List_of_moments_of_inertia 19 | 20 | public static Matrix3x3 SolidSphere(float mass, float radius) 21 | { 22 | float i = (2.0f / 5.0f) * mass * radius * radius; 23 | return 24 | new Matrix3x3 25 | ( 26 | i, 0.0f, 0.0f, 27 | 0.0f, i, 0.0f, 28 | 0.0f, 0.0f, i 29 | ); 30 | } 31 | 32 | public static Matrix3x3 SolidBox(float mass, Vector3 dimensions) 33 | { 34 | float oneTwelfth = 1.0f / 12.0f; 35 | float xx = dimensions.x * dimensions.x; 36 | float yy = dimensions.y * dimensions.y; 37 | float zz = dimensions.z * dimensions.z; 38 | return 39 | new Matrix3x3 40 | ( 41 | oneTwelfth * mass * (yy + zz), 0.0f, 0.0f, 42 | 0.0f, oneTwelfth * mass * (xx + zz), 0.0f, 43 | 0.0f, 0.0f, oneTwelfth * mass * (xx + yy) 44 | ); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/Inertia.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5c2cc956775083948a0591c35a0cdcdd 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/Matrix3x3.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using UnityEngine; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public struct Matrix3x3 17 | { 18 | public float I00; 19 | public float I01; 20 | public float I02; 21 | public float I10; 22 | public float I11; 23 | public float I12; 24 | public float I20; 25 | public float I21; 26 | public float I22; 27 | 28 | public Vector3 Row0 29 | { 30 | get { return new Vector3(I00, I01, I02); } 31 | set { I00 = value.x; I01 = value.y; I02 = value.z; } 32 | } 33 | public Vector3 Row1 34 | { 35 | get { return new Vector3(I10, I11, I12); } 36 | set { I10 = value.x; I11 = value.y; I12 = value.z; } 37 | } 38 | public Vector3 Row2 39 | { 40 | get { return new Vector3(I20, I21, I22); } 41 | set { I20 = value.x; I21 = value.y; I22 = value.z; } 42 | } 43 | public Vector3 Col0 44 | { 45 | get { return new Vector3(I00, I10, I20); } 46 | set { I00 = value.x; I10 = value.y; I20 = value.z; } 47 | } 48 | public Vector3 Col1 49 | { 50 | get { return new Vector3(I01, I11, I21); } 51 | set { I01 = value.x; I11 = value.y; I21 = value.z; } 52 | } 53 | public Vector3 Col2 54 | { 55 | get { return new Vector3(I02, I12, I22); } 56 | set { I02 = value.x; I12 = value.y; I22 = value.z; } 57 | } 58 | 59 | public static Matrix3x3 FromRows(Vector3 row0, Vector3 row1, Vector3 row2) 60 | { 61 | return 62 | new Matrix3x3 63 | ( 64 | row0.x, row0.y, row0.z, 65 | row1.x, row1.y, row1.z, 66 | row2.x, row2.y, row2.z 67 | ); 68 | } 69 | 70 | public static Matrix3x3 FromCols(Vector3 col0, Vector3 col1, Vector3 col2) 71 | { 72 | return 73 | new Matrix3x3 74 | ( 75 | col0.x, col1.x, col2.x, 76 | col0.y, col1.y, col2.y, 77 | col0.z, col1.z, col2.z 78 | ); 79 | } 80 | 81 | public Matrix3x3(float i00, float i01, float i02, float i10, float i11, float i12, float i20, float i21, float i22) 82 | { 83 | I00 = i00; I01 = i01; I02 = i02; 84 | I10 = i10; I11 = i11; I12 = i12; 85 | I20 = i20; I21 = i21; I22 = i22; 86 | } 87 | 88 | public Matrix3x3 Inverted { get { return Inverse(this); } } 89 | public Matrix3x3 Transposed { get { return Transpose(this); } } 90 | 91 | private static readonly Matrix3x3 kIdentity = 92 | new Matrix3x3 93 | ( 94 | 1.0f, 0.0f, 0.0f, 95 | 0.0f, 1.0f, 0.0f, 96 | 0.0f, 0.0f, 1.0f 97 | ); 98 | public static Matrix3x3 Identity { get { return kIdentity; } } 99 | 100 | // Cross(A, B) = Skew(A) B = Skew(-B) A = Cross(-B, A) 101 | public static Matrix3x3 Skew(Vector3 v) 102 | { 103 | return 104 | new Matrix3x3 105 | ( 106 | 0.0f, -v.z, v.y, 107 | v.z, 0.0f, -v.x, 108 | -v.y, v.x, 0.0f 109 | ); 110 | } 111 | 112 | public static Matrix3x3 operator +(Matrix3x3 a, Matrix3x3 b) 113 | { 114 | return FromRows(a.Row0 + b.Row0, a.Row1 + b.Row1, a.Row2 + b.Row2); 115 | } 116 | 117 | public static Matrix3x3 operator -(Matrix3x3 a, Matrix3x3 b) 118 | { 119 | return FromRows(a.Row0 - b.Row0, a.Row1 - b.Row1, a.Row2 - b.Row2); 120 | } 121 | 122 | public static Matrix3x3 operator *(float s, Matrix3x3 m) 123 | { 124 | return FromRows(s * m.Row0, s * m.Row1, s * m.Row2); 125 | } 126 | 127 | public static Matrix3x3 operator *(Matrix3x3 m, float s) 128 | { 129 | return s * m; 130 | } 131 | 132 | public static Vector3 operator *(Matrix3x3 m, Vector3 v) 133 | { 134 | return 135 | new Vector3 136 | ( 137 | Vector3.Dot(m.Row0, v), 138 | Vector3.Dot(m.Row1, v), 139 | Vector3.Dot(m.Row2, v) 140 | ); 141 | } 142 | 143 | public static Vector3 operator *(Vector3 v, Matrix3x3 m) 144 | { 145 | return 146 | new Vector3 147 | ( 148 | Vector3.Dot(v, m.Col0), 149 | Vector3.Dot(v, m.Col1), 150 | Vector3.Dot(v, m.Col2) 151 | ); 152 | } 153 | 154 | public static Matrix3x3 operator *(Matrix3x3 a, Matrix3x3 b) 155 | { 156 | return 157 | FromCols 158 | ( 159 | a * b.Col0, 160 | a * b.Col1, 161 | a * b.Col2 162 | ); 163 | } 164 | 165 | public static float Mul(Vector3 a, Matrix3x3 m, Vector3 b) 166 | { 167 | return Vector3.Dot(a * m, b); 168 | } 169 | 170 | public static Matrix3x3 Inverse(Matrix3x3 m) 171 | { 172 | // too lazy to optimize 173 | // help, compiler 174 | float det = 175 | m.I00 * m.I11 * m.I22 176 | + m.I01 * m.I12 * m.I20 177 | + m.I10 * m.I21 * m.I02 178 | - m.I02 * m.I11 * m.I20 179 | - m.I01 * m.I10 * m.I22 180 | - m.I12 * m.I21 * m.I00; 181 | 182 | // I trust that this inertia tensor is well-constructed 183 | float detInv = 1.0f / det; 184 | 185 | return 186 | new Matrix3x3 187 | ( 188 | (m.I11 * m.I22 - m.I21 * m.I12) * detInv, 189 | (m.I12 * m.I20 - m.I10 * m.I22) * detInv, 190 | (m.I10 * m.I21 - m.I20 * m.I11) * detInv, 191 | (m.I02 * m.I21 - m.I01 * m.I22) * detInv, 192 | (m.I00 * m.I22 - m.I02 * m.I20) * detInv, 193 | (m.I20 * m.I01 - m.I00 * m.I21) * detInv, 194 | (m.I01 * m.I12 - m.I02 * m.I11) * detInv, 195 | (m.I10 * m.I02 - m.I00 * m.I12) * detInv, 196 | (m.I00 * m.I11 - m.I10 * m.I01) * detInv 197 | ); 198 | } 199 | 200 | public static Matrix3x3 Transpose(Matrix3x3 m) 201 | { 202 | return 203 | new Matrix3x3 204 | ( 205 | m.I00, m.I10, m.I20, 206 | m.I01, m.I11, m.I21, 207 | m.I02, m.I12, m.I22 208 | ); 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/Matrix3x3.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5ddded47872e84f4fa3744f0a4d328a7 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/PhysicsBody.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using CjLib; 13 | using System.Diagnostics; 14 | using UnityEngine; 15 | 16 | namespace PhysicsConstraints 17 | { 18 | public class PhysicsBody : MonoBehaviour 19 | { 20 | // mass 21 | private float m_mass; 22 | private float m_inverseMass; 23 | public float Mass 24 | { 25 | get { return m_mass; } 26 | set 27 | { 28 | m_mass = value; 29 | m_inverseMass = (m_mass == float.MaxValue) ? 0.0f : 1.0f / m_mass; 30 | } 31 | } 32 | public float InverseMass 33 | { 34 | get { return m_inverseMass; } 35 | set 36 | { 37 | m_inverseMass = value; 38 | m_mass = (m_inverseMass == 0.0f) ? float.MaxValue : 1.0f / m_inverseMass; 39 | } 40 | } 41 | 42 | // moment of inertia 43 | private Matrix3x3 m_inertiaLs; 44 | private Matrix3x3 m_inverseInertiaLs; 45 | private Matrix3x3 m_inverseInertiaWs; 46 | public Matrix3x3 InertiaLs 47 | { 48 | get { return m_inertiaLs; } 49 | set 50 | { 51 | m_inertiaLs = value; 52 | m_inverseInertiaLs = m_inertiaLs.Inverted; 53 | } 54 | } 55 | public Matrix3x3 InverseInertiaLs { get { return m_inverseInertiaLs; } } 56 | public Matrix3x3 InverseInertiaWs { get { return m_inverseInertiaWs; } } 57 | public void UpdateInertiaWs() 58 | { 59 | var t = transform; 60 | var world2Local = 61 | Matrix3x3.FromRows 62 | ( 63 | t.TransformVector(new Vector3(1.0f, 0.0f, 0.0f)), 64 | t.TransformVector(new Vector3(0.0f, 1.0f, 0.0f)), 65 | t.TransformVector(new Vector3(0.0f, 0.0f, 1.0f)) 66 | ); 67 | m_inverseInertiaWs = world2Local.Transposed * m_inverseInertiaLs * world2Local; 68 | } 69 | 70 | // center of mass 71 | [HideInInspector] 72 | public Vector3 m_centerOfMassLs; 73 | [HideInInspector] 74 | public Vector3 CenterOfMassLs 75 | { 76 | get { return m_centerOfMassLs; } 77 | set { m_centerOfMassLs = value; } 78 | } 79 | public Vector3 CenterOfMassWs 80 | { 81 | get { return transform.TransformPoint(m_centerOfMassLs); } 82 | } 83 | 84 | // velocity 85 | public Vector3 LinearVelocity; 86 | public Vector3 AngularVelocity; 87 | [Range(0.0f, 1.0f)] 88 | public float LinearDrag = 0.0f; 89 | [Range(0.0f, 1.0f)] 90 | public float AngularDrag = 0.0f; 91 | 92 | // gravity 93 | [Range(0.0f, 1.0f)] 94 | public float GravityScale = 1.0f; 95 | 96 | // contact 97 | [Range(0.0f, 1.0f)] 98 | public float ContactBeta = 0.5f; 99 | [Range(0.0f, 1.0f)] 100 | public float Restitution = 0.7f; 101 | [Min(0.0f)] 102 | public float Friction = 1.0f; 103 | 104 | // transform 105 | public bool LockPosition = false; 106 | public bool LockRotation = false; 107 | 108 | public PhysicsBody() 109 | { 110 | Mass = 1.0f; 111 | InertiaLs = Matrix3x3.Identity; 112 | CenterOfMassLs = Vector3.zero; 113 | } 114 | 115 | private void OnEnable() 116 | { 117 | World.Register(this); 118 | 119 | UpdateInertiaWs(); 120 | } 121 | 122 | private void OnDisable() 123 | { 124 | World.Unregister(this); 125 | } 126 | 127 | public void ApplyImpulse(Vector3 impulse, Vector3 atWs) 128 | { 129 | if (LockPosition) 130 | { 131 | LinearVelocity = Vector3.zero; 132 | } 133 | else 134 | { 135 | LinearVelocity += InverseMass * impulse; 136 | } 137 | 138 | if (LockRotation) 139 | { 140 | AngularVelocity = Vector3.zero; 141 | } 142 | else 143 | { 144 | Vector3 atLs = transform.InverseTransformPoint(atWs); 145 | Vector3 r = atLs - CenterOfMassLs; 146 | Vector3 angularImpulse = Vector3.Cross(r, impulse); 147 | AngularVelocity += InverseInertiaWs * angularImpulse; 148 | } 149 | } 150 | 151 | public void Integrate(float dt) 152 | { 153 | if (!LockPosition) 154 | { 155 | transform.position = VectorUtil.Integrate(transform.position, LinearVelocity, dt); 156 | } 157 | 158 | if (!LockRotation) 159 | { 160 | transform.rotation = QuaternionUtil.Integrate(transform.rotation, AngularVelocity, dt); 161 | } 162 | } 163 | } 164 | } 165 | 166 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/PhysicsBody.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 08c296fd7da031c43965412236871afc 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/SoftConstraintParams.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using System; 13 | 14 | using UnityEngine; 15 | 16 | using CjLib; 17 | 18 | namespace PhysicsConstraints 19 | { 20 | [Serializable] 21 | public class ConstraintParams 22 | { 23 | public enum ParameterMode 24 | { 25 | Hard, 26 | SoftDampingRatio, 27 | SoftHalfLife, 28 | SoftExponential, 29 | } 30 | 31 | public ParameterMode Mode = ParameterMode.SoftDampingRatio; 32 | 33 | [ConditionalField("Mode", ParameterMode.SoftDampingRatio, ParameterMode.SoftHalfLife, Min = 0.0001f, Max = 10.0f)] 34 | public float FrequencyHz = 5.0f; 35 | [ConditionalField("Mode", ParameterMode.SoftDampingRatio, Min = 0.0f, Max = 10.0f)] 36 | public float DampingRatio = 0.5f; 37 | [ConditionalField("Mode", ParameterMode.SoftHalfLife, ParameterMode.SoftExponential, Min = 0.0001f, Max = 10.0f)] 38 | public float HalfLife = 0.02f; 39 | 40 | public void GenerateVelocityConstraintParams(float mass, out float dampingCoefficient, out float springConstant) 41 | { 42 | float dampingRatio = DampingRatio; 43 | float angularFrequency = FrequencyHz * ConstraintUtil.TwoPi; 44 | switch (Mode) 45 | { 46 | case ParameterMode.SoftHalfLife: 47 | dampingRatio = 0.6931472f / (angularFrequency * HalfLife); 48 | break; 49 | case ParameterMode.SoftExponential: 50 | angularFrequency = 0.6931472f / HalfLife; 51 | dampingRatio = 1.0f; 52 | break; 53 | } 54 | 55 | dampingCoefficient = 2.0f * mass * dampingRatio * angularFrequency; 56 | springConstant = mass * angularFrequency * angularFrequency; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/SoftConstraintParams.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6f40c6a179eab4f4ea9e8489e3adadc5 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/World.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using System.Collections.Generic; 13 | using UnityEngine; 14 | 15 | namespace PhysicsConstraints 16 | { 17 | public class World : MonoBehaviour 18 | { 19 | private static int s_velocityIterations = 10; 20 | public static int VelocityIterations 21 | { 22 | get { return s_velocityIterations; } 23 | set { s_velocityIterations = Mathf.Max(1, value); } 24 | } 25 | 26 | public static Vector3 Gravity = Vector3.zero; 27 | 28 | private static IBroadphase m_broadphase = new NSquared(); 29 | public static IBroadphase Broadphase 30 | { 31 | get { return m_broadphase; } 32 | set 33 | { 34 | if (value != null) 35 | m_broadphase = value; 36 | else 37 | m_broadphase = new NSquared(); 38 | } 39 | } 40 | 41 | // constraints 42 | private static ICollection s_constraints; 43 | public static void Register(IPhysicsConstraint c) 44 | { 45 | ValidateWorld(); 46 | s_constraints.Add(c); 47 | } 48 | public static void Unregister(IPhysicsConstraint c) 49 | { 50 | if (s_constraints == null) 51 | return; 52 | 53 | s_constraints.Remove(c); 54 | } 55 | 56 | // contacts 57 | private static ICollection s_contacts; 58 | public static void Register(Contact contact) 59 | { 60 | ValidateWorld(); 61 | s_contacts.Add(contact); 62 | } 63 | // no need to unregister, as contacts are cleared every time step 64 | 65 | // physics bodies 66 | private static ICollection s_bodies; 67 | public static void Register(PhysicsBody b) 68 | { 69 | ValidateWorld(); 70 | s_bodies.Add(b); 71 | } 72 | public static void Unregister(PhysicsBody b) 73 | { 74 | if (s_bodies == null) 75 | return; 76 | 77 | s_bodies.Remove(b); 78 | } 79 | 80 | // colliders 81 | private static HashSet s_colliders; 82 | public static void Register(PhysicsCollider collider) 83 | { 84 | ValidateWorld(); 85 | s_colliders.Add(collider); 86 | } 87 | public static void Unregister(PhysicsCollider collider) 88 | { 89 | s_colliders.Remove(collider); 90 | } 91 | 92 | private static GameObject s_world; 93 | private static void ValidateWorld() 94 | { 95 | 96 | if (s_world != null) 97 | return; 98 | 99 | s_constraints = new HashSet(); 100 | s_contacts = new List(); 101 | s_bodies = new HashSet(); 102 | s_colliders = new HashSet(); 103 | 104 | s_world = new GameObject("World (Physics Constraints)"); 105 | s_world.AddComponent(); 106 | } 107 | 108 | private void FixedUpdate() 109 | { 110 | if (s_world != gameObject) 111 | { 112 | Destroy(gameObject); 113 | return; 114 | } 115 | 116 | float dt = Mathf.Max(ConstraintUtil.Epsilon, Time.fixedDeltaTime); 117 | Step(Time.fixedDeltaTime); 118 | } 119 | 120 | public static void Step(float dt) 121 | { 122 | // inertia 123 | foreach (var body in s_bodies) 124 | { 125 | body.UpdateInertiaWs(); 126 | } 127 | 128 | // collision detection 129 | { 130 | var colliderPairs = Broadphase.GenerateColliderPairs(s_colliders); 131 | foreach (var colliderPair in colliderPairs) 132 | { 133 | Contact contact = null; 134 | if (!Collision.DetectCollision(colliderPair.ColliderA, colliderPair.ColliderB, ref contact)) 135 | continue; 136 | 137 | Register(contact); 138 | } 139 | } 140 | 141 | // gravity 142 | { 143 | Vector3 gravityImpulse = Gravity * dt; 144 | foreach (var body in s_bodies) 145 | { 146 | body.LinearVelocity += gravityImpulse * body.GravityScale; 147 | } 148 | } 149 | 150 | // init constraints 151 | foreach (var contact in s_constraints) 152 | { 153 | contact.InitVelocityConstraint(dt); 154 | } 155 | foreach (var contact in s_contacts) 156 | { 157 | contact.InitVelocityConstraint(dt); 158 | } 159 | 160 | // solve constraints 161 | for (int i = 0; i < s_velocityIterations; ++i) 162 | { 163 | foreach (var constraint in s_constraints) 164 | { 165 | constraint.SolveVelocityConstraint(dt); 166 | } 167 | foreach (var contact in s_contacts) 168 | { 169 | contact.SolveVelocityConstraint(dt); 170 | } 171 | } 172 | 173 | // clear contacts 174 | foreach (var contact in s_contacts) 175 | { 176 | Pool.Store(contact); 177 | } 178 | s_contacts.Clear(); 179 | 180 | // integrate 181 | foreach (var body in s_bodies) 182 | { 183 | body.Integrate(dt); 184 | } 185 | 186 | // drag 187 | foreach (var body in s_bodies) 188 | { 189 | body.LinearVelocity *= Mathf.Pow(1.0f - body.LinearDrag, dt); 190 | body.AngularVelocity *= Mathf.Pow(1.0f - body.AngularDrag, dt); 191 | } 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Core/World.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f08311b5254c3ac4a86c97acd9356a40 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Util.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 969107fcdceffe140a984cdbd6724d2e 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Util/Pool.cs: -------------------------------------------------------------------------------- 1 | /******************************************************************************/ 2 | /* 3 | Project - Physics Constraints 4 | https://github.com/TheAllenChou/unity-physics-constraints 5 | 6 | Author - Ming-Lun "Allen" Chou 7 | Web - http://AllenChou.net 8 | Twitter - @TheAllenChou 9 | */ 10 | /******************************************************************************/ 11 | 12 | using System.Collections.Generic; 13 | 14 | namespace PhysicsConstraints 15 | { 16 | public class Pool where T : new() 17 | { 18 | private static List s_pool = new List(); 19 | 20 | public static T Get() 21 | { 22 | if (s_pool.Count == 0) 23 | return new T(); 24 | 25 | var obj = s_pool[s_pool.Count - 1]; 26 | s_pool.RemoveAt(s_pool.Count - 1); 27 | 28 | return obj; 29 | } 30 | 31 | public static void Store(T obj) 32 | { 33 | s_pool.Add(obj); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Physics Constraints/Assets/Physics Constraints/Util/Pool.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 49e821410354d564bba08b56ceaf219a 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /src/Physics Constraints/Packages/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "com.unity.2d.sprite": "1.0.0", 4 | "com.unity.2d.tilemap": "1.0.0", 5 | "com.unity.ads": "3.4.5", 6 | "com.unity.analytics": "3.3.5", 7 | "com.unity.collab-proxy": "1.2.16", 8 | "com.unity.ide.rider": "1.1.4", 9 | "com.unity.ide.vscode": "1.2.0", 10 | "com.unity.multiplayer-hlapi": "1.0.4", 11 | "com.unity.purchasing": "2.0.6", 12 | "com.unity.test-framework": "1.1.14", 13 | "com.unity.textmeshpro": "2.0.1", 14 | "com.unity.timeline": "1.2.9", 15 | "com.unity.ugui": "1.0.0", 16 | "com.unity.xr.legacyinputhelpers": "2.1.3", 17 | "com.unity.modules.ai": "1.0.0", 18 | "com.unity.modules.androidjni": "1.0.0", 19 | "com.unity.modules.animation": "1.0.0", 20 | "com.unity.modules.assetbundle": "1.0.0", 21 | "com.unity.modules.audio": "1.0.0", 22 | "com.unity.modules.cloth": "1.0.0", 23 | "com.unity.modules.director": "1.0.0", 24 | "com.unity.modules.imageconversion": "1.0.0", 25 | "com.unity.modules.imgui": "1.0.0", 26 | "com.unity.modules.jsonserialize": "1.0.0", 27 | "com.unity.modules.particlesystem": "1.0.0", 28 | "com.unity.modules.physics": "1.0.0", 29 | "com.unity.modules.physics2d": "1.0.0", 30 | "com.unity.modules.screencapture": "1.0.0", 31 | "com.unity.modules.terrain": "1.0.0", 32 | "com.unity.modules.terrainphysics": "1.0.0", 33 | "com.unity.modules.tilemap": "1.0.0", 34 | "com.unity.modules.ui": "1.0.0", 35 | "com.unity.modules.uielements": "1.0.0", 36 | "com.unity.modules.umbra": "1.0.0", 37 | "com.unity.modules.unityanalytics": "1.0.0", 38 | "com.unity.modules.unitywebrequest": "1.0.0", 39 | "com.unity.modules.unitywebrequestassetbundle": "1.0.0", 40 | "com.unity.modules.unitywebrequestaudio": "1.0.0", 41 | "com.unity.modules.unitywebrequesttexture": "1.0.0", 42 | "com.unity.modules.unitywebrequestwww": "1.0.0", 43 | "com.unity.modules.vehicles": "1.0.0", 44 | "com.unity.modules.video": "1.0.0", 45 | "com.unity.modules.vr": "1.0.0", 46 | "com.unity.modules.wind": "1.0.0", 47 | "com.unity.modules.xr": "1.0.0" 48 | } 49 | } 50 | --------------------------------------------------------------------------------