Reference
Scripting API
Add, move, hide and remove instances from code, and write custom sources.
Namespace KiwiStudios.Foliage.
FoliageRenderer
| Member | Description |
|---|---|
static FoliageRenderer Main | The first enabled renderer saved with its scene. A renderer on an object with HideFlags.DontSave, which another component owns, is never Main, so sources do not draw into it. |
static IReadOnlyList<FoliageRenderer> Active | Every enabled renderer. |
int RegisterPrototype(GameObject prefab) | Registers a prefab and returns its prototype index. Adding an instance registers it for you. |
int RegisterPrototype(object key, Mesh mesh, Material[] materials, bool castShadows, int layer, string name, FoliagePrototypeSettings settings = null) | Registers a single mesh under your own key. Registering the same key again with a different mesh or materials rebuilds it. |
int AddInstance(GameObject prefab, Vector3 position, Quaternion rotation, Vector3 scale, float tint = 0) | Adds one instance and returns its handle. |
int AddInstances(int prototype, IReadOnlyList<FoliageInstance> instances, List<int> handles = null) | Adds many instances of one prototype. |
bool RemoveInstance(int handle), void RemoveInstances(List<int> handles) | Removes instances. Other handles stay valid. |
bool TryGetInstance(int handle, out FoliageInstance instance) | Reads an instance. |
bool SetInstance, SetInstanceTransform, SetInstanceVisible | Changes an instance. |
void ClearInstances() | Removes every instance. |
FoliagePrototypeSettings GetSettings(GameObject prefab), void SetSettings(FoliagePrototypeSettings settings) | Per prefab distances, shadows and impostor. |
void RefreshPrototypes() | Reads every prefab and material again. |
void GetStatistics(List<FoliagePrototypeStatistics> result) | Per prefab counts and problems. |
Vector2Int CullImmediately(Camera camera) | Culls for one camera now and returns visible and shadow caster totals. For tests and tools; it waits for the GPU. |
List<(int Prototype, int Level, int Handle, float Fade)> ReadVisible(Camera camera, bool shadows) | Every level of detail the camera's last cull listed, with the signed cross-fade. For tests and tools; it waits for the GPU. |
event Action PrototypesRebuilt | Raised after prototypes are rebuilt. |
Properties mirror the inspector: MaxDistance, LodBias, CrossFadeWidth, DistanceScale, CastShadows, ShadowDistance, ShadowLodOffset, ShadowLight, OcclusionCulling, RenderInSceneView, CollectStatistics, UploadBudget, WindZone, DebugView, MaterialLibrary, and LayerOverride, which draws every instance on one layer instead of each prefab renderer's own (-1, the default, keeps the renderers' layers). Cameras whose culling mask leaves out every layer the renderer draws on skip its culling entirely. When several renderers are active, the first one with an active Wind Zone sets the wind for all of them, so a renderer without a Wind Zone does not reset it. Read-only counters: InstanceCount, PrototypeCount, VisibleInstanceCount, ShadowCasterCount, DrawCallCount, PendingUploadCount, GpuMemoryBytes, Bounds.
FoliageInstance
A 48 byte struct: Position, Rotation, Scale, Hidden, Tint (0 to 1) and Prototype. Create one with FoliageInstance.Create(position, rotation, scale, tint).
FoliageSource
Derive from it to feed a renderer from your own data.
public sealed class RockScatter : FoliageSource
{
[SerializeField] private GameObject rock;
[SerializeField] private int count = 1000;
public override string Status => $"{InstanceCount} rocks";
protected override void OnAttached(FoliageRenderer _renderer)
{
var _random = new System.Random(1);
for (int i = 0; i < count; i++)
{
var _position = transform.position + new Vector3((float)_random.NextDouble() * 100f, 0f, (float)_random.NextDouble() * 100f);
Handles.Add(_renderer.AddInstance(rock, _position, Quaternion.Euler(0f, (float)_random.NextDouble() * 360f, 0f), Vector3.one));
}
}
protected override void OnDetached(FoliageRenderer _renderer)
{
}
}The base class removes every handle in Handles when the source is disabled or its renderer goes away. Override Tick for work spread over frames and HasPendingWork to keep edit mode updating while it runs. StreamingFocus(camera) returns the main camera in play mode and the Scene view camera in edit mode.
FoliagePrefabSource
AddPlacement, SetPlacements, ClearPlacements, Prefabs, Placements, FollowTransform.
FoliageTerrainSource
Terrain, DrawTrees, DrawDetails, DetailDistance, DetailDensity, StreamingBudgetMs, StreamingCamera, TreeCount, ImportedTreeCount, LoadedPatchCount, Rebuild().
FoliageMaterials and FoliageMaterialLibrary
FoliageMaterials.Resolve returns the material the renderer draws for a source material. FoliageMaterialLibrary.Default is the project's library loaded from Resources; TryGetShader, TryGetMaterial, SetShader and SetMaterial read and change it.
Still stuck?
Bring the renderer state with you.
Include the Kiwi Foliage version, Unity and render-pipeline versions, the graphics API, the Foliage Renderer's header state and statistics, the Shader Setup row for any shader involved, and a minimal scene.