- Home /
Question was answered
How to send a Ray through a vertex?
I am trying to create a custom shadow solution for casting a shadow on a flat plane in unity indie. For me to do this I need to cast a ray through each vertex of the model that is casting the shadow. This is what I have so far, but it does not work because the rays do not pass through the vertices.
public class Caster : MonoBehaviour {
public GameObject meshTrans; public Transform ground; private float y1, y2; Vector3[] vertices; // Use this for initialization void Start () { if (meshTrans != null) vertices = meshTrans.GetComponent<MeshFilter>().mesh.vertices; } // Update is called once per frame void Update () { for (int i = 0; i < vertices.Length; i++) { y1 = transform.position.y - vertices[0].y; y2 = vertices[0].y - ground.position.y; Vector3 v = vertices[0] - transform.position; v = v * ((y1+y2) / y1) ; Debug.DrawRay(transform.position, transform.TransformPoint(v) - transform.position, Color.red); } } }
Raycasting only detects colliders, so you could generate Sphere colliders on each vertex of your model, and then your Raycast should detect those colliders.
if I had a high-poly object, would that slow performance?
The vertices are in model space - so you'd need to convert them to world space using transform.localToWorld$$anonymous$$atrix
Presumably you want to project the ray onto some plane? So given that you have the equation of the plane and the two coordinates in world space it should be pretty straightforward - but is that what you are trying to do?
Thanks whydoidot! I got it working now. I used Transform.transformPoint() ins$$anonymous$$d of Transform.localToWorld$$anonymous$$atrix(). I am trying to cast a shadow on a flat plane by passing rays from a light source through each vertex of the model casting the shadow and then generating a mesh (for the actual rendering of the shadow) based on the place where each ray hits the plane (ground)
Yeah - transformPoint is a much better idea - don't know what I was thinking last night!
Follow this Question
Related Questions
Creating a working meshcollider in code 3 Answers
Procedurally generated, vertex colored mesh self-shadowing problem 0 Answers
Raycast on mesh deformed by shader 0 Answers
How do I check if the mouse is over a VISIBLE vertex? 1 Answer
Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers