I still have a few problems with my work, so I have some of these ‘do my work for me’ questions >:)
infinite rays
I’m trying to cast a ray from my imported character.
And here is the first big issue:
in the debuglayer (activate in line 164) -open nodes- you can see, that more and more rays are added to the scene. Of course it lags pretty fast. Is it, because I call the function in an import-mesh function? How can I avoid that?
ray position
I want to bind the ray to the head of the character. So far I did it with binding the ray to the character and changing the hight of the casted ray (line 139).
With the scaling-function (line 146) I found out, that I can access the head of the character with skeleton.bones[4], but I can’t bind neither the ray nor the camera to the head with:
rayHelper.attachToMesh(bot, localMeshDirection, localMeshOrigin, length);
or
camera.target = skeleton.bones[4];
because the scene then doesn’t start.
I’m creating the ray-function in line 135 and calling it in line 148.
You are generating a ray per frame in your castRay() function. This function creates an helper which in turn generate a mesh (per frame again). So no wonder why it is slow Why are you generating a ray per frame? (scene.registerBeforeRender is called once a frame)
You can create a dummy box, attach your box to the bone with mesh.attachToBone and then attach your ray to this dummy box
Why are you generating a ray per frame? (scene.registerBeforeRender is called once a frame)
I saw two versions of generating a ray in the tutorial of babylon.
One was in the castRay function:
function vecToLocal(vector, mesh) { var m = mesh.getWorldMatrix(); var v = BABYLON.Vector3.TransformCoordinates(vector, m); return v; }
The other outside of it:
var ray = new BABYLON.Ray(); var rayHelper = new BABYLON.RayHelper(ray); var localMeshDirection = new BABYLON.Vector3(0, 0, 1); var localMeshOrigin = new BABYLON.Vector3(0, 2, 0); var length = 20; rayHelper.attachToMesh(bot, localMeshDirection, localMeshOrigin, length); rayHelper.show(scene, new BABYLON.Color3(255,0,0));
I thought the first one was to cast a ray and the other to just make a visible ray.
Since I wanted to make the ray visible, I thought I could combine it and took the code with rayHelper.show(scene, new BABYLON.Color3(255,0,0));
It seems I didn’t understand it right. So thank you for your explaining. Is there a way to make a ray visible in the castRay function, so without rayhelper?
You can create a dummy box, attach your box to the bone with mesh.attachToBone and then attach your ray to this dummy box
I remember now, why I tried this ray function, like I did.
My ray is just in a certain angle visible. Same happens in the playground, after you move a certain distance. It just turns invisible then.
Can you/someone tell me why that is and how to avoid that?
(Also how do I create a dummy box respectively turn the box invisible?)
Not really immediately, but I tried to simplify the PG.
If you go to the end of the ground-mesh now, the ray disappears suddenly (just visible in a certain angle).
Maybe it has something to do with the limited radius of the camera in line 19.
I figured out, when I zoom out, the ray is visible again, when it disappeared.
Walking further results in disappearing of the ray again. And again, zooming out more let me see the ray again (and so on). Also it is visible again, if you go back.
It looks like the problem is that the ray (despite how it visually appears) is attached to a mesh at approximately the origin of the scene. If the camera is ever looking in such a way that it can no longer see the origin, it’ll cull this invisible mesh at the origin and stop rendering anything associated with it, such as the ray.
https://www.babylonjs-playground.com/#BCU1XR#343
^ here rayHelper._renderLine.alwaysSelectAsActiveMesh = true tells the ray to always be visible (never culled by camera logic) as a test case to confirm that culling was removing it previously
If using these rays purely for debugging purposes then perhaps the above is sufficient. Otherwise the logic needs changed such that the ray is being created as an object moving around with the character, as opposed to an accurately calculated piece of geometry situated near 0,0,0 but consisting of generated vertices that are potentially far away from 0,0,0 as the character+camera go elsewhere. I know that sounds super awkward as a whole but hopefully that makes sense.
Thx that works!
Is there a way to select two answers as ‘solved problem’ ?
Tbh I don’t really understand your approach.
I don’t know yet, how I will use the ray and if it really needs to be visible.
But for now it is fine, as long as it is working now.