Add culling for offscreen decals

This commit is contained in:
Andrew Cassidy 2020-06-11 20:49:13 -07:00
parent b247e9af4e
commit f195fc4c19
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1
5 changed files with 41 additions and 7 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8af8a41d215556458e4f1a77bfd22aaf749c44eb427c1867e5cf8e84fbdaeb37
size 5164
oid sha256:2f6b1f01873a50d91f3af3f0452e49f1819bbd3438383c88e2d23d74fae3e3ee
size 5172

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:347ae98aa8eb27d0d16974c06ea444d2d6fb72893797ab9201a0fa1ca822f6fb
size 37888
oid sha256:f7fcc42c1ba59a7694df18c60f827f5fd98113ce3471dc01dab37bb67b2d63e5
size 38400

View File

@ -51,6 +51,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DecalBoundsBehaviour.cs" />
<Compile Include="DecalConfig.cs" />
<Compile Include="DecalIconFixer.cs" />
<Compile Include="DecalPropertyIDs.cs" />

View File

@ -0,0 +1,12 @@
using System;
using UnityEngine;
namespace ConformalDecals {
public class DecalBoundsBehaviour : MonoBehaviour {
public ModuleConformalDecal decalRenderer;
private void OnWillRenderObject() {
decalRenderer._shouldRender = true;
}
}
}

View File

@ -48,6 +48,8 @@ namespace ConformalDecals {
/// </remarks>
[KSPField] public string decalProjector = "Decal-Projector";
[KSPField] public string decalBounds = "Decal-Bounds";
// Parameters
[KSPField] public bool scaleAdjustable = true;
@ -124,6 +126,7 @@ namespace ConformalDecals {
[KSPField] public Transform decalBackTransform;
[KSPField] public Transform decalModelTransform;
[KSPField] public Transform decalProjectorTransform;
[KSPField] public Transform decalBoundsTransform;
[KSPField] public Material backMaterial;
[KSPField] public Vector2 backTextureBaseScale;
@ -140,6 +143,8 @@ namespace ConformalDecals {
private Material _decalMaterial;
private Material _previewMaterial;
internal bool _shouldRender;
private int DecalQueue {
get {
_decalQueueCounter++;
@ -204,6 +209,15 @@ namespace ConformalDecals {
if (decalProjectorTransform == null) throw new FormatException($"Could not find decalProjector transform: '{decalProjector}'.");
}
// find bounds transform
if (string.IsNullOrEmpty(decalBounds)) {
decalBoundsTransform = part.transform;
}
else {
decalBoundsTransform = part.FindModelTransform(decalBounds);
if (decalBoundsTransform == null) throw new FormatException($"Could not find decalBounds transform: '{decalBounds}'.");
}
// get back material if necessary
if (updateBackScale) {
this.Log("Getting material and base scale for back material");
@ -303,6 +317,9 @@ namespace ConformalDecals {
materialProperties.RenderQueue = DecalQueue;
var boundsBehaviour = decalBoundsTransform.gameObject.AddComponent<DecalBoundsBehaviour>();
boundsBehaviour.decalRenderer = this;
UpdateMaterials();
if (HighLogic.LoadedSceneIsGame) {
@ -419,6 +436,10 @@ namespace ConformalDecals {
UpdateScale();
}
protected void Update() {
_shouldRender = false;
}
protected void UpdateScale() {
var aspectRatio = materialProperties.AspectRatio;
Vector2 size;
@ -592,7 +613,7 @@ namespace ConformalDecals {
}
}
protected void Render(Camera camera) {
public void Render(Camera camera) {
if (!_isAttached) return;
// render on each target object