UE4 Moving vertices towards camera position?

Gentelmens, im stuck… :pensive:

How we can (if we can…) move vertices towards camera position in shader?

Which engine?
UE4 comes with the camera offset material function which is pretty much that.

Ops, sorry, im forgot to mention this. Im on UE4.

Yeah, camera offset function do the trick. Funny what im already try to use it but fail because of mess with vertex paint.
More sadly what correcting vertex position that way cause huge stretching of UV coordinates :pensive:

What are you actually trying to solve? Beware of the XY problem.

1 Like

It depends on if you want to move the vertices independently (this possibly cause stretching of the object) or together as one object.

Pseudo math
float offsetAmount = 1.0f;
// get camera look direction, all positions in [worldspace]
// - if you want each vertex to be offset independantly, use vertex position
// - if you want vertices to share offset, as one object, use object position instead
cameraVector = normalize(vertexPosition - cameraPosition);
// apply the offset along the direction
vertexPosition = vertexPosition + cameraVector * offsetAmount;

1 Like

Its a long story :slight_smile: I was trying to “hack the system” for general problem of fake billboard reflections like this one.
General problem - after disabling depth test, billboard will draw on top of anything, not only the floor.

Spoiler alert!

Yes. I know what i can use custom stencil as a solution :slight_smile: but i was wanted to experimenting and learn something. Maybe im not a smart math guy but at least im, probably, THE BEST living example of XY problem :laughing:

So we need to somehow cut out objects which will appear between camera and billboard.
Solutions like creating mask from scene depth and billboard depth can work but only for object which are pretty far. If object too close - he will recieve out fake reflections.
So i think - “Hey! If we just put billboard on the floor like decal, we can use mask from depth without any problems!” Nice but now we need to make something with our flat polygon lying on the floor which makes him look like classic billboard but still keep them on the floor. So we need to distort polygon vertices.

Fake. Polygon E are laying on the floor. But looks like a classical billboard (which are perpendicular to the floor and offset down by Z

From camera position on previous image everything looks nice. But if we look from another angle...

So if we move 2 bottom vertices of billboard C toward camera position, we can approach desired results.

The big problem - im an idiot :slight_smile: And totally forgot about UV distortions.

1 Like

Cool technique! Can you get around the UV distortion issue by subdividing the plane a few times?

To put the subdivided vertices on the plane (the floor), you can use plane intersection math in your vertex shader
plane point = plane position
plane normal = up vector
ray vector = camera look vector
ray point = camera position

The biggest struggle of trying to get an answer to a problem while working on a live project :frowning: sometimes all you can do is ask about potential solutions because you can’t say what you’re working on. I’ve had to do that probably a half dozen times :pensive:

1 Like

Not a technique at all, im afraid ) Just some theoritically possible trick for billboards reflection without custom stencil.
Btw i doesnt really know how custom stencil cost in terms of performance? Maybe they are bloody cheap.

Unfotrunately at the moment im unable to use c++ of even HLSL (in code), cause its out of my current skill level.

In my images which i provide everything works with something what will define the floor, when i make that images i believe what i can generate that polygon on runtime by procedural mesh, so i can do linetrace and ignore everything except the floor for avoiding some object which will intersect with ray. But i dont know what procedural mest cant be edited on runtime :roll_eyes:.
And cameraoffset node, unfortunately, doesnt take into account the floor level and vertices offset will never have right value.
So there’s needs to define some virtual (cause we cant use scene depth, cause othervice we will have wrong values because of the objects on the floor) floor or “virtual ground” plane, to clamp vertices (like they stick to floor and dont want to move further) or to calculate the distance needed to offset.
So… too much problems :sweat: