2020-05-30 06:26:33 +00:00
|
|
|
using System;
|
2020-05-30 20:36:10 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using ConformalDecals.MaterialModifiers;
|
|
|
|
using UniLinq;
|
2020-05-30 06:26:33 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
2020-05-27 06:04:19 +00:00
|
|
|
namespace ConformalDecals {
|
|
|
|
public class ModuleConformalDecal : PartModule {
|
|
|
|
[KSPField] public string decalPreviewTransform = "";
|
2020-05-30 20:36:10 +00:00
|
|
|
[KSPField] public string decalModelTransform = "";
|
|
|
|
|
|
|
|
[KSPField(guiName = "Scale", guiActive = false, guiActiveEditor = true, isPersistant = true, guiFormat = "F2", guiUnits = "m"),
|
|
|
|
UI_FloatRange(minValue = 0.05f, maxValue = 4f, stepIncrement = 0.05f)]
|
|
|
|
public float scale = 1.0f;
|
|
|
|
|
|
|
|
[KSPField(guiName = "Depth", guiActive = false, guiActiveEditor = true, isPersistant = true, guiFormat = "F2", guiUnits = "m"),
|
|
|
|
UI_FloatRange(minValue = 0.05f, maxValue = 4f, stepIncrement = 0.05f)]
|
|
|
|
public float depth = 1.0f;
|
|
|
|
|
|
|
|
[KSPField(guiActive = true, guiFormat = "F2", guiName = "Aspect Ratio")]
|
|
|
|
public float aspectRatio = 1.0f;
|
|
|
|
|
|
|
|
private List<ProjectionTarget> _targets;
|
|
|
|
private MaterialPropertyCollection _materialProperties;
|
|
|
|
private Material _material;
|
|
|
|
|
|
|
|
private Matrix4x4 _orthoMatrix;
|
|
|
|
private Bounds _decalBounds;
|
|
|
|
|
|
|
|
private Transform _decalPreviewTransform;
|
|
|
|
private Transform _decalModelTransform;
|
|
|
|
|
|
|
|
private bool IsAttached => part.parent != null;
|
|
|
|
|
|
|
|
public override void OnLoad(ConfigNode node) {
|
|
|
|
if (HighLogic.LoadedSceneIsGame) {
|
|
|
|
try {
|
2020-05-30 20:46:03 +00:00
|
|
|
// parse MATERIAL node
|
2020-05-30 20:36:10 +00:00
|
|
|
var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module");
|
|
|
|
_materialProperties = new MaterialPropertyCollection(materialNode, this);
|
|
|
|
_material = _materialProperties.ParsedMaterial;
|
|
|
|
|
2020-05-30 20:46:03 +00:00
|
|
|
// get aspect ratio from main texture, if it exists
|
2020-05-30 20:36:10 +00:00
|
|
|
var mainTexture = _materialProperties.MainTextureProperty;
|
|
|
|
if (mainTexture != null) {
|
|
|
|
aspectRatio = mainTexture.AspectRatio;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aspectRatio = 1;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:46:03 +00:00
|
|
|
// find preview object references
|
2020-05-30 20:36:10 +00:00
|
|
|
_decalPreviewTransform = part.FindModelTransform(decalPreviewTransform);
|
|
|
|
if (_decalPreviewTransform == null) throw new FormatException("Missing decal preview reference");
|
|
|
|
|
|
|
|
_decalModelTransform = part.FindModelTransform(decalModelTransform);
|
|
|
|
if (_decalModelTransform == null) throw new FormatException("Missing decal mesh reference");
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
this.LogException("Exception parsing partmodule", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnStart(StartState state) {
|
2020-05-30 20:46:03 +00:00
|
|
|
// generate orthogonal projection matrix and offset it by 0.5 on x and y axes
|
2020-05-30 20:36:10 +00:00
|
|
|
_orthoMatrix = Matrix4x4.identity;
|
|
|
|
_orthoMatrix[0, 3] = 0.5f;
|
|
|
|
_orthoMatrix[1, 3] = 0.5f;
|
2020-05-30 06:26:33 +00:00
|
|
|
|
|
|
|
if ((state & StartState.Editor) != 0) {
|
2020-05-30 20:46:03 +00:00
|
|
|
// setup OnTweakEvent for scale and depth fields in editor
|
2020-05-30 06:26:33 +00:00
|
|
|
GameEvents.onEditorPartEvent.Add(OnEditorEvent);
|
2020-05-30 20:36:10 +00:00
|
|
|
Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
|
|
|
Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
2020-05-30 06:26:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-05-30 20:46:03 +00:00
|
|
|
// if we start in the flight scene attached, call Attach
|
2020-05-30 20:36:10 +00:00
|
|
|
if (IsAttached) Attach();
|
2020-05-30 06:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
public void OnDisable() {
|
|
|
|
// remove from preCull delegate
|
|
|
|
Camera.onPreCull -= Render;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnTweakEvent(BaseField field, object obj) {
|
|
|
|
// scale or depth values have been changed, so update the projection matrix for each target
|
|
|
|
Project();
|
|
|
|
}
|
|
|
|
|
2020-05-30 06:26:33 +00:00
|
|
|
public void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
|
|
|
if (eventPart != this.part) return;
|
|
|
|
switch (eventType) {
|
|
|
|
case ConstructionEventType.PartAttached:
|
|
|
|
Attach();
|
|
|
|
break;
|
|
|
|
case ConstructionEventType.PartDetached:
|
|
|
|
Detach();
|
|
|
|
break;
|
|
|
|
case ConstructionEventType.PartOffsetting:
|
|
|
|
case ConstructionEventType.PartRotated:
|
|
|
|
case ConstructionEventType.PartDragging:
|
|
|
|
Project();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Attach() {
|
2020-05-30 20:36:10 +00:00
|
|
|
if (!IsAttached) {
|
2020-05-30 06:26:33 +00:00
|
|
|
this.LogError("Attach function called but part has no parent!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
// find all valid renderers
|
|
|
|
var renderers = part.parent.transform.GetComponentsInChildren<MeshRenderer>(false).Where(o => o.GetComponent<MeshFilter>() != null);
|
|
|
|
// generate ProjectionTarget objects for each valid meshrenderer
|
|
|
|
_targets = renderers.Select(o => new ProjectionTarget(o, o.GetComponent<MeshFilter>().mesh, _materialProperties)).ToList();
|
|
|
|
|
|
|
|
// hide preview model
|
|
|
|
_decalModelTransform.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
// add to preCull delegate
|
2020-05-30 06:26:33 +00:00
|
|
|
Camera.onPreCull += Render;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Detach() {
|
2020-05-30 20:36:10 +00:00
|
|
|
if (IsAttached) {
|
2020-05-30 06:26:33 +00:00
|
|
|
this.LogError("Detach function called but part still has parent!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
// unhide preview model
|
|
|
|
_decalModelTransform.gameObject.SetActive(true);
|
|
|
|
|
|
|
|
// remove from preCull delegate
|
2020-05-30 06:26:33 +00:00
|
|
|
Camera.onPreCull -= Render;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:46:03 +00:00
|
|
|
[KSPEvent(guiActive = false, guiName = "Project", guiActiveEditor = true, active = true)]
|
2020-05-30 20:36:10 +00:00
|
|
|
public void Project() {
|
|
|
|
if (!IsAttached) return;
|
|
|
|
|
|
|
|
// generate orthogonal matrix scale values
|
|
|
|
_orthoMatrix[0, 0] = 1 / scale;
|
|
|
|
_orthoMatrix[1, 1] = 1 / (aspectRatio * scale);
|
|
|
|
_orthoMatrix[2, 2] = 1 / depth;
|
|
|
|
|
|
|
|
// generate bounding box for decal for culling purposes
|
|
|
|
_decalBounds.center = Vector3.forward * (depth / 2);
|
|
|
|
_decalBounds.extents = new Vector3(scale / 2, aspectRatio * scale / 2, depth / 2);
|
|
|
|
|
|
|
|
// project to each target object
|
|
|
|
foreach (var target in _targets) {
|
2020-05-30 20:46:03 +00:00
|
|
|
target.Project(_orthoMatrix, _decalBounds, this.transform);
|
2020-05-30 20:36:10 +00:00
|
|
|
}
|
2020-05-30 06:26:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
public void Render(Camera camera) {
|
|
|
|
if (!IsAttached) return;
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
// render on each target object
|
|
|
|
foreach (var target in _targets) {
|
|
|
|
target.Render(_material, part.mpb, camera);
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
}
|