标签: raycasting

3D 屏幕空间光线投射/拾取 DirectX9

我需要根据特定深度(用于光线投射)将 2D 鼠标坐标转换为 3D 世界坐标。我没有直接在 C++ 中使用 DirectX,我使用的是专有语言,但是如果你用 C++ 或伪代码(首选 C++)给我答案,我可以转换它。

我可以访问世界矩阵、视图矩阵和投影矩阵以及各种矩阵操作功能。

如果有必要将 vector4 乘以 matrix4,那么我唯一可以使用 vector4 和 matrix4 的函数是transformVector4(vector4Source,matrix4Source). 如果这很重要,我不确定它将它们乘以哪个顺序。

任何帮助将不胜感激:) 我已经为此工作了几个小时,但我没有得到 3D 数学......

c++ directx 3d matrix raycasting

1
推荐指数
1
解决办法
4058
查看次数

如何使用three.js将飞机“包裹”在球体上?

我对three.js比较陌生,我试图定位和操纵一个平面对象,以产生放置在球体对象(或任何与此相关的)表面上的效果,以便平面采用对象表面的形式. 目的是能够稍后在表面上移动飞机。

我将平面放置在球体的前面,并通过平面的顶点向球体投射光线以检测与球体的交点。然后我尝试更改所述顶点的 z 位置,但没有达到预期的结果。谁能给我一些关于如何使这个工作的指导,或者确实建议另一种方法?

这就是我尝试更改顶点的方式(偏移量为 1,以便在球体表面“可见”);

planeMesh.geometry.vertices[vertexIndex].z = collisionResults[0].distance - 1;
Run Code Online (Sandbox Code Playgroud)

确保在渲染前设置以下内容;

planeMesh.geometry.verticesNeedUpdate = true;
planeMesh.geometry.normalsNeedUpdate = true;
Run Code Online (Sandbox Code Playgroud)

我有一个显示我在哪里的小提琴,在这里我在 z 中投射我的光线并且我没有与球体相交(碰撞),并且不能以我希望的方式改变平面。

http://jsfiddle.net/stokewoggle/vuezL/

您可以使用左右箭头(无论如何都是镀铬)围绕场景旋转相机以查看飞机的形状。我让球体透视,因为我发现更好地看平面很有用。

编辑:更新小提琴并更正描述错误。

javascript 3d collision-detection three.js raycasting

1
推荐指数
1
解决办法
3061
查看次数

Threejs 光线投射点击检测不适用于加载的 3dObject

我加载一个 .obj 文件:

var loader = new THREE.OBJMTLLoader();
loader.load( "../obj/machine.obj", '../obj/machine.mtl',  this.loadObject);
Run Code Online (Sandbox Code Playgroud)

并尝试检测对其的点击:

 click: function(event){
        this.mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        this.mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
        var vector = new THREE.Vector3( this.mouse.x, this.mouse.y, 1 ).unproject( this.camera );
        this.raycaster.set( this.camera.position, vector.sub( this.camera.position ).normalize() );

        console.log(this.scene.children);
        var intersects = this.raycaster.intersectObjects( this.scene.children );

        if ( intersects.length > 0 ) {

            console.log("hitting something");

        }
    },
Run Code Online (Sandbox Code Playgroud)

这在网格上效果很好,但在加载的 3DObject 上效果不佳,因为它在 this.scene.childeren 中可见:

[THREE.Mesh, THREE.Line, …
Run Code Online (Sandbox Code Playgroud)

javascript three.js raycasting

1
推荐指数
1
解决办法
2531
查看次数

unity raycast , on raycast leave, 如何?C#

我在理解如何保留对先前被光线投射击中的对象的引用时遇到问题。

例如,我可以将光线投射脚本放在我的第一人称控制器的相机上,从相机位置到前向矢量 * 某个值

此脚本附加到相机

public class raycast : MonoBehaviour {
float lenthRay = 10.0f;
Vector3 originePos;
Vector3 dir;
RaycastHit hitinfo;
GameObject hitten;
bool isHitting;
Color beforC;
int selectionLayer = 9;

void Update () {
    originePos = Camera.main.transform.position;
    dir = Camera.main.transform.forward * lenthRay;
    Debug.DrawRay(originePos, dir, Color.blue);

    if (Physics.Raycast(originePos, dir, out hitinfo, lenthRay , selectionLayer)) {
        hitten = hitinfo.transform.gameObject;
        MeshRenderer tmp = hitten.transform.GetComponent<MeshRenderer> ();
        beforC = tmp.material.color;
        tmp.material.color = Color.black;
    } 
    //hitten.transform.GetComponent<MeshRenderer> ().material.color = beforC;
    print(hitten.name);
}
Run Code Online (Sandbox Code Playgroud)

}

它工作得很好,除非我尝试在我的 if 条件之外访问 …

c# unity-game-engine raycasting

1
推荐指数
1
解决办法
7847
查看次数

动态更改Raycaster位置

我有一个画布,里面有几个立方体。我使用Raycaster来选择它们并更改它们的颜色。但是画布是在draggable对象内部的,当我四处走动时我无法更改颜色,因此更改颜色的位置是原始的。我认为我还必须更改Raycaster的尺寸。我怎样才能做到这一点?

这是例子

jquery draggable three.js raycasting

1
推荐指数
1
解决办法
216
查看次数

Unity Physics2D.Raycast 击中自身

我正在尝试使用Physics2D.Raycast以检查玩家是否在地面上(我知道还有其他方法可以检查玩家是否在地面上,但我认为光线投射是最可靠的)。问题是,在我的场景中,它将玩家本身作为命中返回,我真的不明白为什么以及我应该做什么。我的代码(在PlayerController)如下:

 public bool IsGrounded () {
        Bounds bounds = this.playerCollider.bounds;
        Vector3 rayOrigin = bounds.center;
        rayOrigin.y -= bounds.extents.y;
        RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.down, 0.1f);
        if (hit.collider != null) {
            Debug.Log ("Collider is: " + hit.collider.name);
        }
        return hit.collider != null;
    }
