- Home /
What is the best way of drawing/projecting dynamic trajectory on a ground surface
Hi, I am trying to implement an effect where a trajectory/path of a character is drawn/highlighted on the ground.
Something like
where the blue path has the effect of as though being painted on the ground.
For now I want to make a simplified version that take the intersection point of the camera with the ground plane, then color between the lookat intersection and where the camera stands. something like
I think I can use raycast to get the intersections, but I have no idea how to approach drawing on the surface.
I looked at Projector example such as http://hyunkell.com/blog/rts-style-unit-selection-in-unity-5/
But I think Projector uses a pre-defined texture? In my case the path will be dynamically calculated. Should I draw to a Rendertexture then project that Rendertexture?
I think there might also be options such as using Decay or Trail renderer.
Could anyone point me in the direction how this problem should be approached? Thank you.
Answer by kevin0228ca · Jun 03, 2017 at 11:30 AM
I implemented a current solution by using two raycasts, one from the camera to find the gaze intersection, and one directly beneath from the camera to find the camera hover position.
Then, from these two vectors, I create a list of TrailSections, and use that to create a Mesh, then I just put some color on the mesh.
This Mesh approach is taken from the Unity Procedural Trail example from Unity
struct TrailSection
{
public Vector3 point;
public Vector3 upDir;
public float time;
}
private void LateUpdate()
{
// remove old sections
while (sections.Count > 0 && now > sections[sections.Count - 1].time + time)
{
sections.RemoveAt(0);
}
for (int i = 0; i < numSections; i++)
{
float fraction = (float)i / (float)numSections;
Vector3 pos = Vector3.Lerp(hit.point, camerarayhit.point, fraction);
TrailSection section = new TrailSection();
section.point = pos;
if (alwaysUp)
section.upDir = Vector3.up;
else
section.upDir = transform.TransformDirection(Vector3.up);
section.time = now;
sections.Insert(0, section);
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
/*
generating mesh vertices, uv, colors, tri
*/
// then assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
}
This works somewhat as I can see the trail extending to where the camera is looking at the floor.
However the framerate is decreased from 60 to about 30.
Is it because I am creating and clear meshes on every frame?
Does anyone know a better approach? Thank you.
Your answer
Follow this Question
Related Questions
Project vertices on surface, make a shape 1 Answer
Animating Offset of projector shader 1 Answer
How to create a 3D surface from a 2d array? 1 Answer
A way to "fold" a texture on a mesh? 1 Answer
Decal on a curved surface 2 Answers