Hello,
I’m trying to create an object with a MID that reacts to hits the actor receives in world space. Ideally, each hit would apply a masks/lerps on a set of base textures, revealing “underlying” textures like gravel under dirt, or “adding” additional textures like mud smeared on a rock. These hits need to accumulate on the material, ie exposed gravel might be smeared with mud, or multiple mud smears should coexist.
Currently, I have a simple form of this MID working for a single hit. I essentially perform a custom sphere mask using a hit world vector parameter and the per-pixel AbsoluteWorldPosition input parameter. This works great, I can feed in hit vectors dynamically and I can get the exact masking effect I want with my example material.
Now, though, I’m working on accumulating multiple hits on my material, and I’ve hit some problems. It looks like there’s no loops, iterations, or array traversal in materials without resorting to custom shader nodes. My plan was instead to iteratively render hit materials as they happened to a CanvasRenderTarget2d, and then use the final rendered texture as a texture parameter in a main material for my mesh. Instead, when I render my material to a CanvasRenderTarget2d I get a solid black (all masked) material instead of the hit “splatter” I have on my hit test mesh.
Troubleshooting of note:
- I made sure my hit material outputs in the emissive channel so it can be rendered by Draw Material
- Used two CanvasRenderTarget2ds to buffer my rendering to make sure I’m accumulating draws instead of overwriting them.
- Removed the AbsoluteWorldPosition input node from my hit calculations in the material and replaced it with a hard-coded world coordinate. This did not do the same thing (I get one world space position for hit masking instead of each pixel’s world space position), but immediately my CanvasRenderTarget2ds started displaying my hit material instead of all black.
My guess is that when I go to render my MID on the CanvasRenderTarget2d, it uses the canvas’ Absolute World Position instead of my original mesh’s. This means the hit is outside the detection radius of my custom sphere mask, so I get black/zero value instead of a masked texture value.
Is there a way to render my MID on a CanvasRenderTarget2d using cached vertex locations from my MID’s parent mesh to ensure my spheremasks compute properly? Is there some other way besides using CanvasRenderTarget2d that I can accumulate multiple executions of my MID to a single texture?