@ -35,6 +35,8 @@ namespace ConformalDecals {
[KSPField] public DecalScaleMode scaleMode = DecalScaleMode . HEIGHT ;
[KSPField] public float aspectRatio = - 1.0f ; // < 0 = use texture
[KSPField] public bool depthAdjustable = true ;
[KSPField] public float defaultDepth = 0.1f ;
[KSPField] public Vector2 depthRange = new Vector2 ( 0 , 2 ) ;
@ -201,6 +203,10 @@ namespace ConformalDecals {
} else if ( tileIndex > = 0 ) {
materialProperties . UpdateTile ( tileIndex , tileSize ) ;
}
// handle aspect ratio overrides
materialProperties . AspectRatio = aspectRatio ;
} catch ( Exception e ) {
this . LogException ( "Exception parsing partmodule" , e ) ;
}
@ -393,30 +399,32 @@ namespace ConformalDecals {
protected void UpdateScale ( ) {
scale = Mathf . Max ( 0.01f , scale ) ;
depth = Mathf . Max ( 0.01f , depth ) ;
var aspectRatio = Mathf . Max ( 0.01f , materialProperties . AspectRatio ) ;
var sizeRatio = Mathf . Max ( 0.01f , materialProperties . AspectRatio ) ;
Vector2 size ;
switch ( scaleMode ) {
default :
case DecalScaleMode . HEIGHT :
size = new Vector2 ( scale / aspect Ratio, scale ) ;
size = new Vector2 ( scale / size Ratio, scale ) ;
break ;
case DecalScaleMode . WIDTH :
size = new Vector2 ( scale , scale * aspect Ratio) ;
size = new Vector2 ( scale , scale * size Ratio) ;
break ;
case DecalScaleMode . AVERAGE :
var width1 = 2 * scale / ( 1 + aspect Ratio) ;
size = new Vector2 ( width1 , width1 * aspect Ratio) ;
var width1 = 2 * scale / ( 1 + size Ratio) ;
size = new Vector2 ( width1 , width1 * size Ratio) ;
break ;
case DecalScaleMode . AREA :
var width2 = Mathf . Sqrt ( scale / aspect Ratio) ;
size = new Vector2 ( width2 , width2 * aspect Ratio) ;
var width2 = Mathf . Sqrt ( scale / size Ratio) ;
size = new Vector2 ( width2 , width2 * size Ratio) ;
break ;
case DecalScaleMode . MINIMUM :
if ( aspect Ratio > 1 ) goto case DecalScaleMode . WIDTH ;
if ( size Ratio > 1 ) goto case DecalScaleMode . WIDTH ;
else goto case DecalScaleMode . HEIGHT ;
case DecalScaleMode . MAXIMUM :
if ( aspect Ratio > 1 ) goto case DecalScaleMode . HEIGHT ;
if ( size Ratio > 1 ) goto case DecalScaleMode . HEIGHT ;
else goto case DecalScaleMode . WIDTH ;
}
@ -433,7 +441,7 @@ namespace ConformalDecals {
// update projection
foreach ( var target in _targets ) {
if ( target = = null ) {
if ( target . target = = null ) {
_targets . Remove ( target ) ;
} else {
target . Project ( _orthoMatrix , decalProjectorTransform , _boundsRenderer . bounds , useBaseNormal ) ;
@ -570,13 +578,19 @@ namespace ConformalDecals {
public void Render ( Camera camera ) {
if ( ! _isAttached ) return ;
// render on each target object
foreach ( var target in _targets ) {
if ( target = = null ) {
_targets . Remove ( target ) ;
} else {
try {
// render on each target object
foreach ( var target in _targets ) {
target . Render ( _decalMaterial , part . mpb , camera ) ;
}
} catch ( NullReferenceException ) {
// catch any NREs and purge null transforms from the target list
// comparing Transform to null is expensive, but a try-catch block is much cheaper
foreach ( var target in _targets ) {
if ( target . target = = null ) {
_targets . Remove ( target ) ;
}
}
}
}
}