I think this came up another time in a different thread but Craft doesn’t support buffers because we use a less flexible but more efficient vertex steam format (interleaved vs separate streams). The only places you can pack extra data in as it stands are the uv attribute and color attribute. The colors are limited to 1 byte per channel though so it’s not that great. The modern runtime (Carbide) fixes this by giving you access to compute buffers but obviously if you’re running on the legacy runtime you can’t access those. I think there is also a second uv attribute in Craft so you that would give you 4 floats and 4 unorm bytes to work with. What sort of stuff did you normally put inside buffers?
assert(OrbitViewer, "Please include Cameras as a dependency")
scene = craft.scene()
for x=grid,-grid,-1 do
for y=grid,-grid,-1 do
if f>180 then
for x=grid,-grid,-1 do
for y=grid,-grid,-1 do
pt.model = craft.model.cube(vec3(1.2,.6,1.2))
pt.material = craft.material(asset.builtin.Materials.Basic)
@dave1707 it’s great, but this it’s not a shader that can then be applied to any different model I choose. For example with mesh you can have shaders that make meshes do an explosion effect and then you can easily apply it to any model you want to see explode.
If I recall correctly one of your projects did a similar thing in Craft, but since you can’t animate values in Craft shaders, you didn’t use a shader at all, you did it by ‘manually’ replacing every single triangle in the original entity’s model with a whole separate entity and model of its own, and then animating those entities individually. Like you have to do in the code you just posted, too.
I don’t know anything about the efficiencies @John was referring to, but it seems at least possible that if you’re making an explosion by animating a thousand different individual entities, as you have to do with Craft, versus making an explosion by applying just one shader to just one entity, as you can do with meshes—it seems possible that the more efficient way is the second way.
Granted this borders on the ridiculous, but could you in theory emulate the power of mesh buffers for use in Craft shaders by generating a different Craft shader for every ‘frame’ you wanted to animate, and then changing the model’s shader every draw() cycle? It’d be wild, but it’d theoretically make it possible to truly update any pre-Craft project to use Craft.
I don’t see how this would be any different than just having a single uniform value driving the animations you want. Multiple shaders don’t give you any extra features. Buffers are just additional per vertex data, which is useful for the kinds of animations you are talking about but not a requirement.
In the undulate example, you could encode the distanceFromCenter attribute into the uv vertex attribute and set that via the model’s uvs giving you exactly the same effect. The main limitation being you couldn’t then use it for texturing, or if you wanted a larger number of per-vertex attributes. In that way mesh buffers are more flexible for sure
Are you talking about changing vertices within the shader or in the Lua code? Because mesh shaders and Craft shaders can both do those things and always have. Buffers give you more custom per-vertex data to work with but you can still just modify vertices in the vertex shader using uniforms/properties in both or based on other vertex attributes. Both shaders types are just GLSL shaders, there’s nothing special about them except in how data gets passed in. When I get some time I can show you how to modify your example so that the craft undulation shader works using a spare vertex attribute (like uv2 or something)
The values you are talking about are vertex attributes. Every vertex in your model no matter what it is has per vertex data. It’s up to the programmer to define what that data is. At a minimum you’ll want position data (which we have), but you’ll generally want other things like colours, uvs and stuff. In the original mesh type I added buffers so that you could define extra per-vertex data beyond what is normally available
The shader takes this per-vertex attribute data and transforms it before it gets drawn to the screen. It doesn’t recalculate anything it just passes the data on to the next stage of the shader pipeline. So you can’t use a vertex shader to write back to this data. What you can do with buffers in codea is just send new data to the gpu every frame. Usually when you want stuff to vary each frame you’ll use uniform values (values that are shared for all vertices within the shader). In Codea we call those properties and you can just set them directly, but they don’t vary for each vertex. If you repurpose an existing channel like color or uv/uv2 you can use it in the shader to drive other visible effects like position offsets, colors or textures in different ways
Honestly you might want to look into some books on shader programming to give yourself a better idea of how they work since it’s a little beyond the scope of a forum post. I recommend this free book: https://thebookofshaders.com/