Batching overrides any custom Shader Chunk


#1

Hi.

I’ve been trying to set up my grass shader seen here:

Trying to get it to work with batching. Because obviously the draw calls get too big either way.

If I set the grass blade entity to a batching group, the shader animation stops working - as if the code was using the default shader chunk.

I’ve searched a lot into this topic but didn’t see any solution. Is there any way of making Batching work with custom shader chunks?

Here is the public project I set up to test this up: https://playcanvas.com/editor/scene/710968


#2

Don’t have code ready, but if you study how the batcher system works you will see that it just creates grouped meshInstances.

I think you will find a reference to them on the batch group object.

From there you will need to override their material, set the custom shader chunk and update it.

I feel that will work.


#3

Looking at your code might be even simpler, if you don’t clone the original material, and just override it, you might not need to find the batched mesh instances and reassign the material.


#4

I’ve tried that but it doesn’t work (I think).

I’m currently trying to find the Batch. I see that you can get the Batch Group using app.batcher.getGroupByName BUT, that doesn’t reference the Batch in any way, it’s just the Batch Group that you create using the editor.

No idea how to find the actual batch and access its meshInstance.


#5

So, doing console.log(this.app.batcher) I can see all of the properties of BatchManager, which some aren’t listed in https://developer.playcanvas.com/en/api/pc.BatchManager.html

I need to access _batchList which I can access with the Chrome Dev Tools but not from a script for some reason (it returns an empty array).

Totally lost right now, undocumented properties… then empty arrays when clearly it’s not empty.


#6

If you are accessing them in the initialize() method most likely they are empty because batches haven’t been generated yet.

I will let you know if I find something.


#7

Yeah, i’ve progressed a tad after that post. But still got nowhere tbh.

I set the console log to delay by 1 second, then I could access the properties. HOWEVER, setting the material shader params like I would set it on the individual mesh Instances material, doesn’t seem to work, it doesn’t do anything.

The material that the batch uses is correct and has the custom shader chunks, but still it doesn’t get the parameter updates.


#8

Actually, the transformVS (vertex shader chunk) isn’t correct, it has been overriden to default.

When I try to set it directly on the batch meshInstance, the mesh disappears from the scene.
When I try to set it to a temporary material and then set the material to the batch meshInstance, the same happens…

So confusing and frustrating :’)


#9

Ooooookay I fixed it, phew!

So apparently checking “Dynamic” in the Batch Group is only for Skinned Meshes, that’s why it was crashing and disappearing when I placed my grass vertex shader.

I thought “Dynamic” was to be set for all moving objects.


#10

So hmm not fully fixed, when not “Dynamic”, the meshes can’t even look at the camera, they’re static.

Gonna see how to fix…


#11

Tired for today. It works when “dynamic” is disabled. Crashes with “dynamic” enabled.

I guess i’ll have to do the billboarding in the vertex shader to avoid having to use lookAt.


#12

So apparently checking “Dynamic” in the Batch Group is only for Skinned Meshes

Batching doesn’t currently support skinned meshes.

Dynamic must mean something else.


#13

That is pretty weird.

So I still don’t know why my vertex shader crashes when checking as “Dynamic”. I will continue testing and researching today.

Worst case is we can’t use Dynamic batching with custom shader chunks, and have to do all of the orientation stuff on the vertex shader, also have to research how to do that.


#14

Okay this time it’s 100% fixed. Grass is running on Dynamic batching and I’m still doing billboarding with lookAt instead of doing it in the vertex shader.

However I have to figure out how to do billboarding in the vertex shader because the lookAt function is taking a lot of time with thousands and thousands of grass quads.

Before, rendering 500 grass quads would result in very bad framerate. now 5000 is still playable.

Gonna keep improving, let’s see if doing billboarding on the shader lets me up that number even more.


#15

With @Mr_F’s help I made it to +500k grass with playable performance!

Current grass project only generates 15k thought, no need for more to see a beautiful grass field.


#16

Applied the result to one of my current projects (with some improvements that are not in the public grass project):

(twitter’s video compression really hurt the little visual details…)