In the spirit of collaboration and knowledge sharing, I've decided to open source a selection of helper functions that I've developed during my journey of creating the first visionOS experience using RealityKit.
![Screenshot 2023-11-11 at 19 18 44](https://private-user-images.githubusercontent.com/1416917/282263183-888af853-779a-414f-8914-d069563cec99.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwMzI3ODEsIm5iZiI6MTczOTAzMjQ4MSwicGF0aCI6Ii8xNDE2OTE3LzI4MjI2MzE4My04ODhhZjg1My03NzlhLTQxNGYtODkxNC1kMDY5NTYzY2VjOTkucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMTYzNDQxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZTFiYzM1MDIxMTE2ZGQ2YTE2NjY2NGIwNDc5NWVhY2JhMTg1YjNhNmQwNmYwMzhiZGMzMzMxOTVhMzE1ZmE0YiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.ahxcerx4zpJrmpTekqFECKbiJF7E0L6YiRTYRUvsAPU)
![Screenshot 2023-11-11 at 19 53 00](https://private-user-images.githubusercontent.com/1416917/282263167-1768bc36-6d73-4400-a970-8e957ec86dca.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwMzI3ODEsIm5iZiI6MTczOTAzMjQ4MSwicGF0aCI6Ii8xNDE2OTE3LzI4MjI2MzE2Ny0xNzY4YmMzNi02ZDczLTQ0MDAtYTk3MC04ZTk1N2VjODZkY2EucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMTYzNDQxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OTFhZDc5NTYyMDI2ZmYxNmNlMjZhMWYxOWZiNWQ2YmE0YjQ5YzhkMTJjZGQzNWE2NDgyN2E5NjYyNDk5NWI4MiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.B0pEQYlLtSnR2HbxWUZdWbI2jKTyqa4PAwqd8VqRBSI)
yourEntity?.visualizeBones()
![Screenshot 2023-11-16 at 23 23 18](https://private-user-images.githubusercontent.com/1416917/283623044-05949eed-b649-4372-abc9-993577cbae9f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwMzI3ODEsIm5iZiI6MTczOTAzMjQ4MSwicGF0aCI6Ii8xNDE2OTE3LzI4MzYyMzA0NC0wNTk0OWVlZC1iNjQ5LTQzNzItYWJjOS05OTM1NzdjYmFlOWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMTYzNDQxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWEwNzc1MzJhZWFlMWYyNDJiNDNjZGFkNmFhMTZlZjBmOGM5ZjllYTE1ZDkxMTliNTQ1ZWJhZTFmZTk3ODBlZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.dxsMo90gmJtaESXgz4H25cTAVwZKFOaJ_CwK-ukkXiw)
-
visualizeBones(size: Float = 0.5)
- Visualizes the bones of a model entity by creating a debug entity with a cone mesh representing each bone.
-
createEntityBox(_ color: UIColor, size: Float) -> Entity
- Creates a box-shaped entity with the specified color and size.
-
update(shaderGraphMaterial oldMaterial: ShaderGraphMaterial, _ handler: (inout ShaderGraphMaterial) throws -> Void) rethrows
- Updates the shader graph material of the entity by applying the changes defined in the handler closure.
-
replaceAndStoreOldMaterials(material: Material, copyPBRInputs: Bool = false)
- Replaces the current materials of the entity with a new material and optionally copies the inputs from the physically based rendering (PBR) materials.
-
restoreOriginalMaterials()
- Restores the original materials of the entity that were saved before replacement.
-
saveMaterialParam(paramName: String)
- Saves the parameter value of the shader graph material with the given name.
-
getMaterialParam(paramName: String) -> MaterialParameters.Value?
- Retrieves the value of the material parameter with the given name.
-
setMaterialParamWeight(paramName: String, value: Float)
- Sets the weight of the material parameter with the given name, multiplying it by the saved value if it exists.
-
setMaterialParam(paramName: String, value: MaterialParameters.Value)
- Sets the value of the material parameter with the given name.
-
playAllAnimations(shouldLoop: Bool = false)
- Plays all available animations for the entity, with an option to loop them indefinitely.
-
distance(from other: Entity) -> Float
- Calculates the distance between the entity and another entity.
-
distance(from point: SIMD3<Float>) -> Float
- Calculates the distance between the entity and a point in 3D space.
-
isDistanceWithinThreshold(from other: Entity, max: Float) -> Bool
- Checks if the distance between the entity and another entity is within a specified threshold.
-
isDistanceWithinThreshold(from point: SIMD3<Float>, max: Float) -> Bool
- Checks if the distance between the entity and a point in 3D space is within a specified threshold.
-
findParentWithName(_ findName: String) -> Entity?
- Searches for a parent entity with the specified name.
-
findParentWithAnyComponents(withComponents componentClasses: [Component.Type]) -> Entity?
- Searches for a parent entity that has any of the specified components.
-
findParentWithComponent<T: Component>(withComponent componentClass: T.Type) -> Entity?
- Searches for a parent entity that has the specified component type.
-
findFirstComponent<T: Component>(withComponent componentClass: T.Type) -> T?
- Searches for the first component of the specified type in the entity hierarchy.
-
hasComponentInHierarchy<T: Component>(withComponent componentClass: T.Type) -> Bool
- Checks if any entity in the hierarchy has the specified component type.
-
forEachDescendant<T: Component>(withComponent componentClass: T.Type, _ closure: (Entity, T) -> Void)
- Iterates over all descendants with the specified component type and performs the given closure.
-
forEach<T: Component>(withComponent componentClass: T.Type, _ closure: (Entity, T) -> Void)
- Iterates over the entity and its children with the specified component type and performs the given closure.
-
removeComponentFromHierarchy(componentType: Component.Type)
- Removes the specified component type from the entity and its descendants.
-
fixObjectPivot() -> Entity?
- Fixes the pivot of the entity by creating a new entity at the visual center and re-parenting the original entity to it.
-
generateCollisionsForEnabledOnly()
- Generates collision shapes for the entity and its children if they are enabled.
-
generatePreciseCollisionsForEachEnabled<T: Component>(withComponent componentClass: T.Type) async
- Asynchronously generates precise collision shapes for each enabled entity with the specified component type.
-
generatePreciseCollisionShapes() async
- Asynchronously generates precise collision shapes for the entity if it is enabled.
-
stopEmittingParticleEmitters()
- Stops all particle emitters from emitting particles in the entity hierarchy.
-
removeAllParticleEmitters()
- Removes all particle emitter components from the entity hierarchy.
-
playAllParticles()
- Starts emitting particles for all particle emitters in the entity hierarchy.
-
addMeshOutline(outlineMaterial: RealityKit.Material, offset: Float)
- Adds an outline to the mesh of the entity using the specified material and offset.
-
makeMeshResourcesUnique()
- Makes the mesh resources of the entity unique to avoid sharing them with other entities.
-
addDoubleSide()
- Adds double-sided rendering to the entity's mesh.
-
addDoubleSide(otherSideMaterial: RealityKit.Material, ignoreComponentTypes: [Component.Type]?)
- Adds double-sided rendering to the entity's mesh with the specified material for the other side, ignoring entities with specified component types.