Browse Source

Added script to curve meshes

added basic coriolis effect, needs to be replaced
develop
Joshua Reason 3 years ago
parent
commit
6802d93949
13 changed files with 457 additions and 5 deletions
  1. BIN
      Assets/Scenes/Test Scenes/Static Ring.unity
  2. +26
    -1
      Assets/Scripts/Static Physics/ArtificialGravity.cs
  3. +22
    -2
      Assets/Scripts/Static Physics/RotationController.cs
  4. +8
    -0
      Assets/Scripts/Utility.meta
  5. +99
    -0
      Assets/Scripts/Utility/MeshCurver.cs
  6. +11
    -0
      Assets/Scripts/Utility/MeshCurver.cs.meta
  7. +77
    -0
      Assets/World Assets/Materials/DoubleSided.mat
  8. +8
    -0
      Assets/World Assets/Materials/DoubleSided.mat.meta
  9. BIN
      Assets/World Assets/Models/SubdividedPlane_WithRail.fbx
  10. +97
    -0
      Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta
  11. +8
    -0
      Assets/World Assets/Shaders.meta
  12. +87
    -0
      Assets/World Assets/Shaders/DoubleSided.shader
  13. +9
    -0
      Assets/World Assets/Shaders/DoubleSided.shader.meta

BIN
Assets/Scenes/Test Scenes/Static Ring.unity (Stored with Git LFS) View File

size 304950

+ 26
- 1
Assets/Scripts/Static Physics/ArtificialGravity.cs View File

@ -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;
}

+ 22
- 2
Assets/Scripts/Static Physics/RotationController.cs View File

@ -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);
}

+ 8
- 0
Assets/Scripts/Utility.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6014d8143b6d2814cb80384d116a39a4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 99
- 0
Assets/Scripts/Utility/MeshCurver.cs View File

@ -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<MeshFilter>();
m_meshCollider = GetComponent<MeshCollider>();
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<Vector3> transformedVertices = new List<Vector3>();
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;
}
}

+ 11
- 0
Assets/Scripts/Utility/MeshCurver.cs.meta View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9d605cf3e1eb369488e85916fc948746
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 77
- 0
Assets/World Assets/Materials/DoubleSided.mat View File

@ -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}

+ 8
- 0
Assets/World Assets/Materials/DoubleSided.mat.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 284575bb9fa87cf408d55264b5273c44
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

BIN
Assets/World Assets/Models/SubdividedPlane_WithRail.fbx (Stored with Git LFS) View File

size 50220

+ 97
- 0
Assets/World Assets/Models/SubdividedPlane_WithRail.fbx.meta View File

@ -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:

+ 8
- 0
Assets/World Assets/Shaders.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 11923c7becd04754289b60db235cd1bb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 87
- 0
Assets/World Assets/Shaders/DoubleSided.shader View File

@ -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"
}

+ 9
- 0
Assets/World Assets/Shaders/DoubleSided.shader.meta View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: af3776ac6a322cb4da895ba92b2bc463
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

Loading…
Cancel
Save