1 2 3 4 5 6 7 8 9 10 | //Find the center of the screen tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f); //Get mouse position tempVector = Input.mousePosition; //Set tempVector z to tempVector.y tempVector.z = tempVector.y; //Set tempVector y to 0 tempVector.y = 0; //Set lookDir to tempVector - tempVector2 lookDir = tempVector - tempVector2; |
是的,先得到屏幕中央坐标,然后把鼠标坐标(由于是俯视类游戏,y轴坐标变为z轴)减去中央坐标,接着:
1 2 | //Rotate player transform.rotation = Quaternion.LookRotation(lookDir); |
//Get right joystick x position float lX = rightJoystick.GetComponent<Joystick>().position.x; //Get right joystick y position float lY = rightJoystick.GetComponent<Joystick>().position.y; //Set lookDir x to joystick x position and dir z joystick y position lookDir = new Vector3(lX,0,lY); //Rotate player transform.rotation = Quaternion.LookRotation(lookDir); |
我自己一开始没有看明白,为什么就是直接调用position的位置就可以旋转了,知道我看了joystick的代码:
1 2 3 | // For a touchpad, let's just set the position directly based on distance from initial touchdown position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 ); position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 ); |
是的,他用的是同样的原理,圆点坐标和当前坐标的差值。
在图形学中,计算光照模型时,经常需要求取反射向量,一般的shader函数库都提供计算反射向量的方法,下面介绍一下如何手动计算反射向量。
给定入射光线向量I和平面法向量N,求反射向量R,如下图。为了方便计算,这里假定I和N都是单位向量(模为1,编程时可先将I和N单位化)
方法一:
设入射光线向量I和反射平面的法向量N之间的夹角为theta。连接I的始端和R的末端,则有
R = 2P - I (1)
现在问题变成了如何求取P,设入射点0到P与N的交点的向量为S,那么有
P = I + S (2)
现在问题变成了如何求取向量S,向量S即向量-N(注意,这里是-N,因为S和N的方向相反。)在向量N上的投影,根据向量的投影公式有
因为N是单位向量,简化一下得到
将S代入公式(2),再将P代入公式(1)得到
方法二:
将R平移一下,与向量N的延长线相交。
由于入射角和反射角相等,且I和R的长度也相等,所以三角形ION是等腰三角形。故有
ON = 2S
所以有
R = I + 2S
而S是-I在N上的投影,所以有
由于N是单位向量,简化一下得到
所以
貌似方法二更直观些。