
第一种:
这是一种发射线,获取当前点击的物体(具有碰撞器)的坐标点(也就是碰撞器范围的坐标点)
if (InputGetMouseButtonDown(0))
{
RaycastHit hitt = new RaycastHit();
Ray ray =CameramainScreenPointToRay(InputmousePosition);
PhysicsRaycast(ray, out hitt);
DebugLog(hittpoint);
//DebugLog(CameramainScreenToWorldPoint(InputmousePosition));
}
第二种:
Camera TempC;//定义摄像机变量
private void Start()
{
TempC = thisGetComponent<Camera>();//获取摄像机的组件 Camera
}
void Update()
{
if(InputGetMouseButtonDown(0))
{
DebugLog(TempCScreenToWorldPoint(new Vector3(InputmousePositionx,InputmousePositiony, 100f)));
}
}
注意:这里容易出现的问题是,从鼠标坐标点转成世界坐标点的时候,新的三维向量Z轴的赋值必须不能是00f,因为,屏幕坐 标点的Z轴,其实是相对于当前摄像机的,如果是0,只能转一次世界坐标系,剩下的不管点哪里都是一个值,所以不能把Z值设为和摄像机的Z坐标重合,我们需要设置一个非0的数字,最后转成世界坐标系后的值是,摄像机的Z轴加上所设置的值。例如当前摄像机Z坐标是10f,我所设置的Z值是10f,最后转成世界坐标系后的Z值就是20;下面是老外的经典回复。
Thereare two problems here The first one is that you need a 'new' in front of yourVector3() in C# The second is that the 'z' parameter must be the distance infront of the camera In perspective mode at least, passing 0 will causeScreenToWorldPoint() to always return the position of the camera So the callwill look something like:
worldPos = CameramainScreenToWorldPoint(new Vector3(InputmousePositionx,InputmousePositiony, 100f));
Notethe measurement for the 'z' parameter is from camera plane to the playingsurface plane It is not the distance between the camera and the object If thecamera is aligned with the axes, then the distance is easy to calculate If thecamera is at an angle (ie not axes aligned), then ScreenToWorldPoint() is notthe way to go
其实,OpenGL的转换管道直接将gl_Vertex,也就是物体坐标,用gl_ModelViewMatrix相乘,得到的是眼坐标。如果将gl_ModelViewMatirx拆分为gl_ModelMatrix和gl_ViewMatrix,那么问题就好解决了。但事实上没有提供。要清楚OpenGL其实没有世界坐标系,世界坐标系是应用程序的概念。其实可以将OpenGL的摄像机看作是固定的,其坐标系就是眼坐标系,移动摄像机和移动物体的位置是一个相反的转换,对于观察者来说根本不知道是摄像机在动,还是物体在动(想想大卫的大变自由女神像的魔术吧,呵呵)
说回来,最终的变换是这样的:
eyePos=viewMatrix modelMatrix modelVertex
在OpenGL里面viewMatrix和modelMatrix合并了,因为OpenGL里面并没有设置摄像机的参数,所以OpenGL并不知道viewMatrix到底是什么。viewMatrix是用户自己定义的,所以如果能够得到这个viewMatrix并能得到其逆矩阵,就可以得到worldPos:
worldPos=viewMatrixInv viewMatrix modelMatrix modelVertex
传统的OpenGL程序里面,你得自己计算这个viewMatrixInv,还好OSG的Camera提供了一个getViewMatrixInverse()方法,通过这个方法我们就可以轻松的获得viewMatrixInv,然后传递给Vertex Shader(用一个Uniform就可以),然后进行这个计算就可以了。
记得每一帧都需要Update这个viewMatrixInv,只需要一个updateCallBack就可以了。
好了,看几个图,我用3DSMAX创建了两个盒子,为了便于观察,模型的顶点值限制在0-1之间,然后用osgExp导出,没有选中Flatten Static Transform这样就不会把模型定点转换成世界坐标系的顶点。
源代码中可以改变gl_FragColor=的值来修改为相应的坐标系的值显示。
世界坐标系的最终输出,可见颜色连续变换的。
在unity中InputmousePosition是只读的,也就是不能通过这个方法给鼠标位置赋值。
Unity3D简介:
Unity3D是由Unity Technologies公司开发的一个让玩家轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。 Unity类似于Director,Blender game engine,Virtools或Torque Game Builder等利用交互的图型化开发环境为首要方式的软件。其编辑器运行在Windows和Mac OS X下,可发布游戏至Windows、Mac、Wii、iPhone、WebGL(需要HTML5)、Windows phone 8和Android平台。也可以利用Unity web player 插件发布网页游戏,支持Mac和Windows的网页浏览。它的网页播放器也被Mac widgets所支持。
把new去掉,直接声明一个Vector3变量,将当前sprite的position值赋给它就行了。就是写成这样“Vector3 position1 = gameObjecttransformposition;”
以上就是关于unity3d 怎么把鼠标坐标转成世界坐标全部的内容,包括:unity3d 怎么把鼠标坐标转成世界坐标、unity shader 如何获取物体的本地坐标、unity3d怎么获得鼠标位置坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)