Sunday, January 3, 2016

unity - How can I make a trail renderer flat with the normal of the nearest face?


I'm making a basic skid system for a car model using a trail renderer. I have everything setup and working properly except that the trail renderer always faces the camera. I want the trails to be perfectly flat with the ground. So I thought if unity can make the faces all rotate towards the camera, then would it be able to rotate towards the normals of the nearest faces?



Answer



Well, the thing is, trail renderers (and the underlying line renderer) are supposed to face the camera.



There are a couple ways I can think of doing this, but I'm humming over which one would be the best.


One way would be to buy a decal system (there are a couple on the asset store for under $10, at least one which is free). The tracks left via decals likely aren't the best for a car, but might be "good enough" for your usage. Also search the store for anything purpose-built for tire tracks, I suspect someone's already done it.


Alternatively, you could create your own (vertex) shader to render the lines in the way that you want. You'd want to base it off the trail and line renderer system, although I can't say for sure where to start. The one (and only) time I've done this kind of thing myself was to turn a 3D mesh into a collection of lines which did face the camera. Doing the reverse would be possible with some effort, though.


(Now that I think about this, I think the LineRenderer component does the mesh construction in the component before it hits the shader: that'd be the better thing to fiddle with than trying to undo that with a vertex shader. Thus, idea #3).


Third, you can manually construct a triangle strip yourself. You'll have to weigh the cost/benefits of doing this over buying one off the asset store. $10 vs. 10 hours is heavily in favor of just buying it. But it shouldn't be that hard to compute the points yourself. Just remember to keep the tris slightly above the actual terrain or you'll get Z-Fighting.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...