ReStocked/Source/Restock/ModuleRestockLinkedMesh.cs

114 lines
3.7 KiB
C#
Raw Normal View History

using UnityEngine;
using CompoundParts;
namespace Restock
{
/* Extends the stock CModuleLinkedMesh used on struts and pipes to make pipe textures repeat instead of stretch when
a pipe is extended beyond its default length */
2020-07-03 08:22:08 +00:00
public class ModuleRestockLinkedMesh : CModuleLinkedMesh
{
// the direction along the texture that the pipe points. set to "x" or "y"
2020-07-03 08:22:08 +00:00
[KSPField] public string stretchAxis = "x";
// space-seperated list of textures to be effected by the length, preferably all of the ones on the material
// Unity has no good way to get all the texture names attached to a material so it has to be set manually, unfortunately
2020-07-03 08:22:08 +00:00
[KSPField] public string stretchTextures = "_MainTex";
// reference to the material we will be modifying
private Material[] pipeMaterials;
2020-07-03 08:22:08 +00:00
// array of property IDs corresponding to the textures
private int[] pipeMaterialIDs;
2020-07-03 08:22:08 +00:00
// index of the texture scale vector, 0 for x, 1 for y
private int pipeStretchIndex;
2020-07-03 08:22:08 +00:00
// initial scale of the pipe object, may not be 1
private float baseStretch;
// scale vector for the material
private Vector2 texScale = Vector2.one;
2020-07-03 08:22:08 +00:00
// offset vector for the material
private Vector2 texOffset = Vector2.zero;
2020-07-03 08:22:08 +00:00
public override void OnStart(StartState state)
{
base.OnStart(state);
2020-07-03 08:22:08 +00:00
// get all materials on the line object, including disabled ones
var renderers = line.GetComponentsInChildren<MeshRenderer>(true);
pipeMaterials = new Material[renderers.Length];
for (int i = 0; i < renderers.Length; i++)
{
pipeMaterials[i] = renderers[i].material;
}
2020-07-03 08:22:08 +00:00
// split texture list and convert to property IDs for easy access
var texNames = stretchTextures.Split(' ');
pipeMaterialIDs = new int[texNames.Length];
for (int i = 0; i < texNames.Length; i++)
{
pipeMaterialIDs[i] = Shader.PropertyToID(texNames[i]);
}
2020-07-03 08:22:08 +00:00
// default to 'x' if an invalid value is used
pipeStretchIndex = stretchAxis != "y" ? 0 : 1;
2019-06-09 16:32:25 +00:00
baseStretch = part.scaleFactor;
2020-07-03 08:22:08 +00:00
2019-06-09 16:32:25 +00:00
GameEvents.onEditorVariantApplied.Add(OnVariantApplied);
}
private void OnDestroy()
{
GameEvents.onEditorVariantApplied.Remove(OnVariantApplied);
}
public override void OnTargetSet(Part newTarget)
{
base.OnTargetSet(newTarget);
UpdateStretch();
}
public override void OnTargetUpdate()
{
base.OnTargetUpdate();
UpdateStretch();
}
public override void OnPreviewAttachment(Vector3 rDir, Vector3 rPos, Quaternion rRot)
{
base.OnPreviewAttachment(rDir, rPos, rRot);
UpdateStretch();
}
public void OnVariantApplied(Part appliedPart, PartVariant variant)
2019-06-09 16:32:25 +00:00
{
if (appliedPart == part) UpdateStretch();
2019-06-09 16:32:25 +00:00
}
2020-07-03 08:22:08 +00:00
// updates the texture stretch to match the pipe object's local scale
private void UpdateStretch()
{
var stretch = line.localScale.z / baseStretch;
2020-07-03 08:22:08 +00:00
texScale[pipeStretchIndex] = stretch;
texOffset[pipeStretchIndex] = (1 - stretch) / 2;
2020-07-03 08:22:08 +00:00
foreach (var material in pipeMaterials)
{
2020-07-03 08:22:08 +00:00
foreach (var id in pipeMaterialIDs)
{
2020-07-03 08:22:08 +00:00
if (material.HasProperty(id))
{
material.SetTextureScale(id, texScale);
material.SetTextureOffset(id, texOffset);
}
}
}
}
}
}