Hi guys I m looking to use Open Brush
Hi guys, I'm looking to use Open Brush materials and shaders in Unity for objects that were actually made in Unity - but I've had limited success. Is there a particular way Open Brush assigns materials to the gameobjects which could help with this?
46 Replies
(just moving this to a thread)
And thank you for getting back to me so quickly.Sorry for the delay @Palaeolak - you rather jinxed it by saying this!
Coarse Bristles is the one I'm trying it out on. Although I'll be looking to try out a range of different materials once I know how to do this.OK. How familiar are you with Unity's shader code? The quickest thing is to look at the shader itself but I think most brush shaders are pretty similar in how they use UV coordinates. Coarse Bristles uses "DiffuseDoubleSided" as it's shader. It's a fairly simple shader: https://github.com/icosa-foundation/open-brush-toolkit/blob/03f11b2ffd2625ca0145f8d0ef97ca0e3f190100/UnitySDK/Assets/TiltBrush/Assets/Brushes/Shared/Shaders/DiffuseDoubleSided.shader
Here's the brush material assigned to a normal unity quad:
I have a tendency to jinx most things in my life, lol.
I've given my conundrum a fresh look this morning. I created an object from scratch (a cube) and applied the coursebristles brush to it. It worked.
So the problem is specific to the objects I'm trying to apply it to. These objects were created from a bit of code I helped write that would separate a gameobject into many other objects on the basis of the muber of faces the mesh has. I.e. One new object per one face.
Here's a comparison:
at the top is a normal unity quad.
at the bottom is a brush stroke drawn in Open Brush. However I've split it into quads and separated them slightly so it's easier to see what's going on
The shader looks normal - so all the magic is happening when the mesh is generated
In this case it looks like the UV mapping is just randomly choosing one of the four quadrants from the texture for each quad
SprayBrush is the script used to generate the mesh: https://github.com/icosa-foundation/open-brush/blob/b99ced6ca62b9b782d1a4a84c8784232c3a78f90/Assets/Scripts/Brushes/SprayBrush.cs
(which is also used by Coarse Bristles, Dot Marker, Leaves, Splatter and Leaves2)
That script seems to just generate quads like this:
so the script, the texture and the shader work together but the script that gnerates the mesh is what does most of the work to make this look the way it does
you'd want to assign your uvs so they randomly map one of the four source images (i.e. one quarter of the main texture)
Hey man, sorry for the late reply. Thank you for getting back to me. This type of thing will require me to look into it further as I don't really understand this part of unity (or open brush).
FYI, this is the script I'm using....
Doesn't look like you're assigning any uv coordinates at all in that script
Oof, looks like we've hit the problem.
what's it meant to do?
ah - it's modifying an existing mesh - not generating a new one
I have no idea how to assign uv coordinates - perhaps I'll ask chatgpt to help
it's helped me in the past
gimme the high level "what this script does" explanation. Split a mesh into separate parts?
This bit here:
So, the script splits an existing gameobject into many new gameobjects on the basis of (for example) one new object per face of the mesh
that's essentially what it does
it needs something like:
subMesh.uv = uv
Lemme try adapting it. Fyi, I'm not much of a coder
in case you haven't guessed that already, haha
and then after Vector3[] vertices = mesh.vertices;
something like
var uv = mesh.uv;
same for uv1 and uv2 probably (can't remember how many we use - uv on it's own will give you the texture mapping back)
BUT - this still relies on the original mesh being mapped the way the shader expects. Where's the original mesh coming from?
and why are you splitting it?
the original mesh is from on openbrush export
right - so the uvs are probably correct. we just need to copy them over
I'm splitting it as I'm developing a game whereby the player walk alongs and activates game objects automatically by coming into close contact with them
Ok, I'm already lost, haha
there's already a script included for splitting up brush strokes - you should probably use that as a starting point: https://github.com/icosa-foundation/open-brush-toolkit/blob/main/UnitySDK/Assets/TiltBrush/Scripts/Editor/EditorUtils.cs
GitHub
open-brush-toolkit/UnitySDK/Assets/TiltBrush/Scripts/Editor/EditorU...
Scripts and assets that help you use Open/Tilt Brush data in your creative projects. - icosa-foundation/open-brush-toolkit
look at line 146
that's what's missing in your script
however - there's another solution!
if you group things in Open Brush before export - they come in as separate meshes
this might be another classic https://xyproblem.info/
I think I've come across this in the unity editor
never start by asking how to do something - start by explaining your end goal. the path to that might not be the one you expect
It's something I've learned to do the hard way.
by failing so many times, lol
Looking back at your original question:
Hi guys, I'm looking to use Open Brush materials and shaders in Unity for objects that were actually made in Unity - but I've had limited success. Is there a particular way Open Brush assigns materials to the gameobjects which could help with this?It seems that you are really asking "how do I split strokes into separate gameobjects?" which i probably could have answered in about 60 seconds!
Ok, what's the main answer there?
I've been using layers and differing colours in open brush to help with this, but it's taking some time
group things
not layers - groups
So by creating groups while in open brush, this would export those groups into separate objects?
or separate children of parent objects?
i believe so, yes
every group is given it's own mesh - there's no hierarchy beyond that
Ok. So, to push things a little further, I would want each brushstroke to be a separate child object
so - we split on brush type and group. that's it (as well as "damn - this is too big, let's split it")
child of what?
of the parent object
yes obviously
but what parent?
So I export an object from open brush into unity. The abject has children object assigned to it
that object*
yes.
listen - why not just try it?
then we can discuss whether it does what you need or not
Fair enough, sounds like a plan.
By the end of this the exported object should have lots of children within it. So that as the player walks along the objects become visible - so it looks like its being painted/sculpted
ok. so you're actually asking a different question!
"how can I animate the drawing in gradually over time?"
if you'd asked me that earlier - I would have given you a different answer again! 😉
does the order you want them animated match the order they were drawn?
i.e. is it the same as replaying the act of drawing?
so - you don't need to split strokes to do this. you can do it all in the shader.
each stroke's vertex has a timestamp for the stroke itself and the vertex - so by combining these you can hide/show parts of each mesh in the order things were drawn
No, this isn't what I'm looking for.
Imagine this, the children of an object are off, but when a player comes into close vicinity with the object, the children are turned on one by one. I have the script for that already. But, the issue I have, is that the object doesn't always have many children within it.
Ideally, I want the object to have a few hundred children assigned to it.
why not do it with shaders?
I have zero idea how to accomplish this within a shader
do things disappear again when the user is further away?
Yes, I have the script for that sorted - they appear and reappear depending on how close the player is to the object. There is also an element of randomness to the colour and shader assigned to the children object.
the line
if (distance > _Radius) discard;
is doing most of the work here