Use bounds to choose projection targets

This commit is contained in:
Andrew Cassidy 2020-06-11 21:01:01 -07:00
parent f195fc4c19
commit 4ed65bdf4a
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1
3 changed files with 26 additions and 18 deletions

View File

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

View File

@ -142,6 +142,7 @@ namespace ConformalDecals {
private Material _decalMaterial; private Material _decalMaterial;
private Material _previewMaterial; private Material _previewMaterial;
private BoxCollider _boundsCollider;
internal bool _shouldRender; internal bool _shouldRender;
@ -320,6 +321,8 @@ namespace ConformalDecals {
var boundsBehaviour = decalBoundsTransform.gameObject.AddComponent<DecalBoundsBehaviour>(); var boundsBehaviour = decalBoundsTransform.gameObject.AddComponent<DecalBoundsBehaviour>();
boundsBehaviour.decalRenderer = this; boundsBehaviour.decalRenderer = this;
_boundsCollider = decalBoundsTransform.GetComponent<BoxCollider>();
UpdateMaterials(); UpdateMaterials();
if (HighLogic.LoadedSceneIsGame) { if (HighLogic.LoadedSceneIsGame) {
@ -481,7 +484,7 @@ namespace ConformalDecals {
// update projection // update projection
foreach (var target in _targets) { foreach (var target in _targets) {
target.Project(_orthoMatrix, decalProjectorTransform, useBaseNormal); target.Project(_orthoMatrix, decalProjectorTransform, _boundsCollider.bounds, useBaseNormal);
} }
} }
else { else {

View File

@ -20,30 +20,35 @@ namespace ConformalDecals {
_decalMPB = new MaterialPropertyBlock(); _decalMPB = new MaterialPropertyBlock();
} }
public void Project(Matrix4x4 orthoMatrix, Transform projector, bool useBaseNormal) { public void Project(Matrix4x4 orthoMatrix, Transform projector, Bounds projectorBounds, bool useBaseNormal) {
_projectionEnabled = true;
var targetMaterial = _targetRenderer.sharedMaterial; if (projectorBounds.Intersects(_targetRenderer.bounds)) {
var projectorToTargetMatrix = target.worldToLocalMatrix * projector.localToWorldMatrix; _projectionEnabled = true;
var targetMaterial = _targetRenderer.sharedMaterial;
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(DecalPropertyIDs._ProjectionMatrix, projectionMatrix); _decalMPB.SetMatrix(DecalPropertyIDs._ProjectionMatrix, projectionMatrix);
_decalMPB.SetVector(DecalPropertyIDs._DecalNormal, decalNormal); _decalMPB.SetVector(DecalPropertyIDs._DecalNormal, decalNormal);
_decalMPB.SetVector(DecalPropertyIDs._DecalTangent, decalTangent); _decalMPB.SetVector(DecalPropertyIDs._DecalTangent, decalTangent);
if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) { if (useBaseNormal && targetMaterial.HasProperty(DecalPropertyIDs._BumpMap)) {
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap)); _decalMPB.SetTexture(DecalPropertyIDs._BumpMap, targetMaterial.GetTexture(DecalPropertyIDs._BumpMap));
var normalScale = targetMaterial.GetTextureScale(DecalPropertyIDs._BumpMap); var normalScale = targetMaterial.GetTextureScale(DecalPropertyIDs._BumpMap);
var normalOffset = targetMaterial.GetTextureOffset(DecalPropertyIDs._BumpMap); var normalOffset = targetMaterial.GetTextureOffset(DecalPropertyIDs._BumpMap);
_decalMPB.SetVector(DecalPropertyIDs._BumpMap_ST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y)); _decalMPB.SetVector(DecalPropertyIDs._BumpMap_ST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y));
}
else {
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, DecalConfig.BlankNormal);
}
} }
else { else {
_decalMPB.SetTexture(DecalPropertyIDs._BumpMap, DecalConfig.BlankNormal); _projectionEnabled = false;
} }
} }