mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Cleanup refactor
This commit is contained in:
parent
e37cf03f7b
commit
a61a2b81a1
Binary file not shown.
@ -105,6 +105,28 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
_materialProperties ??= new Dictionary<string, MaterialProperty>();
|
_materialProperties ??= new Dictionary<string, MaterialProperty>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Load(ConfigNode node) {
|
||||||
|
// add keyword nodes
|
||||||
|
foreach (var keywordNode in node.GetNodes("KEYWORD")) {
|
||||||
|
ParseProperty<MaterialKeywordProperty>(keywordNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add texture nodes
|
||||||
|
foreach (var textureNode in node.GetNodes("TEXTURE")) {
|
||||||
|
ParseProperty<MaterialTextureProperty>(textureNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add float nodes
|
||||||
|
foreach (var floatNode in node.GetNodes("FLOAT")) {
|
||||||
|
ParseProperty<MaterialTextureProperty>(floatNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add color nodes
|
||||||
|
foreach (var colorNode in node.GetNodes("COLOR")) {
|
||||||
|
ParseProperty<MaterialColorProperty>(colorNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void OnDestroy() {
|
public void OnDestroy() {
|
||||||
if (_decalMaterial != null) Destroy(_decalMaterial);
|
if (_decalMaterial != null) Destroy(_decalMaterial);
|
||||||
if (_previewMaterial != null) Destroy(_previewMaterial);
|
if (_previewMaterial != null) Destroy(_previewMaterial);
|
||||||
|
@ -44,7 +44,7 @@ namespace ConformalDecals.MaterialProperties {
|
|||||||
public float AspectRatio {
|
public float AspectRatio {
|
||||||
get {
|
get {
|
||||||
if (_texture == null) return 1;
|
if (_texture == null) return 1;
|
||||||
if (_textureUrl?.Contains("Squad/Flags") == true) return 0.625f;
|
if (_textureUrl?.Contains("Squad/Flags") == true) return 0.625f; // squad flags are slightly stretched, so unstretch them
|
||||||
return MaskedHeight / (float) MaskedWidth;
|
return MaskedHeight / (float) MaskedWidth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,8 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EVENTS
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnAwake() {
|
public override void OnAwake() {
|
||||||
base.OnAwake();
|
base.OnAwake();
|
||||||
@ -133,116 +135,20 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnLoad(ConfigNode node) {
|
public override void OnLoad(ConfigNode node) {
|
||||||
|
// Load
|
||||||
try {
|
try {
|
||||||
// SETUP TRANSFORMS
|
LoadDecal(node);
|
||||||
decalFrontTransform = part.FindModelTransform(decalFront);
|
|
||||||
if (decalFrontTransform == null) throw new FormatException($"Could not find decalFront transform: '{decalFront}'.");
|
|
||||||
|
|
||||||
decalBackTransform = part.FindModelTransform(decalBack);
|
|
||||||
if (decalBackTransform == null) throw new FormatException($"Could not find decalBack transform: '{decalBack}'.");
|
|
||||||
|
|
||||||
decalModelTransform = part.FindModelTransform(decalModel);
|
|
||||||
if (decalModelTransform == null) throw new FormatException($"Could not find decalModel transform: '{decalModel}'.");
|
|
||||||
|
|
||||||
decalProjectorTransform = part.FindModelTransform(decalProjector);
|
|
||||||
if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'.");
|
|
||||||
|
|
||||||
decalColliderTransform = part.FindModelTransform(decalCollider);
|
|
||||||
if (decalColliderTransform == null) throw new FormatException($"Could not find decalCollider transform: '{decalCollider}'.");
|
|
||||||
|
|
||||||
// SETUP BACK MATERIAL
|
|
||||||
if (updateBackScale) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PARSE MATERIAL PROPERTIES
|
|
||||||
|
|
||||||
// set shader
|
|
||||||
materialProperties.SetShader(shader);
|
|
||||||
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_BASE_NORMAL").value = useBaseNormal;
|
|
||||||
|
|
||||||
// add keyword nodes
|
|
||||||
foreach (var keywordNode in node.GetNodes("KEYWORD")) {
|
|
||||||
materialProperties.ParseProperty<MaterialKeywordProperty>(keywordNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add texture nodes
|
|
||||||
foreach (var textureNode in node.GetNodes("TEXTURE")) {
|
|
||||||
materialProperties.ParseProperty<MaterialTextureProperty>(textureNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add float nodes
|
|
||||||
foreach (var floatNode in node.GetNodes("FLOAT")) {
|
|
||||||
materialProperties.ParseProperty<MaterialTextureProperty>(floatNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add color nodes
|
|
||||||
foreach (var colorNode in node.GetNodes("COLOR")) {
|
|
||||||
materialProperties.ParseProperty<MaterialColorProperty>(colorNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle texture tiling parameters
|
|
||||||
var tileString = node.GetValue("tile");
|
|
||||||
if (!string.IsNullOrEmpty(tileString)) {
|
|
||||||
var tileValid = ParseExtensions.TryParseRect(tileString, out tileRect);
|
|
||||||
if (!tileValid) throw new FormatException($"Invalid rect value for tile '{tileString}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tileRect.x >= 0) {
|
|
||||||
materialProperties.UpdateTile(tileRect);
|
|
||||||
}
|
|
||||||
else if (tileIndex >= 0) {
|
|
||||||
materialProperties.UpdateTile(tileIndex, tileSize);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
this.LogException("Exception parsing partmodule", e);
|
this.LogException("Error loading decal", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateMaterials();
|
// Setup
|
||||||
|
try {
|
||||||
foreach (var keyword in _decalMaterial.shaderKeywords) {
|
SetupDecal();
|
||||||
this.Log($"keyword: {keyword}");
|
|
||||||
}
|
}
|
||||||
|
catch (Exception e) {
|
||||||
if (HighLogic.LoadedSceneIsEditor) {
|
this.LogException("Error setting up decal", e);
|
||||||
UpdateTweakables();
|
|
||||||
UpdateTextures();
|
|
||||||
UpdateScale();
|
|
||||||
UpdateTargets();
|
|
||||||
}
|
|
||||||
else if (HighLogic.LoadedSceneIsFlight) {
|
|
||||||
UpdateTextures();
|
|
||||||
UpdateScale();
|
|
||||||
UpdateTargets();
|
|
||||||
//TODO: Target loading
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
scale = defaultScale;
|
|
||||||
depth = defaultDepth;
|
|
||||||
opacity = defaultOpacity;
|
|
||||||
cutoff = defaultCutoff;
|
|
||||||
wear = defaultWear;
|
|
||||||
|
|
||||||
UpdateTextures();
|
|
||||||
UpdateScale();
|
|
||||||
|
|
||||||
// QUEUE PART FOR ICON FIXING IN VAB
|
|
||||||
DecalIconFixer.QueuePart(part.name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,6 +173,8 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called after OnStart is finished for all parts
|
||||||
|
/// This is mostly used to make sure all B9 variants are already in place for the rest of the vessel
|
||||||
public override void OnStartFinished(StartState state) {
|
public override void OnStartFinished(StartState state) {
|
||||||
// handle game events
|
// handle game events
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
@ -295,6 +203,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when the decal is destroyed
|
||||||
public virtual void OnDestroy() {
|
public virtual void OnDestroy() {
|
||||||
// remove GameEvents
|
// remove GameEvents
|
||||||
if (HighLogic.LoadedSceneIsEditor) {
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
@ -313,6 +222,7 @@ namespace ConformalDecals {
|
|||||||
Destroy(materialProperties);
|
Destroy(materialProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when the decal's projection and scale is modified through a tweakable
|
||||||
protected void OnProjectionTweakEvent(BaseField field, object obj) {
|
protected void OnProjectionTweakEvent(BaseField field, object obj) {
|
||||||
// scale or depth values have been changed, so update scale
|
// scale or depth values have been changed, so update scale
|
||||||
// and update projection matrices if attached
|
// and update projection matrices if attached
|
||||||
@ -326,6 +236,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when the decal's material is modified through a tweakable
|
||||||
protected void OnMaterialTweakEvent(BaseField field, object obj) {
|
protected void OnMaterialTweakEvent(BaseField field, object obj) {
|
||||||
materialProperties.SetOpacity(opacity);
|
materialProperties.SetOpacity(opacity);
|
||||||
materialProperties.SetCutoff(cutoff);
|
materialProperties.SetCutoff(cutoff);
|
||||||
@ -343,6 +254,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when a new variant is applied in the editor
|
||||||
protected void OnVariantApplied(Part eventPart, PartVariant variant) {
|
protected void OnVariantApplied(Part eventPart, PartVariant variant) {
|
||||||
if (_isAttached && eventPart != null) {
|
if (_isAttached && eventPart != null) {
|
||||||
if (projectMultiple && eventPart != part.parent) return;
|
if (projectMultiple && eventPart != part.parent) return;
|
||||||
@ -352,6 +264,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when an editor event occurs
|
||||||
protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
protected void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
||||||
if (this.part != eventPart && !part.symmetryCounterparts.Contains(eventPart)) return;
|
if (this.part != eventPart && !part.symmetryCounterparts.Contains(eventPart)) return;
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
@ -369,6 +282,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when part `willDie` will be destroyed
|
||||||
protected void OnPartWillDie(Part willDie) {
|
protected void OnPartWillDie(Part willDie) {
|
||||||
if (willDie == part.parent) {
|
if (willDie == part.parent) {
|
||||||
this.Log("Parent part about to be destroyed! Killing decal part.");
|
this.Log("Parent part about to be destroyed! Killing decal part.");
|
||||||
@ -376,6 +290,7 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when decal is attached to a new part
|
||||||
protected virtual void OnAttach() {
|
protected virtual void OnAttach() {
|
||||||
if (part.parent == null) {
|
if (part.parent == null) {
|
||||||
this.LogError("Attach function called but part has no parent!");
|
this.LogError("Attach function called but part has no parent!");
|
||||||
@ -399,6 +314,7 @@ namespace ConformalDecals {
|
|||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called when decal is detached from its parent part
|
||||||
protected virtual void OnDetach() {
|
protected virtual void OnDetach() {
|
||||||
_isAttached = false;
|
_isAttached = false;
|
||||||
|
|
||||||
@ -415,117 +331,91 @@ namespace ConformalDecals {
|
|||||||
UpdateScale();
|
UpdateScale();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateScale() {
|
// FUNCTIONS
|
||||||
|
|
||||||
|
/// Load any settings from the decal config
|
||||||
|
protected virtual void LoadDecal(ConfigNode node) {
|
||||||
|
// PARSE TRANSFORMS
|
||||||
|
decalFrontTransform = part.FindModelTransform(decalFront);
|
||||||
|
if (decalFrontTransform == null) throw new FormatException($"Could not find decalFront transform: '{decalFront}'.");
|
||||||
|
|
||||||
// Update scale and depth
|
decalBackTransform = part.FindModelTransform(decalBack);
|
||||||
scale = Mathf.Max(0.01f, scale);
|
if (decalBackTransform == null) throw new FormatException($"Could not find decalBack transform: '{decalBack}'.");
|
||||||
depth = Mathf.Max(0.01f, depth);
|
|
||||||
var aspectRatio = Mathf.Max(0.01f, materialProperties.AspectRatio);
|
|
||||||
Vector2 size;
|
|
||||||
|
|
||||||
switch (scaleMode) {
|
decalModelTransform = part.FindModelTransform(decalModel);
|
||||||
default:
|
if (decalModelTransform == null) throw new FormatException($"Could not find decalModel transform: '{decalModel}'.");
|
||||||
case DecalScaleMode.HEIGHT:
|
|
||||||
size = new Vector2(scale / aspectRatio, scale);
|
|
||||||
break;
|
|
||||||
case DecalScaleMode.WIDTH:
|
|
||||||
size = new Vector2(scale, scale * aspectRatio);
|
|
||||||
break;
|
|
||||||
case DecalScaleMode.AVERAGE:
|
|
||||||
var width1 = 2 * scale / (1 + aspectRatio);
|
|
||||||
size = new Vector2(width1, width1 * aspectRatio);
|
|
||||||
break;
|
|
||||||
case DecalScaleMode.AREA:
|
|
||||||
var width2 = Mathf.Sqrt(scale / aspectRatio);
|
|
||||||
size = new Vector2(width2, width2 * aspectRatio);
|
|
||||||
break;
|
|
||||||
case DecalScaleMode.MINIMUM:
|
|
||||||
if (aspectRatio > 1) goto case DecalScaleMode.WIDTH;
|
|
||||||
else goto case DecalScaleMode.HEIGHT;
|
|
||||||
case DecalScaleMode.MAXIMUM:
|
|
||||||
if (aspectRatio > 1) goto case DecalScaleMode.HEIGHT;
|
|
||||||
else goto case DecalScaleMode.WIDTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
// update material scale
|
decalProjectorTransform = part.FindModelTransform(decalProjector);
|
||||||
materialProperties.UpdateScale(size);
|
if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'.");
|
||||||
|
|
||||||
if (_isAttached) {
|
decalColliderTransform = part.FindModelTransform(decalCollider);
|
||||||
// Update projection targets
|
if (decalColliderTransform == null) throw new FormatException($"Could not find decalCollider transform: '{decalCollider}'.");
|
||||||
if (_targets == null) {
|
|
||||||
_targets = new List<ProjectionTarget>();
|
// SETUP BACK MATERIAL
|
||||||
|
if (updateBackScale) {
|
||||||
|
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 {
|
else {
|
||||||
_targets.Clear();
|
backMaterial = backRenderer.material;
|
||||||
}
|
if (backMaterial == null) {
|
||||||
|
this.LogError($"Specified decalBack transform {decalBack} has a renderer but no material! Setting updateBackScale to false.");
|
||||||
// update orthogonal matrix
|
updateBackScale = false;
|
||||||
_orthoMatrix = Matrix4x4.identity;
|
}
|
||||||
_orthoMatrix[0, 3] = 0.5f;
|
else {
|
||||||
_orthoMatrix[1, 3] = 0.5f;
|
if (backTextureBaseScale == default) backTextureBaseScale = backMaterial.GetTextureScale(PropertyIDs._MainTex);
|
||||||
|
|
||||||
decalProjectorTransform.localScale = new Vector3(size.x, size.y, depth);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// rescale preview model
|
|
||||||
decalModelTransform.localScale = new Vector3(size.x, size.y, (size.x + size.y) / 2);
|
|
||||||
|
|
||||||
// update back material scale
|
|
||||||
if (updateBackScale) {
|
|
||||||
backMaterial.SetTextureScale(PropertyIDs._MainTex, new Vector2(size.x * backTextureBaseScale.x, size.y * backTextureBaseScale.y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void UpdateTargets() {
|
|
||||||
if (!_isAttached) return;
|
|
||||||
|
|
||||||
IEnumerable<Part> targetParts;
|
|
||||||
if (projectMultiple) {
|
|
||||||
targetParts = HighLogic.LoadedSceneIsFlight ? part.vessel.parts : EditorLogic.fetch.ship.parts;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
targetParts = new[] {part.parent};
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var targetPart in targetParts) {
|
|
||||||
if (targetPart.GetComponent<ModuleConformalDecal>() != null) continue; // skip other decals
|
|
||||||
|
|
||||||
foreach (var renderer in targetPart.FindModelComponents<MeshRenderer>()) {
|
|
||||||
var target = renderer.transform;
|
|
||||||
var filter = target.GetComponent<MeshFilter>();
|
|
||||||
|
|
||||||
// check if the target has any missing data
|
|
||||||
if (!ProjectionTarget.ValidateTarget(target, renderer, filter)) continue;
|
|
||||||
|
|
||||||
// check bounds for intersection
|
|
||||||
if (_boundsRenderer.bounds.Intersects(renderer.bounds)) {
|
|
||||||
// create new ProjectionTarget to represent the renderer
|
|
||||||
var projectionTarget = new ProjectionTarget(targetPart, target, renderer, filter, _orthoMatrix, decalProjectorTransform, useBaseNormal);
|
|
||||||
|
|
||||||
// add the target to the list
|
|
||||||
_targets.Add(projectionTarget);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void UpdateTextures() { }
|
// PARSE MATERIAL PROPERTIES
|
||||||
|
// set shader
|
||||||
|
materialProperties.SetShader(shader);
|
||||||
|
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_BASE_NORMAL").value = useBaseNormal;
|
||||||
|
materialProperties.Load(node);
|
||||||
|
|
||||||
protected virtual void UpdateMaterials() {
|
// handle texture tiling parameters
|
||||||
materialProperties.UpdateMaterials();
|
var tileString = node.GetValue("tile");
|
||||||
materialProperties.SetOpacity(opacity);
|
if (!string.IsNullOrEmpty(tileString)) {
|
||||||
materialProperties.SetCutoff(cutoff);
|
var tileValid = ParseExtensions.TryParseRect(tileString, out tileRect);
|
||||||
if (useBaseNormal) {
|
if (!tileValid) throw new FormatException($"Invalid rect value for tile '{tileString}'");
|
||||||
materialProperties.SetWear(wear);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_decalMaterial = materialProperties.DecalMaterial;
|
if (tileRect.x >= 0) {
|
||||||
_previewMaterial = materialProperties.PreviewMaterial;
|
materialProperties.UpdateTile(tileRect);
|
||||||
|
}
|
||||||
if (!_isAttached) decalFrontTransform.GetComponent<MeshRenderer>().material = _previewMaterial;
|
else if (tileIndex >= 0) {
|
||||||
|
materialProperties.UpdateTile(tileIndex, tileSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Setup decal by calling update functions relevent for the current situation
|
||||||
|
protected virtual void SetupDecal() {
|
||||||
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
|
// Update tweakables in editor mode
|
||||||
|
UpdateTweakables();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
|
UpdateAll();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
scale = defaultScale;
|
||||||
|
depth = defaultDepth;
|
||||||
|
opacity = defaultOpacity;
|
||||||
|
cutoff = defaultCutoff;
|
||||||
|
wear = defaultWear;
|
||||||
|
|
||||||
|
UpdateAll();
|
||||||
|
|
||||||
|
// QUEUE PART FOR ICON FIXING IN VAB
|
||||||
|
DecalIconFixer.QueuePart(part.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update decal editor tweakables
|
||||||
protected virtual void UpdateTweakables() {
|
protected virtual void UpdateTweakables() {
|
||||||
// setup tweakable fields
|
// setup tweakable fields
|
||||||
var scaleField = Fields[nameof(scale)];
|
var scaleField = Fields[nameof(scale)];
|
||||||
@ -604,6 +494,130 @@ namespace ConformalDecals {
|
|||||||
multiprojectEditor.onFieldChanged = OnProjectionTweakEvent;
|
multiprojectEditor.onFieldChanged = OnProjectionTweakEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates textures, materials, scale and targets
|
||||||
|
protected virtual void UpdateAll() {
|
||||||
|
UpdateTextures();
|
||||||
|
UpdateMaterials();
|
||||||
|
UpdateScale();
|
||||||
|
UpdateTargets();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update decal textures
|
||||||
|
protected virtual void UpdateTextures() { }
|
||||||
|
|
||||||
|
/// Update decal materials
|
||||||
|
protected virtual void UpdateMaterials() {
|
||||||
|
materialProperties.UpdateMaterials();
|
||||||
|
materialProperties.SetOpacity(opacity);
|
||||||
|
materialProperties.SetCutoff(cutoff);
|
||||||
|
if (useBaseNormal) {
|
||||||
|
materialProperties.SetWear(wear);
|
||||||
|
}
|
||||||
|
|
||||||
|
_decalMaterial = materialProperties.DecalMaterial;
|
||||||
|
_previewMaterial = materialProperties.PreviewMaterial;
|
||||||
|
|
||||||
|
if (!_isAttached) decalFrontTransform.GetComponent<MeshRenderer>().material = _previewMaterial;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update decal scale and projection
|
||||||
|
protected void UpdateScale() {
|
||||||
|
|
||||||
|
// Update scale and depth
|
||||||
|
scale = Mathf.Max(0.01f, scale);
|
||||||
|
depth = Mathf.Max(0.01f, depth);
|
||||||
|
var aspectRatio = Mathf.Max(0.01f, materialProperties.AspectRatio);
|
||||||
|
Vector2 size;
|
||||||
|
|
||||||
|
switch (scaleMode) {
|
||||||
|
default:
|
||||||
|
case DecalScaleMode.HEIGHT:
|
||||||
|
size = new Vector2(scale / aspectRatio, scale);
|
||||||
|
break;
|
||||||
|
case DecalScaleMode.WIDTH:
|
||||||
|
size = new Vector2(scale, scale * aspectRatio);
|
||||||
|
break;
|
||||||
|
case DecalScaleMode.AVERAGE:
|
||||||
|
var width1 = 2 * scale / (1 + aspectRatio);
|
||||||
|
size = new Vector2(width1, width1 * aspectRatio);
|
||||||
|
break;
|
||||||
|
case DecalScaleMode.AREA:
|
||||||
|
var width2 = Mathf.Sqrt(scale / aspectRatio);
|
||||||
|
size = new Vector2(width2, width2 * aspectRatio);
|
||||||
|
break;
|
||||||
|
case DecalScaleMode.MINIMUM:
|
||||||
|
if (aspectRatio > 1) goto case DecalScaleMode.WIDTH;
|
||||||
|
else goto case DecalScaleMode.HEIGHT;
|
||||||
|
case DecalScaleMode.MAXIMUM:
|
||||||
|
if (aspectRatio > 1) goto case DecalScaleMode.HEIGHT;
|
||||||
|
else goto case DecalScaleMode.WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update material scale
|
||||||
|
materialProperties.UpdateScale(size);
|
||||||
|
|
||||||
|
if (_isAttached) {
|
||||||
|
// Update projection targets
|
||||||
|
if (_targets == null) {
|
||||||
|
_targets = new List<ProjectionTarget>();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_targets.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// update orthogonal matrix
|
||||||
|
_orthoMatrix = Matrix4x4.identity;
|
||||||
|
_orthoMatrix[0, 3] = 0.5f;
|
||||||
|
_orthoMatrix[1, 3] = 0.5f;
|
||||||
|
|
||||||
|
decalProjectorTransform.localScale = new Vector3(size.x, size.y, depth);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// rescale preview model
|
||||||
|
decalModelTransform.localScale = new Vector3(size.x, size.y, (size.x + size.y) / 2);
|
||||||
|
|
||||||
|
// update back material scale
|
||||||
|
if (updateBackScale) {
|
||||||
|
backMaterial.SetTextureScale(PropertyIDs._MainTex, new Vector2(size.x * backTextureBaseScale.x, size.y * backTextureBaseScale.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called when updating decal targets
|
||||||
|
protected void UpdateTargets() {
|
||||||
|
if (!_isAttached) return;
|
||||||
|
|
||||||
|
IEnumerable<Part> targetParts;
|
||||||
|
if (projectMultiple) {
|
||||||
|
targetParts = HighLogic.LoadedSceneIsFlight ? part.vessel.parts : EditorLogic.fetch.ship.parts;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
targetParts = new[] {part.parent};
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var targetPart in targetParts) {
|
||||||
|
if (targetPart.GetComponent<ModuleConformalDecal>() != null) continue; // skip other decals
|
||||||
|
|
||||||
|
foreach (var renderer in targetPart.FindModelComponents<MeshRenderer>()) {
|
||||||
|
var target = renderer.transform;
|
||||||
|
var filter = target.GetComponent<MeshFilter>();
|
||||||
|
|
||||||
|
// check if the target has any missing data
|
||||||
|
if (!ProjectionTarget.ValidateTarget(target, renderer, filter)) continue;
|
||||||
|
|
||||||
|
// check bounds for intersection
|
||||||
|
if (_boundsRenderer.bounds.Intersects(renderer.bounds)) {
|
||||||
|
// create new ProjectionTarget to represent the renderer
|
||||||
|
var projectionTarget = new ProjectionTarget(targetPart, target, renderer, filter, _orthoMatrix, decalProjectorTransform, useBaseNormal);
|
||||||
|
|
||||||
|
// add the target to the list
|
||||||
|
_targets.Add(projectionTarget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render the decal
|
||||||
public void Render(Camera camera) {
|
public void Render(Camera camera) {
|
||||||
if (!_isAttached) return;
|
if (!_isAttached) return;
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
using ConformalDecals.MaterialProperties;
|
||||||
using ConformalDecals.Util;
|
using ConformalDecals.Util;
|
||||||
|
using UniLinq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
@ -9,6 +11,8 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
[KSPField(isPersistant = true)] public bool useCustomFlag;
|
[KSPField(isPersistant = true)] public bool useCustomFlag;
|
||||||
|
|
||||||
|
private MaterialTextureProperty _flagTextureProperty;
|
||||||
|
|
||||||
public string MissionFlagUrl {
|
public string MissionFlagUrl {
|
||||||
get {
|
get {
|
||||||
if (HighLogic.LoadedSceneIsEditor) {
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
@ -23,15 +27,10 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnLoad(ConfigNode node) {
|
protected override void SetupDecal() {
|
||||||
base.OnLoad(node);
|
_flagTextureProperty = materialProperties.AddOrGetTextureProperty("_Decal");
|
||||||
|
|
||||||
if (useCustomFlag) {
|
base.SetupDecal();
|
||||||
SetFlag(flagUrl);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
SetFlag(MissionFlagUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnStart(StartState state) {
|
public override void OnStart(StartState state) {
|
||||||
@ -44,17 +43,10 @@ namespace ConformalDecals {
|
|||||||
if (HighLogic.LoadedSceneIsEditor) {
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag;
|
Events[nameof(ResetFlag)].guiActiveEditor = useCustomFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useCustomFlag) {
|
|
||||||
SetFlag(flagUrl);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
SetFlag(MissionFlagUrl);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnDestroy() {
|
public override void OnDestroy() {
|
||||||
GameEvents.onMissionFlagSelect.Remove(SetFlag);
|
GameEvents.onMissionFlagSelect.Remove(OnEditorFlagSelected);
|
||||||
base.OnDestroy();
|
base.OnDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,45 +58,43 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
[KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-reset-flag")]
|
||||||
public void ResetFlag() {
|
public void ResetFlag() {
|
||||||
SetFlag(MissionFlagUrl);
|
|
||||||
SetFlagSymmetryCounterparts(MissionFlagUrl);
|
|
||||||
|
|
||||||
useCustomFlag = false;
|
|
||||||
Events[nameof(ResetFlag)].guiActiveEditor = false;
|
Events[nameof(ResetFlag)].guiActiveEditor = false;
|
||||||
}
|
flagUrl = MissionFlagUrl;
|
||||||
|
useCustomFlag = false;
|
||||||
private void OnCustomFlagSelected(FlagBrowser.FlagEntry newFlagEntry) {
|
UpdateAll();
|
||||||
SetFlag(newFlagEntry.textureInfo.name);
|
foreach (var decal in part.symmetryCounterparts.Select(o => o.GetComponent<ModuleConformalFlag>())) {
|
||||||
SetFlagSymmetryCounterparts(newFlagEntry.textureInfo.name);
|
decal.Events[nameof(ResetFlag)].guiActiveEditor = false;
|
||||||
|
decal.flagUrl = flagUrl;
|
||||||
useCustomFlag = true;
|
decal.useCustomFlag = false;
|
||||||
Events[nameof(ResetFlag)].guiActiveEditor = true;
|
decal.UpdateAll();
|
||||||
}
|
|
||||||
|
|
||||||
private void OnEditorFlagSelected(string newFlagUrl) {
|
|
||||||
if (!useCustomFlag) {
|
|
||||||
SetFlag(newFlagUrl);
|
|
||||||
SetFlagSymmetryCounterparts(newFlagUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetFlag(string newFlagUrl) {
|
private void OnCustomFlagSelected(FlagBrowser.FlagEntry newFlagEntry) {
|
||||||
this.Log($"Loading flag texture '{newFlagUrl}'.");
|
Events[nameof(ResetFlag)].guiActiveEditor = true;
|
||||||
|
flagUrl = newFlagEntry.textureInfo.name;
|
||||||
|
useCustomFlag = true;
|
||||||
|
UpdateAll();
|
||||||
|
|
||||||
flagUrl = newFlagUrl;
|
foreach (var decal in part.symmetryCounterparts.Select(o => o.GetComponent<ModuleConformalFlag>())) {
|
||||||
materialProperties.AddOrGetTextureProperty("_Decal", true).TextureUrl = newFlagUrl;
|
decal.Events[nameof(ResetFlag)].guiActiveEditor = true;
|
||||||
|
decal.flagUrl = flagUrl;
|
||||||
UpdateMaterials();
|
decal.useCustomFlag = true;
|
||||||
UpdateScale();
|
decal.UpdateAll();
|
||||||
UpdateTargets();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetFlagSymmetryCounterparts(string newFlagUrl) {
|
private void OnEditorFlagSelected(string newFlagUrl) {
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
if (!useCustomFlag) UpdateAll();
|
||||||
var decal = counterpart.GetComponent<ModuleConformalFlag>();
|
}
|
||||||
|
|
||||||
decal.SetFlag(newFlagUrl);
|
protected override void UpdateTextures() {
|
||||||
decal.useCustomFlag = useCustomFlag;
|
base.UpdateTextures();
|
||||||
|
if (useCustomFlag) {
|
||||||
|
_flagTextureProperty.TextureUrl = flagUrl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_flagTextureProperty.TextureUrl = MissionFlagUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,43 +90,9 @@ namespace ConformalDecals {
|
|||||||
private MaterialColorProperty _outlineColorProperty;
|
private MaterialColorProperty _outlineColorProperty;
|
||||||
private MaterialFloatProperty _outlineWidthProperty;
|
private MaterialFloatProperty _outlineWidthProperty;
|
||||||
|
|
||||||
private DecalText _currentText;
|
private DecalText _currentText;
|
||||||
|
|
||||||
public override void OnLoad(ConfigNode node) {
|
// EVENTS
|
||||||
base.OnLoad(node);
|
|
||||||
|
|
||||||
string textRaw = "";
|
|
||||||
if (ParseUtil.ParseStringIndirect(ref textRaw, node, "text")) {
|
|
||||||
text = WebUtility.UrlDecode(textRaw);
|
|
||||||
}
|
|
||||||
|
|
||||||
string fontName = "";
|
|
||||||
if (ParseUtil.ParseStringIndirect(ref fontName, node, "fontName")) {
|
|
||||||
font = DecalConfig.GetFont(fontName);
|
|
||||||
}
|
|
||||||
else if (font == null) font = DecalConfig.GetFont("Calibri SDF");
|
|
||||||
|
|
||||||
int styleInt = 0;
|
|
||||||
if (ParseUtil.ParseIntIndirect(ref styleInt, node, "style")) {
|
|
||||||
style = (FontStyles) styleInt;
|
|
||||||
}
|
|
||||||
|
|
||||||
ParseUtil.ParseColor32Indirect(ref fillColor, node, "fillColor");
|
|
||||||
ParseUtil.ParseColor32Indirect(ref outlineColor, node, "outlineColor");
|
|
||||||
|
|
||||||
if (HighLogic.LoadedSceneIsGame) {
|
|
||||||
// For some reason, rendering doesnt work right on the first frame a scene is loaded
|
|
||||||
// So delay any rendering until the next frame when called in OnLoad
|
|
||||||
// This is probably a problem with Unity, not KSP
|
|
||||||
StartCoroutine(UpdateTextLate());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
UpdateTextures();
|
|
||||||
UpdateMaterials();
|
|
||||||
UpdateScale();
|
|
||||||
UpdateTargets();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnSave(ConfigNode node) {
|
public override void OnSave(ConfigNode node) {
|
||||||
node.AddValue("text", WebUtility.UrlEncode(text));
|
node.AddValue("text", WebUtility.UrlEncode(text));
|
||||||
@ -254,18 +220,69 @@ namespace ConformalDecals {
|
|||||||
base.OnDetach();
|
base.OnDetach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FUNCTIONS
|
||||||
|
|
||||||
|
protected override void LoadDecal(ConfigNode node) {
|
||||||
|
base.LoadDecal(node);
|
||||||
|
|
||||||
|
string textRaw = "";
|
||||||
|
if (ParseUtil.ParseStringIndirect(ref textRaw, node, "text")) {
|
||||||
|
text = WebUtility.UrlDecode(textRaw);
|
||||||
|
}
|
||||||
|
|
||||||
|
string fontName = "";
|
||||||
|
if (ParseUtil.ParseStringIndirect(ref fontName, node, "fontName")) {
|
||||||
|
font = DecalConfig.GetFont(fontName);
|
||||||
|
}
|
||||||
|
else if (font == null) font = DecalConfig.GetFont("Calibri SDF");
|
||||||
|
|
||||||
|
int styleInt = 0;
|
||||||
|
if (ParseUtil.ParseIntIndirect(ref styleInt, node, "style")) {
|
||||||
|
style = (FontStyles) styleInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
ParseUtil.ParseColor32Indirect(ref fillColor, node, "fillColor");
|
||||||
|
ParseUtil.ParseColor32Indirect(ref outlineColor, node, "outlineColor");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void SetupDecal() {
|
||||||
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
|
// Update tweakables in editor mode
|
||||||
|
UpdateTweakables();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HighLogic.LoadedSceneIsGame) {
|
||||||
|
// For some reason text rendering fails on the first frame of a scene, so this is my workaround
|
||||||
|
StartCoroutine(UpdateTextLate());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
scale = defaultScale;
|
||||||
|
depth = defaultDepth;
|
||||||
|
opacity = defaultOpacity;
|
||||||
|
cutoff = defaultCutoff;
|
||||||
|
wear = defaultWear;
|
||||||
|
|
||||||
|
UpdateTextures();
|
||||||
|
UpdateMaterials();
|
||||||
|
UpdateScale();
|
||||||
|
|
||||||
|
// QUEUE PART FOR ICON FIXING IN VAB
|
||||||
|
DecalIconFixer.QueuePart(part.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void UpdateText() {
|
protected void UpdateText() {
|
||||||
UpdateTextures();
|
UpdateTextures();
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateScale();
|
||||||
UpdateTargets();
|
UpdateTargets();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator UpdateTextLate() {
|
private IEnumerator UpdateTextLate() {
|
||||||
yield return null;
|
yield return null;
|
||||||
UpdateText();
|
UpdateText();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UpdateTextures() {
|
protected override void UpdateTextures() {
|
||||||
// Render text
|
// Render text
|
||||||
var newText = new DecalText(text, font, style, vertical, lineSpacing, charSpacing);
|
var newText = new DecalText(text, font, style, vertical, lineSpacing, charSpacing);
|
||||||
|
Loading…
Reference in New Issue
Block a user