- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359
- 纳金币
- 335582
- 精华
- 0
|
反射ray LineRenderer的使用
光线折射路径绘制
http://www.unity3d8.com/content/%E5%85%89%E7%BA%BF%E6%8A%98%E5%B0%84%E8%B7%AF%E5%BE%84%E7%BB%98%E5%88%B6
上面的是原英文教程 本论坛的英文教程
代码做了解析
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (LineRenderer))]
public class RaycastReflection : MonoBehaviour
{
//this game object's Transform
private Transform goTransform;
//the attached line renderer
private LineRenderer lineRenderer;
//a ray
private Ray ray;
//a RaycastHit variable, to gather informartion about the ray's collision
private RaycastHit hit;
//reflection direction
private Vector3 inDirection;
//the number of reflections
public int nReflections = 2;//反射点的数量,如图,有两个反射点
//the number of points at the line renderer
private int nPoints;//lineRenderer上面顶点的数量
void Awake ()
{
//get the attached Transform component
goTransform = this.GetComponent<Transform>();
//get the attached LineRenderer component
lineRenderer = this.GetComponent<LineRenderer>();
}
void Update ()
{
//clamp the number of reflections between 1 and int capacity
nReflections = Mathf.Clamp(nReflections,1,nReflections);//使nReflections如果小于1 则返回1 否则返回nReflections
//cast a new ray forward, from the current attached game object position
ray = new Ray(goTransform.position,goTransform.forward);//创建射线
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(goTransform.position,goTransform.forward * 100, Color.magenta);//调试用 只在scene场景窗口中显示 ,不能在Game窗口中显示
//set the number of points to be the same as the number of reflections
nPoints = nReflections;//lineRenderer上面顶点的数量,这里nPoint被设置为nReflections,后面还会修改。
//make the lineRenderer have nPoints
lineRenderer.SetVertexCount(nPoints);//设置顶点数量,这里设置成nReflections,只是暂时的,可能是为了下面的一句有效
//Set the first point of the line at the current attached game object position
lineRenderer.SetPosition(0,goTransform.position);//设置第一个顶点的位置 ,不会改变
for(int i=0;i<=nReflections;i++)//这样设置循环,表示最多得到nReflections+2个点作为LineRenderer的顶点,因为初始顶点在Cube上,所以只需最多nReflections+1次循环
{
//If the ray hasn't reflected yet
if(i==0)//why? 这个里面会详细说明
{
//Check if the ray has hit something
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100))//cast the ray 100 units at the specified direction
{
//the refletion direction is the reflection of the current hit point flipped at the hit normal
inDirection = Vector3.Reflect(hit.point,hit.normal);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
ray = new Ray(hit.point,inDirection);
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
Debug.DrawRay(hit.point, hit.normal*3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
Debug.Log("Object name: " + hit.transform.name);
//if the number of reflections is set to 1
if(nReflections==1)//这里来说明为什么要有if(i==0) 这个分支:如果nReflections==1,那么之前lineRenderer.SetVertexCount(nPoints);操作只设置了一个顶点,无法存放接下来的操作
//lineRenderer.SetPosition(i+1,hit.point);所以要增大顶点数量。
{
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);
}
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i+1,hit.point);
}
}
else // the ray has reflected at least once
{
//Check if the ray has hit something
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100))//cast the ray 100 units at the specified direction
{
//the refletion direction is the reflection of the ray's direction at the hit normal
inDirection = Vector3.Reflect(inDirection,hit.normal);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
ray = new Ray(hit.point,inDirection);
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
Debug.DrawRay(hit.point, hit.normal*3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
Debug.Log("Object name: " + hit.transform.name);
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);//如果有碰撞,则LineRenderer增加一个顶点数用来存放这个新加的顶点
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i+1,hit.point); |
|