mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Rework bounds culling and colliders
This commit is contained in:
parent
90adfbd80a
commit
32cd873dec
@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:73cd94acb4687058e5a69522168bcd31d6ce65a9ba7be7cffe4fa8d76a1ca0fc
|
oid sha256:938eed012fe2148a4a3dc8a0f534677b7ebecd62e38d43b7b7b44c62b280bc29
|
||||||
size 23552
|
size 25088
|
||||||
|
@ -44,6 +44,9 @@
|
|||||||
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
<HintPath>dlls\UnityEngine.CoreModule.dll</HintPath>
|
<HintPath>dlls\UnityEngine.CoreModule.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
|
<HintPath>dlls\UnityEngine.PhysicsModule.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs" />
|
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs" />
|
||||||
|
@ -6,8 +6,9 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace ConformalDecals {
|
namespace ConformalDecals {
|
||||||
public class ModuleConformalDecal : PartModule {
|
public class ModuleConformalDecal : PartModule {
|
||||||
[KSPField] public string decalPreviewTransform = "";
|
[KSPField] public string decalPreviewTransform = "";
|
||||||
[KSPField] public string decalModelTransform = "";
|
[KSPField] public string decalModelTransform = "";
|
||||||
|
[KSPField] public string decalProjectorTransform = "";
|
||||||
|
|
||||||
[KSPField(guiName = "Scale", guiActive = false, guiActiveEditor = true, isPersistant = true, guiFormat = "F2", guiUnits = "m"),
|
[KSPField(guiName = "Scale", guiActive = false, guiActiveEditor = true, isPersistant = true, guiFormat = "F2", guiUnits = "m"),
|
||||||
UI_FloatRange(minValue = 0.05f, maxValue = 4f, stepIncrement = 0.05f)]
|
UI_FloatRange(minValue = 0.05f, maxValue = 4f, stepIncrement = 0.05f)]
|
||||||
@ -22,8 +23,12 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
[KSPField] public MaterialPropertyCollection materialProperties;
|
[KSPField] public MaterialPropertyCollection materialProperties;
|
||||||
|
|
||||||
[KSPField] public Transform decalPreviewTransformRef;
|
[KSPField] public Transform decalPreviewTransformRef;
|
||||||
[KSPField] public Transform decalModelTransformRef;
|
[KSPField] public Transform decalModelTransformRef;
|
||||||
|
[KSPField] public Transform decalProjectorTransformRef;
|
||||||
|
[KSPField] public Transform modelTransformRef;
|
||||||
|
[KSPField] public Transform colliderTransformRef;
|
||||||
|
[KSPField] public BoxCollider colliderRef;
|
||||||
|
|
||||||
private List<ProjectionTarget> _targets;
|
private List<ProjectionTarget> _targets;
|
||||||
|
|
||||||
@ -50,11 +55,34 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// find preview object references
|
// find preview object references
|
||||||
|
modelTransformRef = part.transform.Find("model");
|
||||||
|
|
||||||
decalPreviewTransformRef = part.FindModelTransform(decalPreviewTransform);
|
decalPreviewTransformRef = part.FindModelTransform(decalPreviewTransform);
|
||||||
if (decalPreviewTransformRef == null) throw new FormatException("Missing decal preview reference");
|
if (decalPreviewTransformRef == null) throw new FormatException("Missing decal preview reference");
|
||||||
|
|
||||||
decalModelTransformRef = part.FindModelTransform(decalModelTransform);
|
if (String.IsNullOrEmpty(decalModelTransform)) {
|
||||||
if (decalModelTransformRef == null) throw new FormatException("Missing decal mesh reference");
|
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>();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
this.LogException("Exception parsing partmodule", e);
|
this.LogException("Exception parsing partmodule", e);
|
||||||
@ -71,6 +99,7 @@ namespace ConformalDecals {
|
|||||||
if ((state & StartState.Editor) != 0) {
|
if ((state & StartState.Editor) != 0) {
|
||||||
// setup OnTweakEvent for scale and depth fields in editor
|
// setup OnTweakEvent for scale and depth fields in editor
|
||||||
GameEvents.onEditorPartEvent.Add(OnEditorEvent);
|
GameEvents.onEditorPartEvent.Add(OnEditorEvent);
|
||||||
|
GameEvents.onVariantApplied.Add(OnVariantApplied);
|
||||||
Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
Fields[nameof(scale)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
||||||
Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
Fields[nameof(depth)].uiControlEditor.onFieldChanged = OnTweakEvent;
|
||||||
}
|
}
|
||||||
@ -80,16 +109,27 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisable() {
|
private void OnDestroy() {
|
||||||
|
GameEvents.onEditorPartEvent.Remove(OnEditorEvent);
|
||||||
|
GameEvents.onVariantApplied.Remove(OnVariantApplied);
|
||||||
|
|
||||||
// remove from preCull delegate
|
// remove from preCull delegate
|
||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void OnTweakEvent(BaseField field, object obj) {
|
public void OnTweakEvent(BaseField field, object obj) {
|
||||||
// scale or depth values have been changed, so update the projection matrix for each target
|
// scale or depth values have been changed, so update the projection matrix for each target
|
||||||
Project();
|
Project();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnVariantApplied(Part eventPart, PartVariant variant) {
|
||||||
|
if (IsAttached && eventPart == part.parent) {
|
||||||
|
Detach();
|
||||||
|
Attach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
public void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
|
||||||
if (eventPart != this.part) return;
|
if (eventPart != this.part) return;
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
@ -100,7 +140,7 @@ namespace ConformalDecals {
|
|||||||
Detach();
|
Detach();
|
||||||
break;
|
break;
|
||||||
case ConstructionEventType.PartOffsetting:
|
case ConstructionEventType.PartOffsetting:
|
||||||
case ConstructionEventType.PartRotated:
|
case ConstructionEventType.PartRotating:
|
||||||
case ConstructionEventType.PartDragging:
|
case ConstructionEventType.PartDragging:
|
||||||
Project();
|
Project();
|
||||||
break;
|
break;
|
||||||
@ -127,6 +167,9 @@ namespace ConformalDecals {
|
|||||||
// find all valid renderers
|
// find all valid renderers
|
||||||
var renderers = part.parent.FindModelComponents<MeshRenderer>();
|
var renderers = part.parent.FindModelComponents<MeshRenderer>();
|
||||||
foreach (var renderer in renderers) {
|
foreach (var renderer in renderers) {
|
||||||
|
// skip disabled renderers
|
||||||
|
if (renderer.gameObject.activeInHierarchy == false) 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;
|
||||||
@ -143,20 +186,23 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// hide preview model
|
// hide preview model
|
||||||
//decalModelTransformRef.gameObject.SetActive(false);
|
decalModelTransformRef.gameObject.SetActive(false);
|
||||||
|
|
||||||
|
// enable decal collider
|
||||||
|
colliderTransformRef.gameObject.SetActive(true);
|
||||||
|
|
||||||
// add to preCull delegate
|
// add to preCull delegate
|
||||||
Camera.onPreCull += Render;
|
Camera.onPreCull += Render;
|
||||||
|
|
||||||
|
Project();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Detach() {
|
public void Detach() {
|
||||||
if (IsAttached) {
|
|
||||||
this.LogError("Detach function called but part still has parent!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// unhide preview model
|
// unhide preview model
|
||||||
//decalModelTransformRef.gameObject.SetActive(true);
|
decalModelTransformRef.gameObject.SetActive(true);
|
||||||
|
|
||||||
|
// enable decal collider
|
||||||
|
colliderTransformRef.gameObject.SetActive(false);
|
||||||
|
|
||||||
// remove from preCull delegate
|
// remove from preCull delegate
|
||||||
Camera.onPreCull -= Render;
|
Camera.onPreCull -= Render;
|
||||||
@ -166,18 +212,27 @@ namespace ConformalDecals {
|
|||||||
public void Project() {
|
public void Project() {
|
||||||
if (!IsAttached) return;
|
if (!IsAttached) return;
|
||||||
|
|
||||||
|
float width = scale;
|
||||||
|
float height = scale * aspectRatio;
|
||||||
// generate orthogonal matrix scale values
|
// generate orthogonal matrix scale values
|
||||||
_orthoMatrix[0, 0] = 1 / scale;
|
_orthoMatrix[0, 0] = 1 / width;
|
||||||
_orthoMatrix[1, 1] = 1 / (aspectRatio * scale);
|
_orthoMatrix[1, 1] = 1 / height;
|
||||||
_orthoMatrix[2, 2] = 1 / depth;
|
_orthoMatrix[2, 2] = 1 / depth;
|
||||||
|
|
||||||
// generate bounding box for decal for culling purposes
|
// generate bounding box for decal for culling purposes
|
||||||
_decalBounds.center = Vector3.forward * (depth / 2);
|
_decalBounds.center = Vector3.forward * (depth / 2);
|
||||||
_decalBounds.extents = new Vector3(scale / 2, aspectRatio * scale / 2, depth / 2);
|
_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;
|
||||||
|
|
||||||
// project to each target object
|
// project to each target object
|
||||||
foreach (var target in _targets) {
|
foreach (var target in _targets) {
|
||||||
target.Project(_orthoMatrix, _decalBounds, this.transform);
|
target.Project(_orthoMatrix, colliderRef.bounds, decalProjectorTransformRef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,24 +46,28 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Project(Matrix4x4 orthoMatrix, Bounds projectorBounds, Transform projector) {
|
public void Project(Matrix4x4 orthoMatrix, Bounds projectorBounds, Transform projector) {
|
||||||
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
var targetBounds = _targetRenderer.bounds;
|
||||||
|
if (targetBounds.Intersects(projectorBounds)) {
|
||||||
|
_projectionEnabled = true;
|
||||||
|
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix;
|
||||||
|
|
||||||
var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
||||||
var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
||||||
var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
||||||
|
|
||||||
decalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
|
decalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
|
||||||
decalMPB.SetVector(_decalNormalID, decalNormal);
|
decalMPB.SetVector(_decalNormalID, decalNormal);
|
||||||
decalMPB.SetVector(_decalTangentID, decalTangent);
|
decalMPB.SetVector(_decalTangentID, decalTangent);
|
||||||
|
}
|
||||||
var targetBounds = new OrientedBounds(target.localToWorldMatrix, _targetRenderer.bounds);
|
else {
|
||||||
_projectionEnabled = targetBounds.Intersects(projectorBounds);
|
_projectionEnabled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
public bool Render(Material decalMaterial, MaterialPropertyBlock partMPB, Camera camera) {
|
||||||
if (true) {
|
if (_projectionEnabled) {
|
||||||
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
decalMPB.SetFloat(PropertyIDs._RimFalloff, partMPB.GetFloat(PropertyIDs._RimFalloff));
|
||||||
decalMPB.SetColor(PropertyIDs._RimFalloff, partMPB.GetColor(PropertyIDs._RimFalloff));
|
decalMPB.SetColor(PropertyIDs._RimColor, partMPB.GetColor(PropertyIDs._RimFalloff));
|
||||||
|
|
||||||
Graphics.DrawMesh(_targetMesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, decalMPB, ShadowCastingMode.Off, true);
|
Graphics.DrawMesh(_targetMesh, target.localToWorldMatrix, decalMaterial, 0, camera, 0, decalMPB, ShadowCastingMode.Off, true);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user