12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 3287|回复: 12
打印 上一主题 下一主题

光线折射路径绘制 教程的代码解析

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-9-19 08:06:45 |只看该作者 |倒序浏览
反射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);
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

Asen    

867

主题

0

听众

1万

积分

外协人员

Rank: 7Rank: 7Rank: 7

纳金币
17488
精华
1
沙发
发表于 2011-9-23 15:20:00 |只看该作者
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2011-10-7 11:43:18 |只看该作者
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2011-12-21 20:02:50 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-2-12 23:18:31 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-2-13 23:18:20 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-2-24 23:27:52 |只看该作者
我是老实人,我来也!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-3-24 23:30:45 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-5-18 23:21:49 |只看该作者
先顶上去,偶要高亮加精鸟!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-5-30 23:21:35 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-2-6 14:48 , Processed in 0.068775 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部