lovr icon indicating copy to clipboard operation
lovr copied to clipboard

Using custom shaders with Models

Open bjornbytes opened this issue 4 years ago • 1 comments

Currently it isn't possible to use per-node or per-material shaders when rendering a Model. You can set a single shader and call Model:draw and that shader will be used for all of the nodes (Materials will update uniforms for that shader).

Some use cases require different glTF materials to use their own shaders.

In glTF, nodes can have a mesh, a mesh has one or more primitives, and a primitive can have a material.

In LÖVR, each glTF primitive is a Mesh with a Material applied to it.

I can see 2 possible approaches

  • Add something like Model:setShader(material, shader). This sets a shader to use for a material. Any primitives using this material will use the shader. If the shader is nil, the shader set using lovr.graphics.setShader will be used.
  • Add a way to render a specific node of a Model. Imagine Model:drawNode(node, [recurse], transform, instances) or a variant to :draw. Because a node can have multiple meshes/materials, this doesn't give the necessary amount of control. However, changing the Shader per-node would still be useful, and this probably enables lots of other interesting things too. If this was added, there probably also needs to be some way to get the parent/children of a node. The ultimate goal is to be able to write Model:draw entirely in Lua, so that you can change how it works if you need to.

These aren't mutually exclusive. Both would probably be useful.

bjornbytes avatar Feb 21 '21 16:02 bjornbytes

This is implemented on the gpu branch -- when drawing a Model, you can specify a single node to render (default: root) and whether you want to render its children as well (default: true).

bjornbytes avatar Mar 31 '22 05:03 bjornbytes