praeclarum / ShaderGraphCoder

Write RealityKit shaders using Swift
137 stars 2 forks source link

Image texture example #1

Closed cclaan closed 3 months ago

cclaan commented 7 months ago

Thanks for this great library. I was wondering if it's possible to use a local jpg/png image as input to the baseColor / roughness / metallic of SGPBRSurface ( and if so, if you had a snippet laying around ) I made an attempt using .texture2DParameter, but I can't seem to get it working. I assume I should set the material parameter using something like "mat.setParameter(name: "customTexture", value: .textureResource(baseResource))" but I keep getting odd runtime errors. ( RealityFoundation.ShaderGraphMaterial.LoadError.invalidTypeFound ) Thanks

praeclarum commented 6 months ago

Hmm, "RealityFoundation.ShaderGraphMaterial.LoadError.invalidTypeFound" usually indicates a bug in this library. Please update as often as possible.

Tracking down the error can be tricky but watch for any yellow warnings in the log just before the invalidTypeError. Please let me know if you catch any.

I've tested a lot with in-memory textures and there might be a bug for local URLs. I will work on a texture example to find out.

Thanks for the report!

praeclarum commented 6 months ago

I added a texture example to the readme and the tests. I'm still verifying that local resources work but maybe you can compare your code:

https://github.com/praeclarum/ShaderGraphCoder/blob/134cc1f7f00de0747cc28eaf0a9a45dbaf97c351/README.md?plain=1#L26-L43

cclaan commented 6 months ago

Thanks. I tried running your example above, and I get the invalid type error from just running the first 3 lines:

let texture = SGValue.texture2DParameter(name: "ColorTexture") 
let color = texture.sample(texcoord: SGValue.uv0) 
var mat = try await ShaderGraphMaterial(surface: SGPBRSurface(baseColor: color), geometryModifier: nil) 

RealityFoundation.ShaderGraphMaterial.LoadError.invalidTypeFound

I don't see any warnings that seem related.

ynagatomo commented 4 months ago

Thanks for the great library. I encountered the same fatal error, invalidTypeFound, when using texture.sample(texcoord: SGValue.uv0). When using .color3f([1, 0, 0]) instead for example, ShaderGraphMaterial() doesn't throw the fatal error.

praeclarum commented 3 months ago

@cclaan @ynagatomo I'm pleased to let you know I have located the problem. The texture sampling function was returning a color4f where as the surface baseColor is supposed to be a color3f.

This led me down a long rabbit hole of fixing all the types throughout the library. That led me to working on a v2. I just finished that work and hope you'll upgrade to it. https://github.com/praeclarum/ShaderGraphCoder/releases/tag/2.0.0

There are a couple breaking changes, texture2D is now just .texture and .sample is now .sampleColor3f or .sampleColor4f or .sampleVector4f. Also you can just pass textures to the SGValue texture functions.

I also added a ton more type checking so the Swift types are more sound but also the graph is vigorously checked and will throw errors if there are type mismatches. Hopefully invalidTypeFound will be a thing of the past.

ynagatomo commented 3 months ago

Thank you! I put a new issue #5 about textures.