Poikilos / KivyGlops

Manages 3D meshes, their transforms, and camera movement and rotation all in discreet objects, with working Kivy subclasses
http://www.expertmultimedia.com/usingpython/py3
MIT License
15 stars 4 forks source link

Make per-object variables (rather than uniform which is the same from glStart to glEnd or per-vertex which is too fine) #1

Open Poikilos opened 6 years ago

Poikilos commented 6 years ago

Is there a way to add a uniform (accessible to shader) other than canvas["something"]? I have to make it only for InstructionGroup or PushMatrix so it is specific to one Mesh (because I am working on a 3D viewport). The reason why is clear here: https://github.com/expertmm/KivyGlops/blob/master/shaders/fresnel.glsl#L67 (There was no shorter or one-file example I could imagine) related python code is here: https://github.com/expertmm/KivyGlops/blob/master/kivyglops.py#L678 KivyGlop is a subclass of widget but to get it working I had to set canvas to InstructionGroup() as advised on kivy IRC channel, so I can't set the uniform like canvas["texture0_enable"] anymore (except for the parent canvas, but that is not helpful since I need the uniform to apply to only one Mesh)

Poikilos commented 3 years ago

Use a texture as a lookup:

2021-01-08 via the Kivy Discord server #support0 channel:

@poikilos said:

Yrs ago, I was trying to set a shader variable on a per-object basis (rather than a per-vertex or per-scene basis). Someone on IRC (I think) suggested is only possible when each object is a separate context (See instances of _multicontext_enable boolean in https://github.com/poikilos/KivyGlops for examples). It is a little slow though, it seemed, at least on some older machines I was testing, Core 2 Duo with Intel integrated video, but may have also been noticeable on GeForce GT430. I am concerned that low-end mobile devices may also have the problem but I haven't tested any mobile devices. Is there any other way to accomplish changing the value of a variable on a per-object basis without separate contexts? I couldn't find a way in Kivy to access something like OpenGL's set variable function, which it seemed like at least in OpenGL can be placed between objects like any other instruction, correct me if I'm wrong.

@tshirtman said:

you can set uniform variables from the canvas setting a dynamic number of objects into your shader and having a set of value for each is a bit painful, because you can't really have dynamic arrays in the shader the way i experimented with in my branch to improve svg support, is to actually use a texture object defined from python side, and bind it to the shader, so the shader can lookup values by pixels, it's a bit of a pain because you have to know the size of the texture (so you need a variable for that) and you need to compute the position in the texture for that.

@poikilos said:

Ok, in your branch of kivy? Is there a specific branch name or can you point me to the right file?

@tshirtman said:

https://github.com/kivy/kivy/blob/svg_improvements/kivy/graphics/svg.pyx#L864 https://github.com/kivy/kivy/blob/svg_improvements/kivy/data/glsl/svg_fs.glsl#L72-L84 it's quite a mess, and not really commented, but there are working ideas in there allowing to pass data between python and the shader in a less constrained way. i used that to send the parameters of svg gradients to the shader so they can be computed in there

@poikilos said:

Ok, thanks for the ideas. I plan to add an id integer to my vertex format anyway for making 3d objects clickable, so that will help with the lookups I think. Maybe that idea will help you, maybe not.

@tshirtman said:

i didn't touch that branch in some time, mostly because i get issues with matrix transformations that prevent the thing to look correct, and i'm not sure where it needs to be fixed, but it's a but stupid, as the technical challenges are mostly resolved i believe maybe some day...

@poikilos said:

Thanks.

@tshirtman said:

there are certainly better solutions to pass data around if you don't limit yourself to gles2 like kivy does, btw, i'm absolutely not an opengl expert

@poikilos said:

Just so you know, nskrypnik's kivy-3dpicking is what I'll use--the example may be helpful to you in some way. His is actually done "wrong" even though the tutorial was originally called picking objects "the right way"--you can use the same technique he used but use an id from the vertex format instead of the color, from what I could tell. (He doesn't have an ID but I will try to add that) I just forked it so I will run 2to3 and stuff later tonight, so if you come back to it later, try mine instead to save yourself some time: https://github.com/poikilos/kivy-3dpicking

. . .

@poikilos said:

@tshirtman I got my fork of kivy-3dpicking working in Python3 ( ^ link above), and @spinningD20 mentioned https://github.com/kpiorno/kivy3dgui--that may do it a different way but I'm sure you know about that one.