mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Update launch clamp module
• Use instancing instead of editing the mesh to extend the launch clamp girder • Calculate length from local position when in editor to avoid slight delay
This commit is contained in:
parent
e3f53ce64d
commit
7ab6df3bd4
Binary file not shown.
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Restock
|
||||
@ -15,150 +13,82 @@ namespace Restock
|
||||
[KSPField] public Transform towerAnchor;
|
||||
[KSPField] public Transform towerGirder;
|
||||
[KSPField] public Transform towerStretch;
|
||||
|
||||
[KSPField] public Mesh girderSegmentMesh;
|
||||
[KSPField] public Mesh girderMesh;
|
||||
|
||||
private List<Vector3> _girderVerts;
|
||||
private List<Vector2> _girderUVs;
|
||||
private List<Vector3> _girderNormals;
|
||||
private List<Vector4> _girderTangents;
|
||||
private List<Color32> _girderColors;
|
||||
private List<int> _girderTris;
|
||||
private int _girderSegments;
|
||||
|
||||
private bool _girderHasTangents = false;
|
||||
private bool _girderHasColors = false;
|
||||
private int _girderVertCount;
|
||||
private int _girderTriCount;
|
||||
[KSPField] public int maxSegments = 100;
|
||||
|
||||
private bool _flightUpdate = false;
|
||||
private Material _girderMaterial;
|
||||
private Mesh _girderMesh;
|
||||
|
||||
private int _girderSegments;
|
||||
private Matrix4x4[] _girderMatrices;
|
||||
|
||||
public override void OnLoad(ConfigNode node)
|
||||
{
|
||||
Debug.Log("OnLoad Called");
|
||||
|
||||
towerPivot = base.part.FindModelTransform(trf_towerPivot_name);
|
||||
towerYoke = base.part.FindModelTransform(trf_towerYoke_name);
|
||||
towerAnchor = base.part.FindModelTransform(trf_anchor_name);
|
||||
towerGirder = base.part.FindModelTransform(trf_towerGirder_name);
|
||||
towerStretch = base.part.FindModelTransform(trf_towerStretch_name);
|
||||
|
||||
girderMesh = towerGirder.GetComponent<MeshFilter>().mesh;
|
||||
girderSegmentMesh = Instantiate<Mesh>(girderMesh);
|
||||
|
||||
base.OnLoad(node);
|
||||
}
|
||||
|
||||
public override void OnStart(StartState state)
|
||||
{
|
||||
|
||||
Debug.Log("OnStart Called");
|
||||
Debug.Log(girderSegmentMesh == null);
|
||||
girderMesh = towerGirder.GetComponent<MeshFilter>().mesh;
|
||||
|
||||
_girderVertCount = girderSegmentMesh.vertexCount;
|
||||
_girderTriCount = girderSegmentMesh.triangles.Length;
|
||||
|
||||
_girderVerts = new List<Vector3>(girderSegmentMesh.vertices);
|
||||
_girderUVs = new List<Vector2>(girderSegmentMesh.uv);
|
||||
_girderNormals = new List<Vector3>(girderSegmentMesh.normals);
|
||||
if (girderSegmentMesh.tangents.Length > 0)
|
||||
{
|
||||
_girderHasTangents = true;
|
||||
_girderTangents = new List<Vector4>(girderSegmentMesh.tangents);
|
||||
}
|
||||
if (girderSegmentMesh.colors32.Length > 0)
|
||||
{
|
||||
_girderHasColors = true;
|
||||
_girderColors = new List<Color32>(girderSegmentMesh.colors32);
|
||||
}
|
||||
_girderTris = new List<int>(girderSegmentMesh.triangles);
|
||||
_girderSegments = 1;
|
||||
|
||||
base.OnStart(state);
|
||||
|
||||
UpdateClamp();
|
||||
var girderRenderer = towerGirder.GetComponent<MeshRenderer>();
|
||||
_girderMaterial = girderRenderer.material;
|
||||
_girderMesh = towerGirder.GetComponent<MeshFilter>().mesh;
|
||||
|
||||
girderRenderer.enabled = false; // we'll render manually from now on
|
||||
|
||||
_girderSegments = 0;
|
||||
_girderMatrices = new Matrix4x4[maxSegments];
|
||||
|
||||
_girderMaterial.enableInstancing = true;
|
||||
if (!_girderMaterial.enableInstancing)
|
||||
{
|
||||
this.LogError("Could not enable instancing! Aborting");
|
||||
_girderSegments = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void LateUpdate()
|
||||
{
|
||||
|
||||
if (_girderSegments < 0) return;
|
||||
|
||||
if (!HighLogic.LoadedSceneIsEditor)
|
||||
{
|
||||
if (_flightUpdate) return;
|
||||
else _flightUpdate = true;
|
||||
}
|
||||
|
||||
UpdateClamp();
|
||||
}
|
||||
var height1 = HighLogic.LoadedSceneIsEditor ? towerStretch.position.y : base.height;
|
||||
var initialHeight1 = base.initialHeight;
|
||||
|
||||
public void UpdateClamp()
|
||||
{
|
||||
var height = base.height;
|
||||
var initialHeight = base.initialHeight;
|
||||
|
||||
towerAnchor.position = towerStretch.position - (towerStretch.up * height);
|
||||
towerAnchor.position = towerStretch.position - (towerStretch.up * height1);
|
||||
|
||||
var vec1 = Vector3.down;
|
||||
var vec2 = towerAnchor.localPosition - towerYoke.localPosition;
|
||||
towerYoke.localRotation = Quaternion.FromToRotation(vec1, vec2);
|
||||
|
||||
var segments = Mathf.CeilToInt(height / initialHeight);
|
||||
if (segments != _girderSegments)
|
||||
_girderSegments = Mathf.CeilToInt(height1 / base.initialHeight);
|
||||
_girderSegments = Math.Min(_girderSegments, maxSegments);
|
||||
_girderSegments = Math.Max(_girderSegments, 0);
|
||||
|
||||
var matrix = towerGirder.localToWorldMatrix;
|
||||
var offset = Matrix4x4.Translate(matrix.MultiplyVector(Vector3.down * base.initialHeight));
|
||||
|
||||
for (int i = 0; i < _girderSegments; i++)
|
||||
{
|
||||
UpdateGirder(segments);
|
||||
}
|
||||
_girderMatrices[i] = matrix;
|
||||
matrix *= offset;
|
||||
}
|
||||
|
||||
private void UpdateGirder(int length)
|
||||
{
|
||||
if (length > _girderSegments)
|
||||
{
|
||||
for (int i = _girderSegments; i < length; i++)
|
||||
{
|
||||
var offset = Vector3.down * base.initialHeight * i;
|
||||
var indexOffset = _girderVertCount * i;
|
||||
for (int v = 0; v < _girderVertCount; v++)
|
||||
{
|
||||
_girderVerts.Add(girderSegmentMesh.vertices[v] + offset);
|
||||
}
|
||||
_girderNormals.AddRange(girderSegmentMesh.normals);
|
||||
_girderUVs.AddRange(girderSegmentMesh.uv);
|
||||
|
||||
if (_girderHasTangents) _girderTangents.AddRange(girderSegmentMesh.tangents);
|
||||
if (_girderHasColors) _girderColors.AddRange(girderSegmentMesh.colors32);
|
||||
|
||||
for (int t = 0; t < _girderTriCount; t++)
|
||||
{
|
||||
_girderTris.Add(girderSegmentMesh.triangles[t] + indexOffset);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
var startIndex = length * _girderVertCount;
|
||||
var count = (_girderSegments - length) * _girderVertCount;
|
||||
_girderVerts.RemoveRange(startIndex, count);
|
||||
_girderNormals.RemoveRange(startIndex, count);
|
||||
_girderUVs.RemoveRange(startIndex, count);
|
||||
if (_girderHasTangents) _girderTangents.RemoveRange(startIndex, count);
|
||||
if (_girderHasColors) _girderColors.RemoveRange(startIndex, count);
|
||||
|
||||
_girderTris.RemoveRange(length * _girderTriCount, (_girderSegments - length) * _girderTriCount);
|
||||
}
|
||||
|
||||
girderMesh.Clear();
|
||||
|
||||
girderMesh.SetVertices(_girderVerts);
|
||||
girderMesh.SetNormals(_girderNormals);
|
||||
girderMesh.SetUVs(0, _girderUVs);
|
||||
if (_girderHasTangents) girderMesh.SetTangents(_girderTangents);
|
||||
if (_girderHasColors) girderMesh.SetColors(_girderColors);
|
||||
girderMesh.SetTriangles(_girderTris, 0);
|
||||
|
||||
girderMesh.RecalculateBounds();
|
||||
_girderSegments = length;
|
||||
Graphics.DrawMeshInstanced(_girderMesh, 0, _girderMaterial, _girderMatrices, _girderSegments, part.mpb);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user