Foliage Renderer Quality Settings
This commit is contained in:
parent
82faa6c117
commit
843b407bf5
|
@ -45,6 +45,8 @@ namespace Rokojori
|
|||
[Export]
|
||||
public int renderPriority;
|
||||
|
||||
[Export]
|
||||
public FoliageQualitySettings[] qualitySettings;
|
||||
|
||||
|
||||
[Export]
|
||||
|
|
|
@ -0,0 +1,102 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
namespace Rokojori
|
||||
{
|
||||
/** <summary for="class FoliageRendererSettings">
|
||||
|
||||
<title>
|
||||
Resource to define foliage data
|
||||
</title>
|
||||
|
||||
<description>
|
||||
|
||||
</description>
|
||||
|
||||
</summary>
|
||||
*/
|
||||
|
||||
[Tool]
|
||||
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Scatterer.svg") ]
|
||||
public partial class FoliageQualitySettings:Resource
|
||||
{
|
||||
[Export( PropertyHint.Range, "0,200")]
|
||||
public float qualityLevel = 100;
|
||||
|
||||
[Export]
|
||||
public float cellSizeMultiply = 1f;
|
||||
|
||||
[Export]
|
||||
public float visibilityRangeMultiply = 1f;
|
||||
|
||||
|
||||
public static float GetCellSize( float quality, FoliageQualitySettings[] all, FoliageQualitySettings[] own, float ownValue )
|
||||
{
|
||||
if ( own != null && own.Length > 0 )
|
||||
{
|
||||
return ComputeCellSize( quality, all, ownValue );
|
||||
}
|
||||
|
||||
if ( all != null && all.Length > 0 )
|
||||
{
|
||||
return ComputeCellSize( quality, all, ownValue );
|
||||
}
|
||||
|
||||
return ownValue;
|
||||
}
|
||||
|
||||
public static float GetVisibilityRange( float quality, FoliageQualitySettings[] all, FoliageQualitySettings[] own, float ownValue )
|
||||
{
|
||||
if ( own != null && own.Length > 0 )
|
||||
{
|
||||
return ComputeVisibilityRange( quality, all, ownValue );
|
||||
}
|
||||
|
||||
if ( all != null && all.Length > 0 )
|
||||
{
|
||||
return ComputeVisibilityRange( quality, all, ownValue );
|
||||
}
|
||||
|
||||
return ownValue;
|
||||
}
|
||||
|
||||
static Func<FoliageQualitySettings,float> getQualityyLevel = ( fqs ) => fqs.qualityLevel;
|
||||
|
||||
protected static float ComputeCellSize( float quality, FoliageQualitySettings[] settings, float value )
|
||||
{
|
||||
if ( settings.Length == 1 )
|
||||
{
|
||||
return settings[ 0 ].cellSizeMultiply * value;
|
||||
}
|
||||
|
||||
var closest = settings.ClosestIndex( quality, getQualityyLevel );
|
||||
var second = settings.SecondClosestIndex( closest, quality, getQualityyLevel );
|
||||
|
||||
var a = settings[ closest ];
|
||||
var b = settings[ second ];
|
||||
|
||||
return value * MathX.RemapClamped( quality, a.qualityLevel, b.qualityLevel, a.cellSizeMultiply, b.cellSizeMultiply );
|
||||
}
|
||||
|
||||
protected static float ComputeVisibilityRange( float quality, FoliageQualitySettings[] settings, float value )
|
||||
{
|
||||
if ( settings.Length == 1 )
|
||||
{
|
||||
return settings[ 0 ].visibilityRangeMultiply * value;
|
||||
}
|
||||
|
||||
var closest = settings.ClosestIndex( quality, getQualityyLevel );
|
||||
var second = settings.SecondClosestIndex( closest, quality, getQualityyLevel );
|
||||
|
||||
var a = settings[ closest ];
|
||||
var b = settings[ second ];
|
||||
|
||||
return value * MathX.RemapClamped( quality, a.qualityLevel, b.qualityLevel, a.visibilityRangeMultiply, b.visibilityRangeMultiply );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
uid://bdcpb8ug5evdx
|
|
@ -48,14 +48,19 @@ namespace Rokojori
|
|||
{
|
||||
gpuFoliageShaderMaterial.cameraPosition.Set( renderer.GetAssignedCamera().GlobalPosition );
|
||||
|
||||
var sizeInt = Mathf.CeilToInt( data.visibilityRange / data.cellSize ) * 2;
|
||||
|
||||
|
||||
if ( _lastCellSize != data.cellSize || _lastMaxVisibility != data.visibilityRange )
|
||||
var cellSize = FoliageQualitySettings.GetCellSize( renderer.quality, renderer.qualitySettingsAll, data.qualitySettings, data.cellSize );
|
||||
var visibilityRange = FoliageQualitySettings.GetVisibilityRange( renderer.quality, renderer.qualitySettingsAll, data.qualitySettings, data.visibilityRange );
|
||||
|
||||
var sizeInt = Mathf.CeilToInt( visibilityRange / cellSize ) * 2;
|
||||
|
||||
if ( _lastCellSize != cellSize || _lastMaxVisibility != visibilityRange )
|
||||
{
|
||||
_lastCellSize = data.cellSize;
|
||||
_lastMaxVisibility = data.visibilityRange;
|
||||
_lastCellSize = cellSize;
|
||||
_lastMaxVisibility = visibilityRange;
|
||||
|
||||
gpuFoliageShaderMaterial.cellSize.Set( data.cellSize );
|
||||
gpuFoliageShaderMaterial.cellSize.Set( cellSize );
|
||||
|
||||
|
||||
|
||||
|
@ -66,8 +71,8 @@ namespace Rokojori
|
|||
|
||||
}
|
||||
|
||||
var hideStart = data.visibilityRange - ( data.visibilityRange * data.visibilityFadeRelative + data.visibilityFadeAbsolute );
|
||||
gpuFoliageShaderMaterial.hideMax.Set( data.visibilityRange );
|
||||
var hideStart = visibilityRange - ( visibilityRange * data.visibilityFadeRelative + data.visibilityFadeAbsolute );
|
||||
gpuFoliageShaderMaterial.hideMax.Set( visibilityRange );
|
||||
gpuFoliageShaderMaterial.hideStart.Set( hideStart );
|
||||
gpuFoliageShaderMaterial.hideOffset.Set( data.visibilityFadeHidingOffset );
|
||||
|
||||
|
|
|
@ -33,6 +33,12 @@ namespace Rokojori
|
|||
[Export]
|
||||
public FoliageData[] foliage = [];
|
||||
|
||||
[Export( PropertyHint.Range, "0, 200" )]
|
||||
public float quality = 100;
|
||||
|
||||
[Export]
|
||||
public FoliageQualitySettings[] qualitySettingsAll;
|
||||
|
||||
List<FoliageRenderLayer> renderLayers = [];
|
||||
|
||||
[Export]
|
||||
|
|
|
@ -213,7 +213,7 @@ namespace Rokojori
|
|||
return index;
|
||||
}
|
||||
|
||||
public static int SecondClosestIndex<T>( List<T> data, int closest, float compareValue, Func<T,float> getValue, bool sort = false )
|
||||
public static int SecondClosestIndex<T>( this List<T> data, int closest, float compareValue, Func<T,float> getValue, bool sort = false )
|
||||
{
|
||||
if ( sort )
|
||||
{
|
||||
|
@ -249,7 +249,39 @@ namespace Rokojori
|
|||
return closest + 1;
|
||||
}
|
||||
|
||||
public static int ClosestIndex<T>( List<T> data, float compareValue, Func<T,float> getValue )
|
||||
public static int SecondClosestIndex<T>( this T[] data, int closest, float compareValue, Func<T,float> getValue )
|
||||
{
|
||||
|
||||
if ( data.Length == 1 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( closest == 0 )
|
||||
{
|
||||
return closest + 1;
|
||||
}
|
||||
|
||||
if ( closest == data.Length - 1 )
|
||||
{
|
||||
return data.Length - 2;
|
||||
}
|
||||
|
||||
var before = data[ closest - 1 ];
|
||||
var after = data[ closest + 1 ];
|
||||
|
||||
var bD = Mathf.Abs( getValue( before ) - compareValue );
|
||||
var aD = Mathf.Abs( getValue( after ) - compareValue );
|
||||
|
||||
if ( bD < aD )
|
||||
{
|
||||
return closest - 1 ;
|
||||
}
|
||||
|
||||
return closest + 1;
|
||||
}
|
||||
|
||||
public static int ClosestIndex<T>( this List<T> data, float compareValue, Func<T,float> getValue )
|
||||
{
|
||||
var index = -1;
|
||||
var distance = float.MaxValue;
|
||||
|
@ -268,6 +300,25 @@ namespace Rokojori
|
|||
return index;
|
||||
}
|
||||
|
||||
public static int ClosestIndex<T>( this T[] data, float compareValue, Func<T,float> getValue )
|
||||
{
|
||||
var index = -1;
|
||||
var distance = float.MaxValue;
|
||||
|
||||
for ( int i = 0 ; i < data.Length; i++ )
|
||||
{
|
||||
var d = Mathf.Abs( getValue( data[ i ] ) - compareValue );
|
||||
|
||||
if ( d < distance )
|
||||
{
|
||||
index = i;
|
||||
distance = d;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public static List<int> CollectIndices<T>( List<T> list, Func<T,bool> evaluator )
|
||||
{
|
||||
var output = new List<int>();
|
||||
|
|
Loading…
Reference in New Issue