From 6802d93949605e1e72fda6bf3bcf923d8fb14a84 Mon Sep 17 00:00:00 2001 From: Joshua Reason Date: Mon, 7 Sep 2020 11:32:57 +1000 Subject: [PATCH] Added script to curve meshes added basic coriolis effect, needs to be replaced --- Assets/Scenes/Test Scenes/Static Ring.unity | 4 +- .../Static Physics/ArtificialGravity.cs | 27 ++++- .../Static Physics/RotationController.cs | 24 ++++- Assets/Scripts/Utility.meta | 8 ++ Assets/Scripts/Utility/MeshCurver.cs | 99 +++++++++++++++++++ Assets/Scripts/Utility/MeshCurver.cs.meta | 11 +++ Assets/World Assets/Materials/DoubleSided.mat | 77 +++++++++++++++ .../Materials/DoubleSided.mat.meta | 8 ++ .../Models/SubdividedPlane_WithRail.fbx | 3 + .../Models/SubdividedPlane_WithRail.fbx.meta | 97 ++++++++++++++++++ Assets/World Assets/Shaders.meta | 8 ++ .../World Assets/Shaders/DoubleSided.shader | 87 ++++++++++++++++ .../Shaders/DoubleSided.shader.meta | 9 ++ 13 files changed, 457 insertions(+), 5 deletions(-) create mode 100644 Assets/Scripts/Utility.meta create mode 100644 Assets/Scripts/Utility/MeshCurver.cs create mode 100644 Assets/Scripts/Utility/MeshCurver.cs.meta create mode 100644 Assets/World Assets/Materials/DoubleSided.mat create mode 100644 Assets/World Assets/Materials/DoubleSided.mat.meta create mode 100644 Assets/World Assets/Models/SubdividedPlane_WithRail.fbx create mode 100644 Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta create mode 100644 Assets/World Assets/Shaders.meta create mode 100644 Assets/World Assets/Shaders/DoubleSided.shader create mode 100644 Assets/World Assets/Shaders/DoubleSided.shader.meta diff --git a/Assets/Scenes/Test Scenes/Static Ring.unity b/Assets/Scenes/Test Scenes/Static Ring.unity index 2001968..b78531a 100644 --- a/Assets/Scenes/Test Scenes/Static Ring.unity +++ b/Assets/Scenes/Test Scenes/Static Ring.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3b59f06940bc8d2369ce00fdcbf087eb938ced348ef7eff21d5bbdc3ccfc6b29 -size 124840 +oid sha256:01bfc90496a009a41f66e8b1cbca917aecbffb39c1f8526138dfd9b6e8d0bbd1 +size 304950 diff --git a/Assets/Scripts/Static Physics/ArtificialGravity.cs b/Assets/Scripts/Static Physics/ArtificialGravity.cs index 2db4e38..17d6eae 100644 --- a/Assets/Scripts/Static Physics/ArtificialGravity.cs +++ b/Assets/Scripts/Static Physics/ArtificialGravity.cs @@ -9,6 +9,9 @@ public class ArtificialGravity : MonoBehaviour [SerializeField] private RotationController Ship; + [SerializeField] + private float m_correlusMultiplier = 1; + private Rigidbody rb; private void OnEnable() @@ -31,12 +34,34 @@ public class ArtificialGravity : MonoBehaviour if (Ship.RotationPeriod <= 0) return; - Vector3 direction = (transform.position - Ship.Position); + Vector3 direction = Ship.getDownDirection(transform.position, false); float force = direction.magnitude * Mathf.Pow(2 * Mathf.PI / Ship.RotationPeriod, 2); rb.AddForce(direction.normalized * force * Time.fixedDeltaTime, ForceMode.Acceleration); + + if (!IsGrounded()) + { + direction = Ship.getPerpendicularDirection(transform.position); + + rb.AddForce(direction * Ship.CorrelusStrength * m_correlusMultiplier * Time.fixedDeltaTime, ForceMode.Acceleration); + Debug.DrawRay(transform.position, direction, Color.blue); + } } + private bool IsGrounded() + { + + Ray ray = new Ray(transform.position, Ship.getDownDirection(transform.position)); + RaycastHit hit; + + if (Physics.Raycast(ray, out hit, 0.25f)){ + return true; + } + return false; + + } + + diff --git a/Assets/Scripts/Static Physics/RotationController.cs b/Assets/Scripts/Static Physics/RotationController.cs index 42e3091..a87ba20 100644 --- a/Assets/Scripts/Static Physics/RotationController.cs +++ b/Assets/Scripts/Static Physics/RotationController.cs @@ -8,6 +8,8 @@ public class RotationController : MonoBehaviour #region Read-Only Fields public float RotationPeriod => m_RotationPeriod; public Vector3 Position => transform.position; + public Vector3 RotationAxis => m_RotationAxis; + public float CorrelusStrength => m_correlusStrength; #endregion Read-Only Fields [SerializeField] @@ -16,11 +18,29 @@ public class RotationController : MonoBehaviour [SerializeField] private Vector3 m_RotationAxis = Vector3.forward; + [SerializeField] + private float m_correlusStrength = 100; + + + + public Vector3 getDownDirection(Vector3 objectPosition, bool normalized = true) + { + if (normalized) + return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis).normalized; + else + return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis); + + } + + public Vector3 getPerpendicularDirection(Vector3 objectPosition) + { + return Vector3.Cross(RotationAxis, getDownDirection(objectPosition)).normalized; + } - public Vector3 getDownDirection(Vector3 objectPosition) + private void FixedUpdate() { - return Vector3.ProjectOnPlane((objectPosition - transform.position).normalized, m_RotationAxis); + //transform.Rotate(m_RotationAxis, 360 / m_RotationPeriod * Time.fixedDeltaTime); } diff --git a/Assets/Scripts/Utility.meta b/Assets/Scripts/Utility.meta new file mode 100644 index 0000000..85e2e08 --- /dev/null +++ b/Assets/Scripts/Utility.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6014d8143b6d2814cb80384d116a39a4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Utility/MeshCurver.cs b/Assets/Scripts/Utility/MeshCurver.cs new file mode 100644 index 0000000..765403c --- /dev/null +++ b/Assets/Scripts/Utility/MeshCurver.cs @@ -0,0 +1,99 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class MeshCurver : MonoBehaviour +{ + + [Header("References")] + [SerializeField] + private RotationController m_pivot; + + [Header("Settings")] + [SerializeField] + private bool m_ApplyAtStart = true; + [SerializeField] + private bool m_UpdateCollider = true; + + [Header("Debug")] + [SerializeField] + private bool m_CalculateEachUpdate = false; + + + private Mesh m_startMesh; + private MeshFilter m_meshFilter; + private MeshCollider m_meshCollider; + + private void Start() + { + m_meshFilter = GetComponent(); + m_meshCollider = GetComponent(); + + m_startMesh = m_meshFilter.mesh; + if (m_ApplyAtStart) + UpdateMesh(); + } + + private void Update() + { + if (m_CalculateEachUpdate) + UpdateMesh(); + } + + [ContextMenu("Update Mesh")] + public void UpdateMesh() + { + Mesh newMesh = UpdateMesh(m_pivot.Position, m_startMesh, m_pivot.RotationAxis, transform); + m_meshFilter.mesh = newMesh; + + if (m_meshCollider != null && m_UpdateCollider) + m_meshCollider.sharedMesh = newMesh; + + + } + + + + public Mesh UpdateMesh(Vector3 centre, Mesh mesh, Vector3 Axis, Transform origin) + { + + List transformedVertices = new List(); + Vector3[] vertices = mesh.vertices; + Vector3 closestPoint = origin.TransformPoint(vertices.OrderBy(p => Vector3.Distance(centre, origin.TransformPoint(p))).First()); + Vector3 direction = closestPoint - centre; + Vector3 perpendicular = Vector3.Cross(Axis, direction).normalized; + + Debug.DrawLine(centre, closestPoint, Color.red); + + float radius = direction.magnitude; + + foreach (Vector3 vertex in vertices) + { + Vector3 worldPosition = origin.TransformPoint(vertex); + Vector3 directionFromStartPoint = Vector3.ProjectOnPlane(worldPosition - closestPoint, Axis); + Vector3 planeOffset = Vector3.Project(worldPosition - closestPoint, Axis); + float heightOffset = radius - Vector3.Project(worldPosition-centre,direction.normalized).magnitude; + + float arcLength = directionFromStartPoint.magnitude; + float angle = (arcLength / radius); + if (Vector3.Dot(directionFromStartPoint, perpendicular) < 0) + angle = -angle; + + Vector3 directionFromCentre = Quaternion.AngleAxis(Mathf.Rad2Deg * angle, Axis) * ((direction)); + transformedVertices.Add((origin.InverseTransformPoint(centre + directionFromCentre.normalized * (radius - heightOffset) + planeOffset))); + } + + Mesh retVal = new Mesh(); + retVal.vertices = transformedVertices.ToArray(); + retVal.triangles = mesh.triangles; + retVal.uv = mesh.uv; + + retVal.RecalculateNormals(); + retVal.RecalculateTangents(); + retVal.RecalculateBounds(); + + return retVal; + } + +} diff --git a/Assets/Scripts/Utility/MeshCurver.cs.meta b/Assets/Scripts/Utility/MeshCurver.cs.meta new file mode 100644 index 0000000..2b9e042 --- /dev/null +++ b/Assets/Scripts/Utility/MeshCurver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9d605cf3e1eb369488e85916fc948746 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/World Assets/Materials/DoubleSided.mat b/Assets/World Assets/Materials/DoubleSided.mat new file mode 100644 index 0000000..f3850fd --- /dev/null +++ b/Assets/World Assets/Materials/DoubleSided.mat @@ -0,0 +1,77 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: DoubleSided + m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + m_ShaderKeywords: + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0.5 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/World Assets/Materials/DoubleSided.mat.meta b/Assets/World Assets/Materials/DoubleSided.mat.meta new file mode 100644 index 0000000..5505699 --- /dev/null +++ b/Assets/World Assets/Materials/DoubleSided.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 284575bb9fa87cf408d55264b5273c44 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx b/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx new file mode 100644 index 0000000..9013cbe --- /dev/null +++ b/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a2dc39763a0f2feb9ff56d254ef405861e0f58fc3f4f764e0b213e4d81383816 +size 50220 diff --git a/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta b/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta new file mode 100644 index 0000000..209e8e0 --- /dev/null +++ b/Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta @@ -0,0 +1,97 @@ +fileFormatVersion: 2 +guid: 08d37afad95a047449d920cf7f3002f4 +ModelImporter: + serializedVersion: 19301 + internalIDToNameTable: [] + externalObjects: {} + materials: + materialImportMode: 1 + materialName: 0 + materialSearch: 1 + materialLocation: 1 + animations: + legacyGenerateAnimations: 4 + bakeSimulation: 0 + resampleCurves: 1 + optimizeGameObjects: 0 + motionNodeName: + rigImportErrors: + rigImportWarnings: + animationImportErrors: + animationImportWarnings: + animationRetargetingWarnings: + animationDoRetargetingWarnings: 0 + importAnimatedCustomProperties: 0 + importConstraints: 0 + animationCompression: 1 + animationRotationError: 0.5 + animationPositionError: 0.5 + animationScaleError: 0.5 + animationWrapMode: 0 + extraExposedTransformPaths: [] + extraUserProperties: [] + clipAnimations: [] + isReadable: 1 + meshes: + lODScreenPercentages: [] + globalScale: 1 + meshCompression: 0 + addColliders: 0 + useSRGBMaterialColor: 1 + sortHierarchyByName: 1 + importVisibility: 1 + importBlendShapes: 1 + importCameras: 1 + importLights: 1 + fileIdsGeneration: 2 + swapUVChannels: 0 + generateSecondaryUV: 0 + useFileUnits: 1 + keepQuads: 0 + weldVertices: 1 + preserveHierarchy: 0 + skinWeightsMode: 0 + maxBonesPerVertex: 4 + minBoneWeight: 0.001 + meshOptimizationFlags: -1 + indexFormat: 0 + secondaryUVAngleDistortion: 8 + secondaryUVAreaDistortion: 15.000001 + secondaryUVHardAngle: 88 + secondaryUVPackMargin: 4 + useFileScale: 1 + tangentSpace: + normalSmoothAngle: 60 + normalImportMode: 0 + tangentImportMode: 3 + normalCalculationMode: 4 + legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 0 + blendShapeNormalImportMode: 1 + normalSmoothingSource: 0 + referencedClips: [] + importAnimation: 1 + humanDescription: + serializedVersion: 3 + human: [] + skeleton: [] + armTwist: 0.5 + foreArmTwist: 0.5 + upperLegTwist: 0.5 + legTwist: 0.5 + armStretch: 0.05 + legStretch: 0.05 + feetSpacing: 0 + globalScale: 1 + rootMotionBoneName: + hasTranslationDoF: 0 + hasExtraRoot: 0 + skeletonHasParents: 1 + lastHumanDescriptionAvatarSource: {instanceID: 0} + autoGenerateAvatarMappingIfUnspecified: 1 + animationType: 2 + humanoidOversampling: 1 + avatarSetup: 0 + additionalBone: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/World Assets/Shaders.meta b/Assets/World Assets/Shaders.meta new file mode 100644 index 0000000..c1ef747 --- /dev/null +++ b/Assets/World Assets/Shaders.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 11923c7becd04754289b60db235cd1bb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/World Assets/Shaders/DoubleSided.shader b/Assets/World Assets/Shaders/DoubleSided.shader new file mode 100644 index 0000000..244f30c --- /dev/null +++ b/Assets/World Assets/Shaders/DoubleSided.shader @@ -0,0 +1,87 @@ +Shader "Custom/DoubleSided" { + Properties{ + _Color("Color", Color) = (1,1,1,1) + _MainTex("Albedo (RGB)", 2D) = "white" {} + _Glossiness("Smoothness", Range(0,1)) = 0.5 + _Metallic("Metallic", Range(0,1)) = 0.0 + _BumpMap("NormalMap", 2D) = "Bump" {} + _BumpScale("Normal Power", Float) = 1.0 + _NormalDefault("Normal Default", Color) = (128, 128, 255) + } + SubShader{ + Tags { "RenderType" = "Opaque" } + LOD 200 + + // Render back faces first + Cull Front + CGPROGRAM + // Physically based Standard lighting model, and enable shadows on all light types + #pragma surface surf Standard fullforwardshadows + + // Use shader model 3.0 target, to get nicer looking lighting + #pragma target 3.0 + + sampler2D _MainTex; + sampler2D _BumpMap; + + struct Input { + float2 uv_MainTex; + }; + + half _Glossiness; + half _BumpScale; + half _Metallic; + fixed4 _Color; + half3 _NormalDefault; + + void surf(Input IN, inout SurfaceOutputStandard o) { + // Albedo comes from a texture tinted by color + fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; + o.Albedo = c.rgb; + // Metallic and smoothness come from slider variables + o.Metallic = _Metallic; + o.Smoothness = _Glossiness; + o.Normal = -UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex)); + //o.Normal = -_NormalDefault; + o.Alpha = c.a; + } + ENDCG + + + // Now render front faces + Cull Back + CGPROGRAM + // Physically based Standard lighting model, and enable shadows on all light types + #pragma surface surf Standard fullforwardshadows + + // Use shader model 3.0 target, to get nicer looking lighting + #pragma target 3.0 + + sampler2D _MainTex; + sampler2D _BumpMap; + + struct Input { + float2 uv_MainTex; + }; + + half _Glossiness; + half _BumpScale; + half _Metallic; + fixed4 _Color; + half3 _NormalDefault; + + void surf(Input IN, inout SurfaceOutputStandard o) { + // Albedo comes from a texture tinted by color + fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; + o.Albedo = c.rgb; + // Metallic and smoothness come from slider variables + o.Metallic = _Metallic; + o.Smoothness = _Glossiness; + o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex)); + //o.Normal = _NormalDefault; + o.Alpha = c.a; + } + ENDCG + } + FallBack "Diffuse" +} \ No newline at end of file diff --git a/Assets/World Assets/Shaders/DoubleSided.shader.meta b/Assets/World Assets/Shaders/DoubleSided.shader.meta new file mode 100644 index 0000000..df5effa --- /dev/null +++ b/Assets/World Assets/Shaders/DoubleSided.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: af3776ac6a322cb4da895ba92b2bc463 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: