I am trying to use the raycast function a.e.
this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction);
in my project… works fine as long as the camera is in perspective mode - as soon as it is orthographic the raycast gives nothing back - is this a known bug? Is there a workaround?
thx,
Pep
will
December 6, 2017, 8:13pm
#2
I just tested this and it seems to work fine. Here’s a script I added to my camera:
var Ray = pc.createScript('ray');
// initialize code called once per entity
Ray.prototype.initialize = function() {
this.app.mouse.on('mousemove', this.onMouseMove, this);
this.rayStart = new pc.Vec3();
this.rayEnd = new pc.Vec3();
this.green = new pc.Color(0, 1, 0);
this.red = new pc.Color(1, 0, 0);
};
Ray.prototype.onMouseMove = function(e) {
var farClip = this.entity.camera.farClip;
var nearClip = this.entity.camera.nearClip;
this.entity.camera.screenToWorld(e.x, e.y, nearClip, this.rayStart);
this.entity.camera.screenToWorld(e.x, e.y, farClip, this.rayEnd);
var result = this.app.systems.rigidbody.raycastFirst(this.rayStart, this.rayEnd);
if (result) {
this.entity.camera.clearColor = this.green;
} else {
this.entity.camera.clearColor = this.red;
}
};
// update code called every frame
Ray.prototype.update = function(dt) {
};
All I did was the following:
Create a blank project
Add the script above to the Camera entity.
Added rigidbody and collision components to the Cube entity.
Change the camera to Ortho and tweak the clip planes a bit to get the whole scene rendered
Run the scene. The background is red when the mouse isn’t over the cube and green otherwise.
2 Likes