Can't show camera's forward ray

I need to emit a ray from camera. But looks like built in method getForwardRay doesn’t work.
At least I can’t display it and be sure that this is working correctly:

If there is no way to use this method, then how can I cast ray forward from camera?
Actually I don’t know how to build direction vector.

var direction = camera.position.clone();
direction.z += 2;

Camera position is absolute here, but I need to add z in camera’s local space and then transform it back to global space. Not sure, how to do this.

Hi s27. I toured a few getForwardRay playgrounds… and it seems to be working… BUT… I think we could do an improvement to rayHelper for rays gotten via camera.getForwardRay(). It seems that the ORIGIN of the linesMesh used for… is SO NEAR to the camera, that camera.minZ is killing the ray line. If you reduce the cam minZ to REAL LOW (line 8 in below PG), you can start to see the ray lines.

Looks terrible, but hey, we’re seeing some rayHelper lines. It would be nice… if rayHelper had a feature to x-invert linesMesh (put linesMesh origin at OTHER end of linesMesh)… to avoid/avert camera.minZ issues. We should NOT flip the ray itself, but only offer an option to flip the linesMesh. Possibly, a setPivotMatrix/pivotPoint thing.

btw, ray.direction exists… makes life easier, sometimes. Also, keep in mind that directions… can be gotten by myDirection = someNode.position.subtract(anotherNode.position). PickedMesh are nodes, and so is the camera. You can swap the mesh in the subtraction… to reverse the resulting direction vector, or use myDirection.negate(), as well, if needed.

Note: Sometimes folks .normalize() a direction vector, which is similar to “reducing” in the math world. Normalize() reduces the magnitude of a direction… until all values are between -1 and +1 inclusive. In essence, it reduces the “length” (magnitude) of the direction… to the smallest possible values and still keep it pointing in the same direction. Not sure if normalize() will be useful for you, but I thought I’d tell you about it, using my feeble knowledge levels on that subject. :slight_smile:

Ok, that’s all I have so far. It might be enough to keep your project moving “forward” :slight_smile: Stay tuned for more/wiser comments.


Hey s27… I tried some experiments today…

Line 8: Camera .minZ still set quite low.

Lines 67-71: Changed event to be RIGHT-mousebutton down. So, a right-click on canvas… performs the line 62 ray cast.

Lines 33-58: A Wingy-modified “replacement” for Line 40-44 simply creates the RayHelper._renderPoints… in reverse order (it’s a 2-element array - line start/stop points.)

Line 48: Removes the webpack garbage from original line 47. My god… _Meshes_mesh__WEBPACK_IMPORTED_MODULE_1__["Mesh"] ?? Why not getWebpack("Mesh") ? Core-folk, do we NEED to have this webpack debris in the BJS max dist file? Alternatives available? Open to discussion? (Maybe Wingy doesn’t understand “the system”). ANYWAY…

…I am failing in this reversal endeavor. Scene mesh is gone… when canvas is right-clicked. Obviously, I’m doing something wrong. :slight_smile:

Objective reminder: When line 62 does let ray = camera.getForwardRay(200); the RayHelper linesMesh can’t be seen. My theory for why: The origin of the linesMesh is SO near-to the camera, that the camera.minZ seems to make it disappear. My objective is to reverse the points array that is sent to the linesMesh for RayHelper._renderFunction()… a function which is currently bound-to RayHelper._render(). This reversal action should place the origin/pivotPoint of the linesMesh line… at the distant-end of the line, well beyond camera.minZ concerns.

TS src: Babylon.js/rayHelper.ts at master · BabylonJS/Babylon.js · GitHub

So, I/we could use some help with our reversed linesMesh points array… or some other method of fixing the rendering of camera-forward rayHelper lines. Keep in mind… that the line STILL could be difficult to see, because it is aimed directly at the camera, and perfectly matches camera.direction. Screen coords of right-click location… is not used.

Fun symptoms: Right-click more times AFTER the mesh disappears… and pan camera around a bit. Various linesMesh junk is seen… but I’m not learning much from seen symptom. Help/ideas welcome from anyone, thx.

I’m wondering… if maybe… we could add a “special case” branch… in the rayHelper renderer.

IF (either end of points array == camera.position && ray-direction == camera.direction) { use a special cone mesh or shader-made cone-like “beam” to show the rayLine }

Is it used often-enough to warrant a “special case” branch? How many OTHER places in BJS core… could we use a “beam” leaving the camera… for helping do/see something? (I’m thinking about re-use possibilities of this special “camera-beam” thing). Could use a cone. But, ANY line leaving the camera.position… traveling in the camera.direction… will tend to be invisible, or look like a 2d circle.

One idea… the RayHelper renderer for camera-forward rays… just makes crosshairs/target-reticle. hmm.