New: Nature Shaders

Interaction, Wind, Dynamic Masking, and Better Shading for your vegetation.

Learn more
This page may not work correctly inside the Unity editor. Open in browser

Nature Asset API

Requires the full Nature Shaders package.

Not supported in Nature Shaders Essentials. (Learn more)

Nature Assets are used to convert objects for use with Nature Shaders. While you can create Nature Assets manually, for large projects it can be convenient to automate this process. You can do this using the Nature Asset (Editor) API:

Create Nature Asset

Use the following code to create a new Nature Asset in your project:

using UnityEditor;
using VisualDesignCafe.Nature.Editor.Importers;

var prefabPath = "Assets/Your Prefab.prefab";
var natureAssetPath = "Assets/Your New Nature Asset.nature";

NatureAsset.Create( 
natureAssetPath,
AssetDatabase.LoadAssetAtPath( prefabPath ) );

The Nature Asset is created at the given path with the default import settings, and the asset is imported.

Edit Import Settings

After creating a Nature Asset in your project you can easily edit the import settings. To do so, you have to get the Asset Importer for the asset:

using VisualDesignCafe.Nature.Editor.Importers;

NatureAssetImporter importer =
NatureAsset.GetImporter( natureAssetPath );

After creating the importer, you can edit the settings and apply them:

// Change the format for the Nature Asset.
importer.ImportSettings.Format = MeshFormat.UnityTreeCreator;

// Change more import settings
// ...
// Mark the importer as dirty so that the settings are saved. EditorUtility.SetDirty( importer.Importer ); // Save the import settings and reimport the Nature Asset. importer.SaveAndReimport();

Material Import Settings

After getting the import settings using the code above you can edit settings for specific materials. Import settings for specific materials are loaded and saved using the material name as a unique ID.

var materialSettings = 
    importer.ImportSettings.MaterialSettings["Material Name"];

If you don't know the specific material name then you can loop through all materials in the import settings to find the materials that you need:

foreach( var materialSettings in importer.ImportSettings.MaterialSettings )
    Debug.Log( materialSettings.Id );

After getting the settings for the material, you can edit its type or properties:

using VisualDesignCafe.Nature;

// Set the material type.
materialSettings.Type = MaterialType.TreeBark;

// Enable/Disable material features.
materialSettings.Wind = true;
materialSettings.Interaction = false;
materialSettings.Translucency = false;
materialSettings.Overlay = false;

// Set specific material properties.
materialSettings.Floats =
    new[]
    {
        new MaterialImportSettings.Settings.FloatOverride()
        {
            Name = NatureProperties.TurbulenceStrength,
            Value = 0f
        }
    };

materialSettings.Textures =
    new[]
    {
        new MaterialImportSettings.Settings.TextureOverride()
        {
                Name = NatureProperties.NormalMap,
                Value = Texture2D.normalTexture,
        }
    };

If you want to assign a specific material instead of converting the material in the prefab then you can do so using the "Override" material type. Note that all other material import settings are ignored if you override a material.

materialSettings.Type = MaterialType.Override;
materialSettings.Override = 
    AssetDatabase.LoadAssetAtPath<Material>( 
        "Assets/Your Material.mat" );

Mesh Import Settings

Mesh Import Settings are edited the same way as the material settings above, using the name of the mesh as ID. Furthermore, each mesh has multiple sub-meshes, one for each material:

var meshSettings = importer.ImportSettings.MeshSettings["Mesh Name"];
var subMeshSettings = meshSettings["Material Name"];
subMeshSettings.type = MeshType.TreeBranch;

Most of the import settings for the meshes are set on a global level, for all meshes in the Nature Asset. These are the same settings that are displayed in the UI in the "Mesh Conversion" section.

Note: These settings are read-only in the current version. They will be available in the next update (version 1.0.6)

var meshSettings = importer.ImportSettings.MeshSettings;
meshSettings.RemoveDoubleSidedFaces = true; meshSettings.Orientation = MeshImportSettings.AxisOrientation.YUp; meshSettings.Complexity = MeshImportSettings.MeshComplexity.Complex; meshSettings.WindVariation = 1f; meshSettings.BranchWindScale = 1f; meshSettings.LeafWindScale = 1f; meshSettings.MergeIntersecting = false; meshSettings.TrunkHasColliders = false;

"Auto" Import Settings

A lot of import settings for Nature Assets are set to "Auto" by default. These automatic settings are resolved during import, based on the materials and meshes in the game object.

Material types are resolved based on the material name and shader name. The name is checked for keywords to assign a material type. To check the result for a specific material you can use the following code:

MaterialType materialType = 
MaterialImportSettings.ResolveMaterialType(
"Material Name",
"Shader Name" );

For meshes, use:

MeshType meshType = 
MeshImportSettings.ResolveMeshType( materialType );

It can be convenient to get the resolved settings for the entire Nature Asset to check what the actual import settings will be for the "Auto" settings. You can do this using the "Resolve()" method. Resolve() will create a copy of the import settings with all the "Auto" settings applied.

MaterialImportSettings resolvedMaterialSettings = 
importer.ImportSettings.MaterialSettings.Resolve();

Meshes use the same method, note that the mesh settings require the resolved material settings. This is required because the mesh types are resolved based on the material types.

MeshImportSettings resolvedMeshSettings = 
importer.ImportSettings.MeshSettings.Resolve(
resolvedMaterialSettings );

These resolved settings do not affect anything and are only used for checking what the import settings will be. This is useful for debugging or displaying information. For example, the UI of the Nature Asset importer uses these methods to display the material type:

nature-asset-import-settings-resolved-types.jpg

Was this article helpful?
0 out of 1 found this helpful