Runtime Usage

With Sloyd, you can even generate 3D models at runtime!

Sliders, toggles and buttons are automatically available for use during playmode.

This section will show you how to access parameter control through code.

Spawning A Sloyd Object In Play Mode

  • Add a reference to an existing SloydAsset:

using Sloyd;
...
[SerializeField] private SloydAsset _sloydAsset;
  • Create SloydObject via its constructor:

SloydObject sloydObject = new SloydObject(_sloydAsset);
sloydObject.RebuildMesh();

Call SloydObject.RebuildMesh() each time you create new SloydObject or update its controls (see more about controls below)

Editing A Sloyd Object In Play Mode

In edit mode you have sliders, checkboxes and buttons to edit a Sloyd object. The same functions can be accessed by code.

There are two types of SloydObject's control collections: unique controls specified for a concrete template and common controls which are same for each template.

Here is the way to get access to these two types of controls:

ControlsCollection uniqueControls = sloydObject.GetControls();
ControlsCollection commonControls = sloydObject.GetCommonControls();

Each ControlsCollection contains different sets of controls:

SliderControl

A container for float value between 0 and 1.

SliderControl slider = commonControls.Sliders[0];
slider.Value = 0.2f;

Example slider (from web app)

ToggleControl

A container for bool value. Typically turns parts off/on, or switches between different states.

ToggleControl toggle = uniqueControls.Toggles[0];
toggle.Value = true;

Example toggles:

SwitchListControl

A container for named and indexed items. Allows the user to switch between various parts of a model.

SwitchListControl switchList = uniqueControls.SwitchLists[0];
foreach (string itemName in switchList.Items)
{
    Debug.Log(itemName);
}

switchList.Value = switchList.Items.Length - 1;

Example switch list:

GroupControl

A container for sets of Slider-, Toggle- and SwitchList controls. A group can be disabled if it has a toggle controller.

GroupControl group = uniqueControls.Groups[0];
  
SliderControl[] sliders = group.SliderControls;
ToggleControl[] toggles = group.ToggleControls;
SwitchListControl[] switchLists = group.SwitchListControls;
  
...
  
if(group.IsToggled && group.ToggleController.Value == false)
{
  group.ToggleController.Value = true;
}
  
sloydObject.RebuildMesh();

Example groups, with and without toggle:

Unique controls are specific controls/parameters for a given generator.

Common controls are shared across all generators.

Random values

You are also able to set random values to whole collection by calling ControlsCollection.Randomize()

commonControls.Randomize();
uniqueControls.Randomize();
  
sloydObject.RebuildMesh();

Important common controls

Currently all Sloyd Objects have following common controls:

LOD – Level Of Detail slider.

const string levelOfDetailSliderName = "LOD";
SliderControl lodSlider = commonControls.GetSlider(levelOfDetailSliderName);
lodSlider.Value = 0.8f;
sloydObject.RebuildMesh();

Wack – controls group with a set of sliders for deforming a Sloyd Object in different ways.

const string wackGroupName = "Wack";
GroupControl wackGroup = commonControls.GetGroup(wackGroupName);

var tiltSideways = (SliderControl) wackGroup.GetControl("RotateZ");
var tiltForwardBack = (SliderControl) wackGroup.GetControl("RotateX");
var taperTopSideways = (SliderControl) wackGroup.GetControl("TaperTopX");
var taperTopForwardBack = (SliderControl) wackGroup.GetControl("TaperTopY");
var twist = (SliderControl) wackGroup.GetControl("RotateY");
var slantSideways = (SliderControl) wackGroup.GetControl("OffsetX");
var slantForwardBack = (SliderControl) wackGroup.GetControl("OffsetZ");
// There are more minor sliders in the group. Enumerate wackGroup.SliderControls

Moving and Rotating A Sloyd Object In Play Mode

You can translate and rotate an object via SloydObject.Position and SloydObject.Rotation properties or directly by using its Transform

sloydObject.Position = Vector3.up;
sloydObject.Rotation = Quaternion.identity;

sloydObject.GameObject.transform.localEulerAngles = Vector.right * 45f;

**Next, see:**AI prompting in play mode (https://doc.clickup.com/d/h/kh4k0-705/ed114594b2af381/kh4k0-7695)