mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Fix scaling issues when part is created
• KSP rescales the model object back to 1,1,1 on start, so don't use that for the model that gets scaled • Some refactoring to consolidate property IDs • rename some classes because I am indecisive • Add and Get methods for MaterialPropertyCollection • Make an attempt at a scale culling fix
This commit is contained in:
58
Source/ConformalDecals/DecalIconFixer.cs
Normal file
58
Source/ConformalDecals/DecalIconFixer.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals {
|
||||
[KSPAddon(KSPAddon.Startup.EditorAny, true)]
|
||||
public class DecalIconFixer : MonoBehaviour {
|
||||
private static readonly List<string> PartNames = new List<string>();
|
||||
|
||||
public static void QueuePart(string name) {
|
||||
PartNames.Add(name);
|
||||
}
|
||||
|
||||
public void Start() {
|
||||
foreach (var partName in PartNames) {
|
||||
Debug.Log($"Unf*&king decal preview on {partName}");
|
||||
var partInfo = PartLoader.getPartInfoByName(partName);
|
||||
|
||||
if (partInfo == null) {
|
||||
Debug.Log($"Part {partName} not found!");
|
||||
continue;
|
||||
}
|
||||
|
||||
var icon = partInfo.iconPrefab;
|
||||
|
||||
var decalModule = partInfo.partPrefab.FindModuleImplementing<ModuleConformalDecalBase>();
|
||||
|
||||
var frontTransform = Part.FindHeirarchyTransform(icon.transform, decalModule.decalFront);
|
||||
var backTransform = Part.FindHeirarchyTransform(icon.transform, decalModule.decalBack);
|
||||
|
||||
if (frontTransform == null) {
|
||||
Debug.Log($"Part {partName} has no frontTransform");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (backTransform == null) {
|
||||
Debug.Log($"Part {partName} has no backTransform");
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector2 backScale = default;
|
||||
if (decalModule.updateBackScale) {
|
||||
var aspectRatio = decalModule.materialProperties.AspectRatio;
|
||||
var size = new Vector2(decalModule.scale, decalModule.scale * aspectRatio);
|
||||
backScale.x = size.x * decalModule.backTextureBaseScale.x;
|
||||
backScale.y = size.y * decalModule.backTextureBaseScale.y;
|
||||
Debug.Log($"backscale is {backScale}");
|
||||
}
|
||||
|
||||
backTransform.GetComponent<MeshRenderer>().material = decalModule.backMaterial;
|
||||
frontTransform.GetComponent<MeshRenderer>().material = decalModule.materialProperties.PreviewMaterial;
|
||||
|
||||
if (decalModule.updateBackScale) {
|
||||
backTransform.GetComponent<MeshRenderer>().material.SetTextureScale(PropertyIDs._MainTex, backScale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user