纳金网

标题: 光线折射路径绘制 教程的代码解析 [打印本页]

作者: 晃晃    时间: 2011-9-19 08:06
标题: 光线折射路径绘制 教程的代码解析
反射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);
作者: Asen    时间: 2011-9-23 15:20

作者: 彬彬    时间: 2011-10-7 11:43

作者: C.R.CAN    时间: 2011-12-21 20:02
加精、加亮滴铁子,尤其要多丁页丁页

作者: 奇    时间: 2012-2-12 23:18
响应天帅号召,顶

作者: tc    时间: 2012-2-13 23:18
佩服,好多阿 ,哈哈

作者: C.R.CAN    时间: 2012-2-24 23:27
我是老实人,我来也!

作者: 奇    时间: 2012-3-24 23:30
很经典,很实用,学习了!

作者: 奇    时间: 2012-5-18 23:21
先顶上去,偶要高亮加精鸟!

作者: 菜刀吻电线    时间: 2012-5-30 23:21
再看一看,再顶楼主

作者: tc    时间: 2012-7-2 23:21
先顶上去,偶要高亮加精鸟!

作者: 晃晃    时间: 2012-9-19 23:20
不错哦,顶一下......

作者: tc    时间: 2012-12-16 23:25
发了那么多,我都不知道该用哪个给你回帖了,呵呵





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5