UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

RealityKit Materials: Basic Gradient

Forums > SwiftUI


I’m trying to make a material in RealityKit that has a basic gradient. I am making an iPadOS app.

A few thoughts:

  • I cannot use Reality Composer Pro to do this because the Shader Graph tool only works for visionOS.
  • I cannot use a Metal file to create a shader because I am using a .swiftpm (app playgrounds) file targeted for Swift Playgrounds. Metal files don’t seem to work on Swift Playgrounds (it’s a Swift playground, after all).
  • I would prefer to not use image textures for a simple thing like this. That would take up storage. I wish it was as easy as applying a .basecolor with a UIColor, but UIColor does not support gradients.

What are my options? I know my requirements are likely not typical, but I really need to try to not break those.

I looked into CustomMaterial from RealityKit but once again, those take Metal shaders. Amazing tool, but I sadly cannot use them because I’m using a Swift Playground that doesn’t seem to with Metal files, at least it seems.

I’ve briefly done research on MetalKit? Could that help me out?

Let’s say I have a simple box in RealityKit. How would I apply a simple gradient to it given my constraints?

I really appreciate the help.

P.S. This is a SwiftUI project for reference.


Could I create a texture without images, perhaps by making a view into a texture and applying it? How would I do this? What are the pros and cons of this?


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.