Run Code Online (Sandbox Code Playgroud)

我可以使用以下方法调试投射的光线:

Debug.DrawLine (rayOrigin, new Vector3 (rayOrigin.x, rayOrigin.y - 0.1f, rayOrigin.z), Color.magenta);
Run Code Online (Sandbox Code Playgroud)

...它按预期进行了转换,而Debug.Log总是报告“播放器”本身,我不知道这怎么可能。那么怎么了?

附:我正在使用 Unity 5.3

c# collision-detection unity-game-engine raycasting

1
推荐指数
1
解决办法
4251
查看次数

指向 3d 封闭体积内

我有一个由 6 个曲面定义的 3d 封闭体积,每个曲面都有 4 个顶点。

所以,我想检查给定的点是在体积内还是在体积外。我想到的一个解决方案是:

  1. 从给定点绘制一条随机线并检查它与包围体积的表面相交的位置。由于我使用矢量代数来计算线和曲面的交点,因此交点可以位于 3d 无限曲面上的任何位置。

  2. 现在我检查这个交点是否恰好位于位于无限平面上并包围体积的那个面上。

为此,我再次想从我的交点到体积的各个面绘制一条随机光线,并检查点是否位于面上。

But I don't know how to check this feature of locating if it is on the surface  
or not. Can someone please suggest how can I do it.



P.S. One way of doing this was extending ray casting to 3d but that involves 
comparison of slopes to check the orientation. So how can I check orientation 
of 3 points in 3d space. I have 4 vertices which …
Run Code Online (Sandbox Code Playgroud)

c++ 3d point-in-polygon raycasting

1
推荐指数
1
解决办法
6768
查看次数

在 Unity C# 中将 LayerMasks 与 Physic2D.Linecast() 一起使用

我有一个简单的 DirectPath bool 函数,旨在根据两点之间是否有墙返回 true 或 false。

public bool DirectPath (Vector2 start, Vector2 end) {

    RaycastHit2D hitWall = Physics2D.Linecast (start, end, 8);

    if (hitWall == null) {
        Debug.Log ("Direct path returning true");
        return true;
    } else {
        Debug.Log ("Direct path returning false");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已将游戏中所有墙壁碰撞器的图层蒙版设置为“Wall”,它是我的图层列表中的第 8 号。但该函数每次都会返回 false,这意味着每次都会撞到墙壁碰撞器。“起始”点甚至不在对撞机内部,因此它不可能撞击自己的对撞机。但我想使用相同的函数来确定玩家的视线,因此这将涉及对撞机。

c# unity-game-engine raycasting

1
推荐指数
1
解决办法
2949
查看次数

三.js:点击2*2*2几何立方体时检测faceIndex不准确

我通过 Three.js 创建了一个 2*2*2 几何立方体。现在我想检测点击面孔(总共24张面孔)时的点击事件。

请在https://jsfiddle.net/agongdai/pdwg3myr/17/检查我当前的实现。单击脸部时,我想要console.log当前的脸部索引。但该指数并不总是准确的。例如,单击左上角的灰色单元格应显示 0,但实际上单击其底部部分显示 2。

请帮我检查一下鼠标单击事件处理程序:

var raycaster = new THREE.Raycaster();
function onDocumentMouseDown( event ) {

   var vector = new THREE.Vector3( 
      ( event.clientX / window.innerWidth ) * 2 - 1, 
      - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
   vector.unproject( camera );
   raycaster.setFromCamera( vector, camera );
   raycaster.set( camera.position, vector.sub( camera.position ).normalize() );

   var intersects = raycaster.intersectObject( cube );
   if ( intersects.length > 0 ) {
      var index = Math.floor( intersects[0].faceIndex / …
Run Code Online (Sandbox Code Playgroud)

three.js raycasting

1
推荐指数
1
解决办法
582
查看次数

光线投射到 Unity 中的特定层

我正在使用 GVRTeleport 脚本来允许使用纸板应用程序进行传送。我希望用于传送的光线投射忽略除一层之外的所有层。通过页面,我相应地修改了脚本(抱歉,找不到此代码所有者的原始链接),但现在传送脚本什么也看不到。有任何想法吗?我的地板层是第 8 层,这是我希望此光线投射与之交互的层。

using UnityEngine;

public class GVRTeleport : MonoBehaviour {

    public float viewHeight = 7f;

    Vector3 fwd;
    public float maxDistance = 10f;
    public LineRenderer line;
    public GameObject parent;
    public GameObject targetIndicator;

    public StraightLineParam genLine;

    int layerMask = 1 << 8;

    void Start() {
    }


    void Update() {
        RaycastHit hit;
        Ray ray;

        if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) {
            Debug.Log ("The ray hit the floor");


            if (debugWithMouse) {
                Vector2 mousePos = new Vector2 (Input.mousePosition.x / Screen.width, …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine raycasting

1
推荐指数
1
解决办法
1万
查看次数