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 {
|
2020-05-31 05:44:44 +00:00
|
|
|
[KSPField] public string decalPreviewTransform = "";
|
|
|
|
[KSPField] public string decalModelTransform = "";
|
|
|
|
[KSPField] public string decalProjectorTransform = "";
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
[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;
|
|
|
|
|
2020-05-31 00:03:14 +00:00
|
|
|
[KSPField] public MaterialPropertyCollection materialProperties;
|
|
|
|
|
2020-05-31 05:44:44 +00:00
|
|
|
[KSPField] public Transform decalPreviewTransformRef;
|
|
|
|
[KSPField] public Transform decalModelTransformRef;
|
|
|
|
[KSPField] public Transform decalProjectorTransformRef;
|
|
|
|
[KSPField] public Transform modelTransformRef;
|
|
|
|
[KSPField] public Transform colliderTransformRef;
|
|
|
|
[KSPField] public BoxCollider colliderRef;
|
2020-05-31 00:03:14 +00:00
|
|
|
|
|
|
|
private List<ProjectionTarget> _targets;
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
private Matrix4x4 _orthoMatrix;
|
|
|
|
private Bounds _decalBounds;
|
|
|
|
|
|
|
|
private bool IsAttached => part.parent != null;
|
|
|
|
|
|
|
|
public override void OnLoad(ConfigNode node) {
|
2020-05-31 00:03:14 +00:00
|
|
|
this.Log("Loading module");
|
|
|
|
try {
|
|
|
|
// parse MATERIAL node
|
|
|
|
var materialNode = node.GetNode("MATERIAL") ?? throw new FormatException("Missing MATERIAL node in module");
|
|
|
|
materialProperties = ScriptableObject.CreateInstance<MaterialPropertyCollection>();
|
|
|
|
materialProperties.Initialize(materialNode, this);
|
|
|
|
|
|
|
|
// get aspect ratio from main texture, if it exists
|
|
|
|
var mainTexture = materialProperties.MainTextureProperty;
|
|
|
|
if (mainTexture != null) {
|
|
|
|
aspectRatio = mainTexture.AspectRatio;
|
2020-05-30 20:36:10 +00:00
|
|
|
}
|
2020-05-31 00:03:14 +00:00
|
|
|
else {
|
|
|
|
aspectRatio = 1;
|
2020-05-30 20:36:10 +00:00
|
|
|
}
|
2020-05-31 00:03:14 +00:00
|
|
|
|
|
|
|
// find preview object references
|
2020-05-31 05:44:44 +00:00
|
|
|
modelTransformRef = part.transform.Find("model");
|
|
|
|
|
2020-05-31 00:03:14 +00:00
|
|
|
decalPreviewTransformRef = part.FindModelTransform(decalPreviewTransform);
|
|
|
|
if (decalPreviewTransformRef == null) throw new FormatException("Missing decal preview reference");
|
|
|
|
|
2020-05-31 05:44:44 +00:00
|
|
|
if (String.IsNullOrEmpty(decalModelTransform)) {
|
|
|
|
decalModelTransformRef = decalPreviewTransformRef;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
decalModelTransformRef = part.FindModelTransform(decalModelTransform);
|
|
|
|
if (decalModelTransformRef == null) throw new FormatException("Missing decal mesh reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(decalProjectorTransform)) {
|
|
|
|
decalProjectorTransformRef = modelTransformRef;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
decalProjectorTransformRef = part.FindModelTransform(decalProjectorTransform);
|
|
|
|
if (decalProjectorTransform == null) throw new FormatException("Missing decal projector reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
colliderTransformRef = new GameObject("Decal Collider").transform;
|
|
|
|
colliderTransformRef.parent = modelTransformRef;
|
|
|
|
colliderTransformRef.position = decalProjectorTransformRef.position;
|
|
|
|
colliderTransformRef.rotation = decalProjectorTransformRef.rotation;
|
|
|
|
colliderTransformRef.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
colliderRef = colliderTransformRef.gameObject.AddComponent<BoxCollider>();
|
2020-05-31 00:03:14 +00:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
this.LogException("Exception parsing partmodule", e);
|
2020-05-30 20:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnStart(StartState state) {
|
2020-05-31 00:03:14 +00:00
|
|
|
this.Log("Starting module");
|
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-31 05:44:44 +00:00
|
|
|
GameEvents.onVariantApplied.Add(OnVariantApplied);
|
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-31 05:44:44 +00:00
|
|
|
private void OnDestroy() {
|
|
|
|
GameEvents.onEditorPartEvent.Remove(OnEditorEvent);
|
|
|
|
GameEvents.onVariantApplied.Remove(OnVariantApplied);
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
// remove from preCull delegate
|
|
|
|
Camera.onPreCull -= Render;
|
|
|
|
}
|
|
|
|
|
2020-05-31 05:44:44 +00:00
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
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-31 05:44:44 +00:00
|
|
|
public void OnVariantApplied(Part eventPart, PartVariant variant) {
|
|
|
|
if (IsAttached && eventPart == part.parent) {
|
|
|
|
Detach();
|
|
|
|
Attach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2020-05-31 05:44:44 +00:00
|
|
|
case ConstructionEventType.PartRotating:
|
2020-05-30 06:26:33 +00:00
|
|
|
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-31 00:03:14 +00:00
|
|
|
this.Log($"Decal attached to {part.parent.partName}");
|
|
|
|
this.Log($"{materialProperties == null}");
|
|
|
|
this.Log($"{decalModelTransformRef == null}");
|
|
|
|
|
|
|
|
if (_targets == null) {
|
|
|
|
_targets = new List<ProjectionTarget>();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_targets.Clear();
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:36:10 +00:00
|
|
|
// find all valid renderers
|
2020-05-31 00:03:14 +00:00
|
|
|
var renderers = part.parent.FindModelComponents<MeshRenderer>();
|
|
|
|
foreach (var renderer in renderers) {
|
2020-05-31 05:44:44 +00:00
|
|
|
// skip disabled renderers
|
|
|
|
if (renderer.gameObject.activeInHierarchy == false) continue;
|
|
|
|
|
2020-05-31 00:03:14 +00:00
|
|
|
var meshFilter = renderer.GetComponent<MeshFilter>();
|
|
|
|
if (meshFilter == null) continue; // object has a meshRenderer with no filter, invalid
|
|
|
|
var mesh = meshFilter.mesh;
|
|
|
|
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
|
|
|
|
var target = new ProjectionTarget(renderer, mesh, materialProperties);
|
|
|
|
|
|
|
|
this.Log("done.");
|
|
|
|
|
|
|
|
// add the target to the list
|
|
|
|
_targets.Add(target);
|
|
|
|
}
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
// hide preview model
|
2020-05-31 05:44:44 +00:00
|
|
|
decalModelTransformRef.gameObject.SetActive(false);
|
|
|
|
|
|
|
|
// enable decal collider
|
|
|
|
colliderTransformRef.gameObject.SetActive(true);
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
// add to preCull delegate
|
2020-05-30 06:26:33 +00:00
|
|
|
Camera.onPreCull += Render;
|
2020-05-31 05:44:44 +00:00
|
|
|
|
|
|
|
Project();
|
2020-05-30 06:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Detach() {
|
2020-05-30 20:36:10 +00:00
|
|
|
// unhide preview model
|
2020-05-31 05:44:44 +00:00
|
|
|
decalModelTransformRef.gameObject.SetActive(true);
|
|
|
|
|
|
|
|
// enable decal collider
|
|
|
|
colliderTransformRef.gameObject.SetActive(false);
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2020-05-31 05:44:44 +00:00
|
|
|
float width = scale;
|
|
|
|
float height = scale * aspectRatio;
|
2020-05-30 20:36:10 +00:00
|
|
|
// generate orthogonal matrix scale values
|
2020-05-31 05:44:44 +00:00
|
|
|
_orthoMatrix[0, 0] = 1 / width;
|
|
|
|
_orthoMatrix[1, 1] = 1 / height;
|
2020-05-30 20:36:10 +00:00
|
|
|
_orthoMatrix[2, 2] = 1 / depth;
|
|
|
|
|
|
|
|
// generate bounding box for decal for culling purposes
|
|
|
|
_decalBounds.center = Vector3.forward * (depth / 2);
|
2020-05-31 05:44:44 +00:00
|
|
|
_decalBounds.extents = new Vector3(width / 2, height / 2, depth / 2);
|
|
|
|
|
|
|
|
// rescale preview model
|
|
|
|
decalModelTransformRef.localScale = new Vector3(width, height, (width + height) / 2);
|
|
|
|
|
|
|
|
// assign dimensions to collider
|
|
|
|
colliderRef.center = _decalBounds.center;
|
|
|
|
colliderRef.size = _decalBounds.size;
|
2020-05-30 20:36:10 +00:00
|
|
|
|
|
|
|
// project to each target object
|
|
|
|
foreach (var target in _targets) {
|
2020-05-31 05:44:44 +00:00
|
|
|
target.Project(_orthoMatrix, colliderRef.bounds, decalProjectorTransformRef);
|
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) {
|
2020-05-31 00:03:14 +00:00
|
|
|
target.Render(materialProperties.parsedMaterial, part.mpb, camera);
|
2020-05-30 20:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
}
|