mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
More work trying to fix part icons
This commit is contained in:
parent
e9c8f3dafb
commit
0ef46a3182
62
Source/ConformalDecals/ConformalDecalIconFixer.cs
Normal file
62
Source/ConformalDecals/ConformalDecalIconFixer.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace ConformalDecals {
|
||||||
|
[KSPAddon(KSPAddon.Startup.EditorAny, true)]
|
||||||
|
public class ConformalDecalIconFixer : MonoBehaviour {
|
||||||
|
private static readonly List<string> PartNames = new List<string>();
|
||||||
|
|
||||||
|
public static void QueuePart(string name) {
|
||||||
|
PartNames.Add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start() {
|
||||||
|
foreach (var name in PartNames) {
|
||||||
|
Debug.Log($"Unf*&king decal preview on {name}");
|
||||||
|
var partInfo = PartLoader.getPartInfoByName(name);
|
||||||
|
|
||||||
|
if (partInfo == null) {
|
||||||
|
Debug.Log($"Part {name} not found!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var icon = partInfo.iconPrefab;
|
||||||
|
|
||||||
|
var decalModule = partInfo.partPrefab.FindModuleImplementing<ModuleConformalDecalBase>();
|
||||||
|
|
||||||
|
if (partInfo == null) {
|
||||||
|
Debug.Log($"Part {name} has no decal module!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var frontTransform = Part.FindHeirarchyTransform(icon.transform, decalModule.decalFront);
|
||||||
|
var backTransform = Part.FindHeirarchyTransform(icon.transform, decalModule.decalBack);
|
||||||
|
|
||||||
|
if (frontTransform == null) {
|
||||||
|
Debug.Log($"Part {name} has no frontTransform");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (backTransform == null) {
|
||||||
|
Debug.Log($"Part {name} 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;
|
||||||
|
|
||||||
|
if (decalModule.updateBackScale) {
|
||||||
|
backTransform.GetComponent<MeshRenderer>().material.SetTextureScale(PropertyIDs._MainTex, backScale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -50,6 +50,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ConformalDecalConfig.cs" />
|
<Compile Include="ConformalDecalConfig.cs" />
|
||||||
|
<Compile Include="ConformalDecalIconFixer.cs" />
|
||||||
<Compile Include="MaterialModifiers\MaterialColorProperty.cs" />
|
<Compile Include="MaterialModifiers\MaterialColorProperty.cs" />
|
||||||
<Compile Include="MaterialModifiers\MaterialFloatProperty.cs" />
|
<Compile Include="MaterialModifiers\MaterialFloatProperty.cs" />
|
||||||
<Compile Include="MaterialModifiers\MaterialProperty.cs" />
|
<Compile Include="MaterialModifiers\MaterialProperty.cs" />
|
||||||
|
@ -4,7 +4,7 @@ using Object = UnityEngine.Object;
|
|||||||
|
|
||||||
namespace ConformalDecals.MaterialModifiers {
|
namespace ConformalDecals.MaterialModifiers {
|
||||||
public abstract class MaterialProperty : ScriptableObject {
|
public abstract class MaterialProperty : ScriptableObject {
|
||||||
public string Name {
|
public string PropertyName {
|
||||||
get => _propertyName;
|
get => _propertyName;
|
||||||
set {
|
set {
|
||||||
_propertyName = value;
|
_propertyName = value;
|
||||||
@ -18,7 +18,7 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
public virtual void ParseNode(ConfigNode node) {
|
public virtual void ParseNode(ConfigNode node) {
|
||||||
if (node == null) throw new ArgumentNullException("node cannot be null");
|
if (node == null) throw new ArgumentNullException("node cannot be null");
|
||||||
|
|
||||||
Name = node.GetValue("name");
|
PropertyName = node.GetValue("name");
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void Modify(Material material);
|
public abstract void Modify(Material material);
|
||||||
@ -57,10 +57,10 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (valueString == null)
|
if (valueString == null)
|
||||||
throw new FormatException($"Missing {typeof(T)} value for {valueName} in property '{Name}'");
|
throw new FormatException($"Missing {typeof(T)} value for {valueName} in property '{PropertyName}'");
|
||||||
|
|
||||||
if (valueString == string.Empty)
|
if (valueString == string.Empty)
|
||||||
throw new FormatException($"Empty {typeof(T)} value for {valueName} in property '{Name}'");
|
throw new FormatException($"Empty {typeof(T)} value for {valueName} in property '{PropertyName}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tryParse(valueString, out var value)) {
|
if (tryParse(valueString, out var value)) {
|
||||||
@ -72,7 +72,7 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
throw new FormatException($"Improperly formatted {typeof(T)} value for {valueName} in property '{Name}' : '{valueString}");
|
throw new FormatException($"Improperly formatted {typeof(T)} value for {valueName} in property '{PropertyName}' : '{valueString}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using UniLinq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace ConformalDecals.MaterialModifiers {
|
namespace ConformalDecals.MaterialModifiers {
|
||||||
public class MaterialPropertyCollection : ScriptableObject {
|
public class MaterialPropertyCollection : ScriptableObject, ISerializationCallbackReceiver {
|
||||||
private static readonly int OpacityId = Shader.PropertyToID("_DecalOpacity");
|
private static readonly int OpacityId = Shader.PropertyToID("_DecalOpacity");
|
||||||
private static readonly int CutoffId = Shader.PropertyToID("_Cutoff");
|
private static readonly int CutoffId = Shader.PropertyToID("_Cutoff");
|
||||||
|
|
||||||
public MaterialTextureProperty MainMaterialTextureProperty => _mainTexture;
|
|
||||||
|
|
||||||
public Shader DecalShader => _shader;
|
public Shader DecalShader => _shader;
|
||||||
|
|
||||||
public Material DecalMaterial {
|
public Material DecalMaterial {
|
||||||
get {
|
get {
|
||||||
Debug.Log($"{_textureMaterialProperties == null}");
|
|
||||||
if (_decalMaterial == null) {
|
if (_decalMaterial == null) {
|
||||||
_decalMaterial = new Material(_shader);
|
_decalMaterial = new Material(_shader);
|
||||||
UpdateMaterial(_decalMaterial);
|
UpdateMaterial(_decalMaterial);
|
||||||
@ -35,54 +34,108 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float AspectRatio {
|
public MaterialTextureProperty MainTexture {
|
||||||
get {
|
get => _mainTexture;
|
||||||
if (MainMaterialTextureProperty == null) return 1;
|
set {
|
||||||
return MainMaterialTextureProperty.AspectRatio;
|
if (!_materialProperties.ContainsValue(value))
|
||||||
|
throw new ArgumentException($"Texture property {value.name} is not part of this property collection.");
|
||||||
|
|
||||||
|
_mainTexture = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float AspectRatio {
|
||||||
|
get {
|
||||||
|
if (MainTexture == null) {
|
||||||
|
Debug.Log("No main texture specified! returning 1 for aspect ratio");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return MainTexture.AspectRatio;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SerializeField] private string[] _serializedIDs;
|
||||||
|
[SerializeField] private MaterialProperty[] _serializedProperties;
|
||||||
|
|
||||||
[SerializeField] private Shader _shader;
|
[SerializeField] private Shader _shader;
|
||||||
[SerializeField] private List<MaterialProperty> _materialProperties;
|
|
||||||
[SerializeField] private List<MaterialTextureProperty> _textureMaterialProperties;
|
|
||||||
[SerializeField] private MaterialTextureProperty _mainTexture;
|
[SerializeField] private MaterialTextureProperty _mainTexture;
|
||||||
|
|
||||||
|
private Dictionary<string, MaterialProperty> _materialProperties;
|
||||||
|
|
||||||
private Material _decalMaterial;
|
private Material _decalMaterial;
|
||||||
private Material _previewMaterial;
|
private Material _previewMaterial;
|
||||||
|
|
||||||
public void Initialize() {
|
public void OnBeforeSerialize() {
|
||||||
_materialProperties = new List<MaterialProperty>();
|
Debug.Log($"Serializing MaterialPropertyCollection {this.GetInstanceID()}");
|
||||||
_textureMaterialProperties = new List<MaterialTextureProperty>();
|
if (_materialProperties == null) throw new SerializationException("Tried to serialize an unininitalized MaterialPropertyCollection");
|
||||||
|
|
||||||
|
_serializedIDs = _materialProperties.Keys.ToArray();
|
||||||
|
_serializedProperties = _materialProperties.Values.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnAfterDeserialize() {
|
||||||
|
Debug.Log($"Deserializing MaterialPropertyCollection {this.GetInstanceID()}");
|
||||||
|
if (_serializedIDs == null) throw new SerializationException("ID array is null");
|
||||||
|
if (_serializedProperties == null) throw new SerializationException("Property array is null");
|
||||||
|
if (_serializedProperties.Length != _serializedIDs.Length) throw new SerializationException("Material property arrays are different lengths.");
|
||||||
|
|
||||||
|
_materialProperties ??= new Dictionary<string, MaterialProperty>();
|
||||||
|
|
||||||
|
for (var i = 0; i < _serializedIDs.Length; i++) {
|
||||||
|
var property = MaterialProperty.Instantiate(_serializedProperties[i]);
|
||||||
|
Debug.Log($"insantiating {property.GetType().Name} {property.GetInstanceID()}");
|
||||||
|
_materialProperties.Add(_serializedIDs[i], property);
|
||||||
|
|
||||||
|
if (property is MaterialTextureProperty textureProperty) {
|
||||||
|
_mainTexture = textureProperty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Awake() {
|
||||||
|
Debug.Log($"MaterialPropertyCollection {this.GetInstanceID()} onAwake");
|
||||||
|
_materialProperties ??= new Dictionary<string, MaterialProperty>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddProperty(MaterialProperty property) {
|
public void AddProperty(MaterialProperty property) {
|
||||||
if (property == null) throw new ArgumentNullException("Tried to add a null property");
|
if (property == null) throw new ArgumentNullException("Tried to add a null property");
|
||||||
if (_materialProperties == null || _textureMaterialProperties == null) {
|
|
||||||
Initialize();
|
|
||||||
Debug.LogWarning("Tried to add a property to uninitialized property collection! correcting now.");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var p in _materialProperties) {
|
_materialProperties.Add(property.name, property);
|
||||||
if (p.Name == property.Name) {
|
|
||||||
_materialProperties.Remove(property);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_materialProperties.Add(property);
|
|
||||||
|
|
||||||
if (property is MaterialTextureProperty textureProperty) {
|
if (property is MaterialTextureProperty textureProperty) {
|
||||||
foreach (var p in _textureMaterialProperties) {
|
|
||||||
if (p.Name == textureProperty.Name) {
|
|
||||||
_textureMaterialProperties.Remove(textureProperty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_textureMaterialProperties.Add(textureProperty);
|
|
||||||
|
|
||||||
if (textureProperty.isMain) _mainTexture ??= textureProperty;
|
if (textureProperty.isMain) _mainTexture ??= textureProperty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ParseProperty<T>(ConfigNode node) where T : MaterialProperty {
|
||||||
|
var name = node.GetValue("name");
|
||||||
|
if (string.IsNullOrEmpty(name)) throw new ArgumentException("node has no name");
|
||||||
|
|
||||||
|
Debug.Log($"Parsing material property {name}");
|
||||||
|
|
||||||
|
T newProperty;
|
||||||
|
|
||||||
|
if (_materialProperties.ContainsKey(name)) {
|
||||||
|
if (_materialProperties[name] is T property) {
|
||||||
|
newProperty = property;
|
||||||
|
property.ParseNode(node);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new ArgumentException("Material property already exists for {name} but it has a different type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
newProperty = MaterialProperty.CreateInstance<T>();
|
||||||
|
Debug.Log($"Adding new material property of type {newProperty.GetType().Name} {newProperty.GetInstanceID()}");
|
||||||
|
newProperty.ParseNode(node);
|
||||||
|
_materialProperties.Add(name, newProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newProperty is MaterialTextureProperty textureProperty && textureProperty.isMain) {
|
||||||
|
_mainTexture = textureProperty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SetShader(string shaderName) {
|
public void SetShader(string shaderName) {
|
||||||
if (string.IsNullOrEmpty(shaderName)) {
|
if (string.IsNullOrEmpty(shaderName)) {
|
||||||
if (_shader == null) {
|
if (_shader == null) {
|
||||||
@ -107,9 +160,12 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
DecalMaterial.renderQueue = queue;
|
DecalMaterial.renderQueue = queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetScale(Vector2 scale) {
|
public void UpdateScale(Vector2 scale) {
|
||||||
foreach (var textureProperty in _textureMaterialProperties) {
|
foreach (var entry in _materialProperties) {
|
||||||
|
if (entry.Value is MaterialTextureProperty textureProperty) {
|
||||||
textureProperty.UpdateScale(DecalMaterial, scale);
|
textureProperty.UpdateScale(DecalMaterial, scale);
|
||||||
|
textureProperty.UpdateScale(PreviewMaterial, scale);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,20 +181,23 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
if (_decalMaterial == null) {
|
if (_decalMaterial == null) {
|
||||||
_decalMaterial = DecalMaterial;
|
_decalMaterial = DecalMaterial;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
UpdateMaterial(_decalMaterial);
|
||||||
|
}
|
||||||
|
|
||||||
if (_previewMaterial == null) {
|
if (_previewMaterial == null) {
|
||||||
_previewMaterial = PreviewMaterial;
|
_previewMaterial = PreviewMaterial;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
UpdateMaterial(_decalMaterial);
|
|
||||||
UpdateMaterial(_previewMaterial);
|
UpdateMaterial(_previewMaterial);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateMaterial(Material material) {
|
public void UpdateMaterial(Material material) {
|
||||||
if (material == null) throw new ArgumentNullException("material cannot be null");
|
if (material == null) throw new ArgumentNullException(nameof(material));
|
||||||
|
|
||||||
foreach (var property in _materialProperties) {
|
foreach (var entry in _materialProperties) {
|
||||||
property.Modify(material);
|
entry.Value.Modify(material);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,16 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
|
|
||||||
public float AspectRatio {
|
public float AspectRatio {
|
||||||
get {
|
get {
|
||||||
if (texture == null) return 1;
|
if (texture == null) {
|
||||||
if (!_hasTile || Mathf.Approximately(0, _tileRect.width)) return ((float) texture.height) / ((float) texture.width);
|
Debug.Log("Returning 1");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_hasTile) {
|
||||||
|
Debug.Log("Returning texture aspect ratio");
|
||||||
|
return ((float) texture.height) / ((float) texture.width);
|
||||||
|
}
|
||||||
|
|
||||||
return _tileRect.height / _tileRect.width;
|
return _tileRect.height / _tileRect.width;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,7 +43,7 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
public override void ParseNode(ConfigNode node) {
|
public override void ParseNode(ConfigNode node) {
|
||||||
base.ParseNode(node);
|
base.ParseNode(node);
|
||||||
|
|
||||||
isNormal = ParsePropertyBool(node, "isNormalMap", true, (Name == "_BumpMap") || isNormal);
|
isNormal = ParsePropertyBool(node, "isNormalMap", true, (PropertyName == "_BumpMap") || isNormal);
|
||||||
isMain = ParsePropertyBool(node, "isMain", true, isMain);
|
isMain = ParsePropertyBool(node, "isMain", true, isMain);
|
||||||
autoScale = ParsePropertyBool(node, "autoScale", true, autoScale);
|
autoScale = ParsePropertyBool(node, "autoScale", true, autoScale);
|
||||||
|
|
||||||
|
@ -115,21 +115,27 @@ namespace ConformalDecals {
|
|||||||
if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'.");
|
if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get back material if necessary
|
||||||
if (HighLogic.LoadedSceneIsEditor) {
|
if (updateBackScale) {
|
||||||
UpdateTweakables();
|
this.Log("Getting material and base scale for back material");
|
||||||
}
|
var backRenderer = decalBackTransform.GetComponent<MeshRenderer>();
|
||||||
|
if (backRenderer == null) {
|
||||||
// setup material properties
|
this.LogError($"Specified decalBack transform {decalBack} has no renderer attached! Setting updateBackScale to false.");
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
updateBackScale = false;
|
||||||
// additional load, in flight or in the editor
|
|
||||||
materialProperties = ScriptableObject.Instantiate(materialProperties);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// first load, so get everything set up
|
backMaterial = backRenderer.material;
|
||||||
materialProperties = ScriptableObject.CreateInstance<MaterialPropertyCollection>();
|
if (backMaterial == null) {
|
||||||
materialProperties.Initialize();
|
this.LogError($"Specified decalBack transform {decalBack} has a renderer but no material! Setting updateBackScale to false.");
|
||||||
|
updateBackScale = false;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if (backTextureBaseScale == default) backTextureBaseScale = backMaterial.GetTextureScale(PropertyIDs._MainTex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ConformalDecalIconFixer.QueuePart(part.name);
|
||||||
|
|
||||||
// set shader
|
// set shader
|
||||||
materialProperties.SetShader(shader);
|
materialProperties.SetShader(shader);
|
||||||
@ -137,6 +143,23 @@ namespace ConformalDecals {
|
|||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
this.LogException("Exception parsing partmodule", e);
|
this.LogException("Exception parsing partmodule", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
|
UpdateMaterials();
|
||||||
|
UpdateScale();
|
||||||
|
UpdateProjection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnAwake() {
|
||||||
|
base.OnAwake();
|
||||||
|
|
||||||
|
if (materialProperties == null) {
|
||||||
|
materialProperties = ScriptableObject.CreateInstance<MaterialPropertyCollection>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
materialProperties = ScriptableObject.Instantiate(materialProperties);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnStart(StartState state) {
|
public override void OnStart(StartState state) {
|
||||||
@ -150,30 +173,32 @@ namespace ConformalDecals {
|
|||||||
UpdateTweakables();
|
UpdateTweakables();
|
||||||
}
|
}
|
||||||
|
|
||||||
// clone materialProperties and setup queue
|
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
|
||||||
materialProperties = ScriptableObject.Instantiate(materialProperties);
|
|
||||||
materialProperties.SetRenderQueue(DecalQueue);
|
materialProperties.SetRenderQueue(DecalQueue);
|
||||||
|
|
||||||
|
UpdateMaterials();
|
||||||
|
UpdateScale();
|
||||||
|
|
||||||
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
// set initial attachment state
|
// set initial attachment state
|
||||||
if (part.parent == null) {
|
if (part.parent == null) {
|
||||||
_isAttached = false;
|
OnDetach();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
OnAttach();
|
OnAttach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateMaterials();
|
|
||||||
UpdateScale();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDestroy() {
|
public virtual void OnDestroy() {
|
||||||
|
// remove GameEvents
|
||||||
GameEvents.onEditorPartEvent.Remove(OnEditorEvent);
|
GameEvents.onEditorPartEvent.Remove(OnEditorEvent);
|
||||||
GameEvents.onVariantApplied.Remove(OnVariantApplied);
|
GameEvents.onVariantApplied.Remove(OnVariantApplied);
|
||||||
|
|
||||||
// remove from preCull delegate
|
// remove from preCull delegate
|
||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
|
|
||||||
|
// destroy material properties object
|
||||||
|
Destroy(materialProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void OnSizeTweakEvent(BaseField field, object obj) {
|
protected void OnSizeTweakEvent(BaseField field, object obj) {
|
||||||
@ -229,7 +254,7 @@ namespace ConformalDecals {
|
|||||||
// add to preCull delegate
|
// add to preCull delegate
|
||||||
Camera.onPreCull += Render;
|
Camera.onPreCull += Render;
|
||||||
|
|
||||||
//UpdateScale();
|
UpdateScale();
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
UpdateProjection();
|
UpdateProjection();
|
||||||
}
|
}
|
||||||
@ -244,13 +269,13 @@ namespace ConformalDecals {
|
|||||||
// remove from preCull delegate
|
// remove from preCull delegate
|
||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
|
|
||||||
//UpdateScale();
|
UpdateScale();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateScale() {
|
protected void UpdateScale() {
|
||||||
var aspectRatio = materialProperties.AspectRatio;
|
var aspectRatio = materialProperties.AspectRatio;
|
||||||
this.Log($"Aspect ratio is {aspectRatio}");
|
this.Log($"Aspect ratio is {aspectRatio}");
|
||||||
var size = new Vector2(scale, scale * materialProperties.AspectRatio);
|
var size = new Vector2(scale, scale * aspectRatio);
|
||||||
|
|
||||||
// update orthogonal matrix scale
|
// update orthogonal matrix scale
|
||||||
_orthoMatrix = Matrix4x4.identity;
|
_orthoMatrix = Matrix4x4.identity;
|
||||||
@ -278,7 +303,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update material scale
|
// update material scale
|
||||||
materialProperties.SetScale(size);
|
materialProperties.UpdateScale(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateMaterials() {
|
protected void UpdateMaterials() {
|
||||||
@ -286,26 +311,6 @@ namespace ConformalDecals {
|
|||||||
_decalMaterial = materialProperties.DecalMaterial;
|
_decalMaterial = materialProperties.DecalMaterial;
|
||||||
_previewMaterial = materialProperties.PreviewMaterial;
|
_previewMaterial = materialProperties.PreviewMaterial;
|
||||||
|
|
||||||
// get back material if necessary
|
|
||||||
if (updateBackScale) {
|
|
||||||
this.Log("Getting material and base scale for back material");
|
|
||||||
var backRenderer = decalBackTransform.GetComponent<MeshRenderer>();
|
|
||||||
if (backRenderer == null) {
|
|
||||||
this.LogError($"Specified decalBack transform {decalBack} has no renderer attached! Setting updateBackScale to false.");
|
|
||||||
updateBackScale = false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
backMaterial = backRenderer.material;
|
|
||||||
if (backMaterial == null) {
|
|
||||||
this.LogError($"Specified decalBack transform {decalBack} has a renderer but no material! Setting updateBackScale to false.");
|
|
||||||
updateBackScale = false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (backTextureBaseScale == default) backTextureBaseScale = backMaterial.GetTextureScale(PropertyIDs._MainTex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (decalFrontTransform == null) {
|
if (decalFrontTransform == null) {
|
||||||
this.LogError("No reference to decal front transform");
|
this.LogError("No reference to decal front transform");
|
||||||
return;
|
return;
|
||||||
@ -340,22 +345,16 @@ namespace ConformalDecals {
|
|||||||
if (renderer.gameObject.activeInHierarchy == false) continue;
|
if (renderer.gameObject.activeInHierarchy == false) continue;
|
||||||
|
|
||||||
// skip blacklisted shaders
|
// skip blacklisted shaders
|
||||||
if (ConformalDecalConfig.IsBlacklisted(renderer.material.shader)) {
|
if (ConformalDecalConfig.IsBlacklisted(renderer.material.shader)) continue;
|
||||||
this.Log($"Skipping blacklisted shader '{renderer.material.shader.name}' in '{renderer.gameObject.name}'");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var meshFilter = renderer.GetComponent<MeshFilter>();
|
var meshFilter = renderer.GetComponent<MeshFilter>();
|
||||||
if (meshFilter == null) continue; // object has a meshRenderer with no filter, invalid
|
if (meshFilter == null) continue; // object has a meshRenderer with no filter, invalid
|
||||||
var mesh = meshFilter.mesh;
|
var mesh = meshFilter.mesh;
|
||||||
if (mesh == null) continue; // object has a null mesh, invalid
|
if (mesh == null) continue; // object has a null mesh, invalid
|
||||||
|
|
||||||
this.Log($"Adding target for object {meshFilter.gameObject.name} with the mesh {mesh.name}");
|
|
||||||
// create new ProjectionTarget to represent the renderer
|
// create new ProjectionTarget to represent the renderer
|
||||||
var target = new ProjectionTarget(renderer, mesh, useBaseNormal);
|
var target = new ProjectionTarget(renderer, mesh, useBaseNormal);
|
||||||
|
|
||||||
this.Log("done.");
|
|
||||||
|
|
||||||
// add the target to the list
|
// add the target to the list
|
||||||
_targets.Add(target);
|
_targets.Add(target);
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,11 @@ namespace ConformalDecals {
|
|||||||
public override void OnLoad(ConfigNode node) {
|
public override void OnLoad(ConfigNode node) {
|
||||||
base.OnLoad(node);
|
base.OnLoad(node);
|
||||||
|
|
||||||
|
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
UpdateMaterials();
|
UpdateFlag(EditorLogic.FlagURL != string.Empty ? EditorLogic.FlagURL : HighLogic.CurrentGame.flagURL);
|
||||||
UpdateScale();
|
}
|
||||||
UpdateProjection();
|
else {
|
||||||
|
UpdateFlag(defaultFlag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,20 +23,20 @@ namespace ConformalDecals {
|
|||||||
base.OnStart(state);
|
base.OnStart(state);
|
||||||
|
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
UpdateFlag(EditorLogic.FlagURL != string.Empty ? EditorLogic.FlagURL : HighLogic.CurrentGame.flagURL);
|
|
||||||
GameEvents.onMissionFlagSelect.Add(UpdateFlag);
|
GameEvents.onMissionFlagSelect.Add(UpdateFlag);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
UpdateFlag(defaultFlag);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnIconCreate() {
|
public override void OnIconCreate() {
|
||||||
this.Log("called OnIconCreate");
|
this.Log("called OnIconCreate");
|
||||||
OnStart(StartState.None);
|
|
||||||
UpdateScale();
|
UpdateScale();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void OnDestroy() {
|
||||||
|
GameEvents.onMissionFlagSelect.Remove(UpdateFlag);
|
||||||
|
base.OnDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
private void UpdateFlag(string flagUrl) {
|
private void UpdateFlag(string flagUrl) {
|
||||||
this.Log($"Loading flag texture '{flagUrl}'.");
|
this.Log($"Loading flag texture '{flagUrl}'.");
|
||||||
var flagTexture = GameDatabase.Instance.GetTexture(flagUrl, false);
|
var flagTexture = GameDatabase.Instance.GetTexture(flagUrl, false);
|
||||||
@ -48,9 +48,10 @@ namespace ConformalDecals {
|
|||||||
if (flagTextureProperty == null) {
|
if (flagTextureProperty == null) {
|
||||||
this.Log("Initializing flag property");
|
this.Log("Initializing flag property");
|
||||||
flagTextureProperty = ScriptableObject.CreateInstance<MaterialTextureProperty>();
|
flagTextureProperty = ScriptableObject.CreateInstance<MaterialTextureProperty>();
|
||||||
flagTextureProperty.Name = "_Decal";
|
flagTextureProperty.PropertyName = "_Decal";
|
||||||
flagTextureProperty.isMain = true;
|
flagTextureProperty.isMain = true;
|
||||||
materialProperties.AddProperty(flagTextureProperty);
|
materialProperties.AddProperty(flagTextureProperty);
|
||||||
|
materialProperties.MainTexture = flagTextureProperty;
|
||||||
}
|
}
|
||||||
else { }
|
else { }
|
||||||
|
|
||||||
|
@ -5,41 +5,29 @@ using UnityEngine;
|
|||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
public class ModuleConformalDecalGeneric : ModuleConformalDecalBase {
|
public class ModuleConformalDecalGeneric : ModuleConformalDecalBase {
|
||||||
public override void OnLoad(ConfigNode node) {
|
public override void OnLoad(ConfigNode node) {
|
||||||
base.OnLoad(node);
|
|
||||||
|
|
||||||
// set shader
|
// set shader
|
||||||
materialProperties.SetShader(shader);
|
materialProperties.SetShader(shader);
|
||||||
// add texture nodes
|
// add texture nodes
|
||||||
foreach (var textureNode in node.GetNodes("TEXTURE")) {
|
foreach (var textureNode in node.GetNodes("TEXTURE")) {
|
||||||
var textureProperty = ScriptableObject.CreateInstance<MaterialTextureProperty>();
|
materialProperties.ParseProperty<MaterialTextureProperty>(textureNode);
|
||||||
textureProperty.ParseNode(textureNode);
|
|
||||||
materialProperties.AddProperty(textureProperty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add float nodes
|
// add float nodes
|
||||||
foreach (var floatNode in node.GetNodes("FLOAT")) {
|
foreach (var floatNode in node.GetNodes("FLOAT")) {
|
||||||
var floatProperty = ScriptableObject.CreateInstance<MaterialFloatProperty>();
|
materialProperties.ParseProperty<MaterialTextureProperty>(floatNode);
|
||||||
floatProperty.ParseNode(floatNode);
|
|
||||||
materialProperties.AddProperty(floatProperty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add color nodes
|
// add color nodes
|
||||||
foreach (var colorNode in node.GetNodes("COLOR")) {
|
foreach (var colorNode in node.GetNodes("COLOR")) {
|
||||||
var colorProperty = ScriptableObject.CreateInstance<MaterialColorProperty>();
|
materialProperties.ParseProperty<MaterialColorProperty>(colorNode);
|
||||||
colorProperty.ParseNode(colorNode);
|
|
||||||
materialProperties.AddProperty(colorProperty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
base.OnLoad(node);
|
||||||
UpdateMaterials();
|
|
||||||
UpdateScale();
|
|
||||||
UpdateProjection();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnIconCreate() {
|
public override void OnIconCreate() {
|
||||||
this.Log("called OnIconCreate");
|
this.Log("called OnIconCreate");
|
||||||
OnStart(StartState.None);
|
|
||||||
UpdateScale();
|
UpdateScale();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user