Yes, sorry. I've not previously used the forum, and could not find the original question, so posed it again.I've found it now. Thank you.
Post
Replies
Boosts
Views
Activity
Thank you, this helps, and certainly works with the documented example!(As you say, it depends on how the data are being used, and my example is more complex than the one in the documentation...I need to try to understand how this works using the newly defined dense vector later in the code, where it now does not seem think the vector has been defined. For example, in the code you provided, the vector is printed, via the stride command, within the braces of the values.withUnsaftMutableBufferPointer construct. But if the brace ends before the stride command, the error is that "vector" is unresolved. I think my lack of understanding is from coming from Fortran to Swift, with need of0 more study of structs and classes...)Anyway, many thanks!
Thank you, this helps, and certainly works with the documented example!(As you say, it depends on how the data are being used, and my example is more complex than the one in the documentation...I need to try to understand how this works using the newly defined dense vector later in the code, where it now does not seem think the vector has been defined. For example, in the code you provided, the vector is printed, via the stride command, within the braces of the values.withUnsaftMutableBufferPointer construct. But if the brace ends before the stride command, the error is that "vector" is unresolved. I think my lack of understanding is from coming from Fortran to Swift, with need of0 more study of structs and classes...)Anyway, many thanks!
In response to songyeah's note: "Thanks for sharing the code. But the code is not working for me, got errors like "Cannot find type NSViewRepresentable in scope".
I just tried the posted MetalView struct, and it ran fine with the macOS simulator. However, switching to the iOS simulator, I get the same error that you described. I then changed all of the NSView... commands to instead be UIView... commands, and it now runs with the iOS simulator.
(By the way, this code is very helpful for me!) Thanks.
In trying to answer my own question, I have developed a mostly satisfactory solution using the LinearGradient with SwiftUI and a ZStack. It's not quite the quality of the solution that the Metal documentation: "Using a Render Pipeline to Render Primitives" (my solution is too bright in the center of the triangle) and I don't know how this might scale over hundreds of thousands of triangles, but it gets me started. Thanks to those who looked into my issue.
Just in case someone is interested, here's my code:
import SwiftUI
struct Triangle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: 150.0, y: 0.0))
path.addLine(to: CGPoint(x: 0.0, y: 300.0))
path.addLine(to: CGPoint(x: 300.0, y: 300.0))
path.addLine(to: CGPoint(x: 150.0, y: 0.0))
return path
}
}
struct ContentView: View {
var body: some View {
let colorsTop = Gradient(colors: [Color(red: 0, green: 0, blue: 1), .white])
let colorsLeading = Gradient(colors: [Color(red: 0, green: 1, blue: 0), .white])
let colorsTrailing = Gradient(colors:[Color(red: 1, green: 0, blue: 0), .white])
let gradTop = (LinearGradient(gradient: colorsTop, startPoint: UnitPoint(x: 0.5, y: 0.0), endPoint: UnitPoint(x: 0.5, y: 0.6)))
let gradLeading = (LinearGradient(gradient: colorsLeading, startPoint: UnitPoint(x: 0.0, y: 1.0), endPoint: UnitPoint(x: 0.5, y: 0.6)))
let gradTrailing = (LinearGradient(gradient: colorsTrailing, startPoint: UnitPoint(x: 1.0, y: 1.0), endPoint: UnitPoint(x: 0.5, y: 0.6)))
VStack {
// If the background is white, the blendMode is multiply
ZStack {
Triangle()
.fill(gradTop)
.frame(width: 300, height: 300)
.blendMode(.multiply)
Triangle()
.fill(gradLeading)
.frame(width: 300, height: 300)
.blendMode(.multiply)
Triangle()
.fill(gradTrailing)
.frame(width: 300, height: 300)
.blendMode(.multiply)
}
}
// if the background is black, the blendMode is difference
ZStack {
Triangle()
.fill(gradTop)
.frame(width: 300, height: 300)
.blendMode(.difference)
Triangle()
.fill(gradLeading)
.frame(width: 300, height: 300)
.blendMode(.difference)
Triangle()
.fill(gradTrailing)
.frame(width: 300, height: 300)
.blendMode(.difference)
}
.background(Color.black)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hi: This thead was quite helpful with my own code, thank you!
With the newly announced (WWDC 2023) metal shaders available in SwiftUI, I wonder how the standalone example that creates its own MTKView would be updated with the latest SwiftUI? It is not obvious, at least to me...
I also find the documentation difficult to follow. A full example of the triangle that is shown in the documents would be quite helpful.
Thank you, this is perfect! (It should be included in the documentation...) As a followup, will the use of the shader graph material be as efficient as metal for millions of triangles? If not, could you provide an example of how to do the same thing, starting with the example in the documentation, and blending the vertex colors over the face of the triangle, but with metal instead of the shader graph?