public class FullyCustomAnchorPrefabSpawner : MonoBehaviour
{
public GameObject Prefab; // A prefab to spawn for each anchor
public MRUKAnchor.SceneLabels AnchorsToInclude; // The labels of the anchors for which to spawn the prefab
private void Start()
{
MRUK.Instance.RegisterSceneLoadedCallback(() =>
{
// Get all anchors in the scene
var anchors = FindObjectsOfType<MRUKAnchor>();
// Iterate over each anchor
foreach (var anchor in anchors)
{
// Check if the anchor's labels match the exhibit label
if ((anchor.Label & AnchorsToInclude) == 0)
{
continue;
}
var newPanel = Instantiate(Prefab, anchor.transform);
// Use AnchorPrefabSpawnerUtilities to scale and position the new panel based on the anchor's volume
var trs = AnchorPrefabSpawnerUtilities.GetTransformationMatrixMatchingAnchorVolume(anchor, false,
true, null);
newPanel.transform.localPosition = CustomPrefabAlignment(anchor.VolumeBounds.Value, null);
newPanel.transform.localRotation = trs.rotation;
newPanel.transform.localScale = trs.lossyScale / 40;
}
});
}
@iverbatim
public Vector3 CustomPrefabAlignment(Bounds anchorVolumeBounds,
Bounds? prefabBounds)
{
Vector3 prefabPivot = new();
if (prefabBounds.HasValue)
{
var center = prefabBounds.Value.center;
var min = prefabBounds.Value.min;
prefabPivot = new Vector3(center.x, center.z, min.y);
}
var anchorVolumePivot = anchorVolumeBounds.center;
anchorVolumePivot.z = anchorVolumeBounds.max.z;
anchorVolumePivot.y = anchorVolumeBounds.max.y;
return anchorVolumePivot - prefabPivot;
}
@endiverbatim
}AlignPrefabPivot
(
anchorVolumeBounds
, prefabBounds
, localScale
, alignMode
)
|
Aligns the pivot points of a prefab and of the anchors' volume based on the specified AnchorPrefabSpawner.AlignMode.
See also AnchorPrefabSpawnerUtilities.AlignPrefabPivot(Rect, Bounds?, Vector2, AnchorPrefabSpawner.AlignMode) for aligning a prefab based on the anchor's plane rect.
Example usage:
var anchor = FindObjectsOfType<MRUKAnchor>()[0];
var volumeBounds = anchor.VolumeBounds.Value;
var prefabBounds = Utilities.GetPrefabBounds(prefab);
var prefabSize = prefabBounds?.size ?? Vector3.one;
var scale = new Vector3(volumeSize.x / prefabSize.x, volumeSize.z / prefabSize.y,
volumeSize.y / prefabSize.z);
scale = AnchorPrefabSpawnerUtilities.ScalePrefab(scale, AnchorPrefabSpawner.ScalingMode.Stretch);
var localPosition = AnchorPrefabSpawnerUtilities.AlignPrefabPivot(volumeBounds, prefabBounds, scale,
AnchorPrefabSpawner.AlignMode.Automatic);
The alignment logic modes are as follows:
Signature
static Vector3 AlignPrefabPivot(Bounds anchorVolumeBounds, Bounds? prefabBounds, Vector3 localScale, AnchorPrefabSpawner.AlignMode alignMode=AnchorPrefabSpawner.AlignMode.Automatic) Parameters anchorVolumeBounds: BoundsÂ
The volume bounds of the anchor.
prefabBounds: Bounds?Â
The bounds of the prefab.
localScale: Vector3Â
The local scale of the prefab.
alignMode: AnchorPrefabSpawner.AlignModeÂ
The alignment mode to use. See AnchorPrefabSpawner.AlignMode.
Returns Vector3Â
The pivot points of the prefab and of the anchor's volume.
|
AlignPrefabPivot
(
planeRect
, prefabBounds
, localScale
, alignMode
)
|
Aligns the pivots of a prefab and of the anchor's plane rect, based on the specified AnchorPrefabSpawner.AlignMode.
Example usage:
var anchor = FindObjectsOfType<MRUKAnchor>()[0];
var prefabBounds = Utilities.GetPrefabBounds(prefab);
var prefabSize = prefabBounds?.size ?? Vector3.one;
var scale = new Vector2(anchor.PlaneRect.Value.size.x / prefabSize.x, anchor.PlaneRect.Value.size.y);
scale = AnchorPrefabSpawnerUtilities.ScalePrefab(scale, AnchorPrefabSpawner.ScalingMode.Stretch);
var localPosition = AnchorPrefabSpawnerUtilities.AlignPrefabPivot(anchor.PlaneRect.Value, prefabBounds, scale,
AnchorPrefabSpawner.AlignMode.Automatic);
The alignment logic modes are as follows:]
Signature
static Vector3 AlignPrefabPivot(Rect planeRect, Bounds? prefabBounds, Vector2 localScale, AnchorPrefabSpawner.AlignMode alignMode=AnchorPrefabSpawner.AlignMode.Automatic) Parameters planeRect: RectÂ
The plane rectangle of the anchor.
prefabBounds: Bounds?Â
The bounds of the prefab.
localScale: Vector2Â
The local scale of the prefab.
alignMode: AnchorPrefabSpawner.AlignModeÂ
The alignment mode to use. See AnchorPrefabSpawner.AlignMode.
Returns Vector3Â
The pivot points of the prefab and the anchor plane rectangle.
|
GetPrefabWithClosestSizeToAnchor
(
anchor
, prefabList
, sizeMatchingPrefab
)
|
Selects a prefab from a list that has the closest size to the volume of an anchor.
This method selects a prefab from a list that has the closest size to the volume of an anchor. It first checks if the anchor has a volume. If not, it throws an exception. It then calculates the volume of the anchor and each prefab in the list. It selects the prefab with the smallest difference in size to the anchor's volume.
Signature
static bool GetPrefabWithClosestSizeToAnchor(MRUKAnchor anchor, List< GameObject > prefabList, out GameObject sizeMatchingPrefab) Parameters prefabList: List< GameObject >Â
The list of prefabs to select from.
sizeMatchingPrefab: out GameObjectÂ
The selected prefab with the closest size to the anchor's volume.
Returns boolÂ
True if a matching prefab is found, false otherwise.
|
GetTransformationMatrixMatchingAnchorPlaneRect
(
anchorInfo
, prefabBounds
, scaling
, alignment
)
|
Calculates a transformation matrix that matches the plane rectangle of the anchor.
See also AnchorPrefabSpawnerUtilities.GetTransformationMatrixMatchingAnchorVolume for matching the volume of an anchor.
This method calculates and returns a transformation matrix for a prefab that matches the plane rect of an anchor, by determining the local scale and pose of the prefab based on the rect.
Signature
static Matrix4x4 GetTransformationMatrixMatchingAnchorPlaneRect(MRUKAnchor anchorInfo, Bounds? prefabBounds, AnchorPrefabSpawner.ScalingMode scaling=AnchorPrefabSpawner.ScalingMode.Stretch, AnchorPrefabSpawner.AlignMode alignment=AnchorPrefabSpawner.AlignMode.Automatic) Parameters prefabBounds: Bounds?Â
The bounds of the prefab.
scaling: AnchorPrefabSpawner.ScalingModeÂ
The scaling mode to use. See AnchorPrefabSpawner.ScalingMode.
alignment: AnchorPrefabSpawner.AlignModeÂ
The alignment mode to use. See AnchorPrefabSpawner.AlignMode.
Returns Matrix4x4Â
A transformation matrix that matches the plane rectangle of the anchor.
|
GetTransformationMatrixMatchingAnchorVolume
(
anchorInfo
, matchAspectRatio
, calculateFacingDirection
, prefabBounds
, scalingMode
, alignMode
)
|
Calculates a transformation matrix that matches the volume of the anchor.
See also AnchorPrefabSpawnerUtilities.GetTransformationMatrixMatchingAnchorPlaneRect for matching the plane rect of an anchor.
This method calculates the local scale and pose of a prefab based on the anchor's volume, and then combines these to return a transformation matrix.
Signature
static Matrix4x4 GetTransformationMatrixMatchingAnchorVolume(MRUKAnchor anchorInfo, bool matchAspectRatio, bool calculateFacingDirection, Bounds? prefabBounds, AnchorPrefabSpawner.ScalingMode scalingMode=AnchorPrefabSpawner.ScalingMode.Stretch, AnchorPrefabSpawner.AlignMode alignMode=AnchorPrefabSpawner.AlignMode.Automatic) Parameters matchAspectRatio: boolÂ
Whether to match the aspect ratio of the anchor.
calculateFacingDirection: boolÂ
Whether to calculate the facing direction of the anchor.
prefabBounds: Bounds?Â
The bounds of the prefab.
scalingMode: AnchorPrefabSpawner.ScalingModeÂ
The scaling mode to use. See AnchorPrefabSpawner.ScalingMode.
alignMode: AnchorPrefabSpawner.AlignModeÂ
The alignment mode to use. See AnchorPrefabSpawner.AlignMode.
Returns Matrix4x4Â
A transformation matrix that matches the volume of the anchor.
|
ScalePrefab
(
localScale
, scalingMode
)
|
Scales a prefab based on the specified scaling mode.
see also AnchorPrefabSpawnerUtilities.ScalePrefab(Vector2, AnchorPrefabSpawner.ScalingMode) for for scaling a prefab based on the anchor's plane rect.
Example usage:
var anchor = FindObjectsOfType<MRUKAnchor>()[0];
var prefabSize = prefabBounds?.size ?? Vector3.one;
var scale = new Vector3(VolumeBounds.Value.size.x / prefabSize.x, VolumeBounds.Value.size.z / prefabSize.y
VolumeBounds.Value.size.y / prefabSize.z);
scale = AnchorPrefabSpawnerUtilities.ScalePrefab(scale, AnchorPrefabSpawner.ScalingMode.Stretch);
The scaling logic modes are as follows:
Signature
static Vector3 ScalePrefab(Vector3 localScale, AnchorPrefabSpawner.ScalingMode scalingMode=AnchorPrefabSpawner.ScalingMode.Stretch) Parameters localScale: Vector3Â
The local scale of the prefab.
scalingMode: AnchorPrefabSpawner.ScalingModeÂ
The scaling mode to use. See AnchorPrefabSpawner.ScalingMode.
Returns Vector3Â
The scaled local scale of the prefab.
|
ScalePrefab
(
localScale
, scalingMode
)
|
Scales a prefab based on the specified scaling mode.
See also AnchorPrefabSpawnerUtilities.ScalePrefab(Vector3, AnchorPrefabSpawner.ScalingMode) for scaling a prefab based on the anchor's volume.
Example usage:
var anchor = FindObjectsOfType<MRUKAnchor>()[0]; var prefabBounds = Utilities.GetPrefabBounds(prefab); var prefabSize = prefabBounds?.size ?? Vector3.one; var scale = new Vector2(anchor.PlaneRect.Value.size.x / prefabSize.x, anchor.Plane .Rect.Value.size.y / prefabSize.y); scale = AnchorPrefabSpawnerUtilities.ScalePrefab(scale, AnchorPrefabSpawner.ScalingMode.Stretch);
The scaling logic modes are as follows:
Signature
static Vector3 ScalePrefab(Vector2 localScale, AnchorPrefabSpawner.ScalingMode scalingMode=AnchorPrefabSpawner.ScalingMode.Stretch) Parameters localScale: Vector2Â
The local scale of the prefab.
scalingMode: AnchorPrefabSpawner.ScalingModeÂ
The scaling mode to use. See AnchorPrefabSpawner.ScalingMode.
Returns Vector3Â
The scaled local scale of the prefab.
|
SelectPrefab
(
anchor
, prefabSelectionMode
, prefabs
, random
)
|
Selects a prefab based on the specified selection mode.
This method selects a prefab based on the specified selection mode. If the selection mode is Random, it selects a random prefab from the list. If the selection mode is ClosestSize, it selects the prefab with the closest size to the anchor. If the selection mode is Custom, it uses the provided custom selection logic.
Signature
static GameObject SelectPrefab(MRUKAnchor anchor, AnchorPrefabSpawner.SelectionMode prefabSelectionMode, List< GameObject > prefabs, System.Random random) Parameters prefabSelectionMode: AnchorPrefabSpawner.SelectionModeÂ
The selection mode to use. See AnchorPrefabSpawner.SelectionMode.
prefabs: List< GameObject >Â
The list of prefabs to select from.
random: System.RandomÂ
The random generator used to generate the index of the prefab to be selected
Returns GameObjectÂ
True if a prefab was selected, false otherwise.
|