Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ <h2>Tests</h2>
<li><a href="primitives/torus/">Primitive: Torus</a></li>
<li><a href="test/raycaster/">Raycaster</a></li>
<li><a href="test/raycaster/simple.html">Raycaster (Simple)</a></li>
<li><a href="test/raycaster/sprite.html">Raycaster (Sprite)</a></li>
<li><a href="test/shaders/">Shaders</a></li>
<li><a href="test/shadows/">Shadows</a></li>
<li><a href="test/text/">Text</a></li>
Expand Down
74 changes: 74 additions & 0 deletions examples/test/raycaster/sprite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Raycaster - Sprites</title>
<meta name="description" content="Raycaster Sprites - A-Frame" />
<script src="../../../dist/aframe-master.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent('sprite', {
schema: {
color: { type: 'color', default: '#69f' },
width: { type: 'number', default: 1 },
height: { type: 'number', default: 1 }
},

init: function () {
var data = this.data;
var material = new THREE.SpriteMaterial({ color: data.color });
var sprite = new THREE.Sprite(material);
sprite.scale.set(data.width, data.height, 1);
this.el.setObject3D('mesh', sprite);
},

update: function () {
var data = this.data;
var sprite = this.el.getObject3D('mesh');
if (!sprite) {
return;
}
sprite.material.color.set(data.color);
sprite.scale.set(data.width, data.height, 1);
},

remove: function () {
this.el.removeObject3D('mesh');
}
});

AFRAME.registerComponent('raycast-highlight', {
init: function () {
this.el.addEventListener('raycaster-intersected', function (evt) {
var el = evt.target;
var sprite = el.getObject3D('mesh');
if (sprite) {
sprite.material.color.set('#f44');
}
});

this.el.addEventListener('raycaster-intersected-cleared', function (evt) {
var el = evt.target;
var sprite = el.getObject3D('mesh');
if (sprite) {
sprite.material.color.set('#69f');
}
});
}
});
</script>
<p>Look at a sprite, it should turn red. Look away, it turns back to blue.</p>

<a-scene>
<a-entity camera look-controls wasd-controls raycaster="interval: 100; objects: [raycast-highlight]"></a-entity>

<a-entity sprite="color: #69f; width: 2; height: 2" position="-3 1.6 -5" raycast-highlight></a-entity>
<a-entity sprite="color: #69f; width: 1; height: 3" position="0 1.6 -5" raycast-highlight></a-entity>
<a-entity sprite="color: #69f; width: 3; height: 1" position="3 1.6 -5" raycast-highlight></a-entity>

<a-entity sprite="color: #69f; width: 1.5; height: 1.5" position="-2 3 -8" raycast-highlight></a-entity>
<a-entity sprite="color: #69f; width: 1.5; height: 1.5" position="2 3 -8" raycast-highlight></a-entity>
</a-scene>
</body>
</html>
2 changes: 2 additions & 0 deletions src/components/raycaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ export var Component = registerComponent('raycaster', {
// Raycast.
this.updateOriginDirection();
rawIntersections.length = 0;
this.raycaster.camera = this.el.sceneEl.camera;
this.raycaster.intersectObjects(this.objects, true, rawIntersections);
this.raycaster.camera = null;

// Only keep intersections against objects that have a reference to an entity.
intersections.length = 0;
Expand Down
41 changes: 41 additions & 0 deletions tests/components/raycaster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,47 @@ suite('raycaster', function () {
});
});

suite('sprite raycasting', function () {
var spriteEl;

setup(function (done) {
spriteEl = document.createElement('a-entity');
el.setAttribute('position', '0 0 1');
el.setAttribute('raycaster', {near: 0.1, far: 10});

spriteEl.addEventListener('loaded', function () {
var material = new THREE.SpriteMaterial({color: 0x6699ff});
var sprite = new THREE.Sprite(material);
spriteEl.setObject3D('mesh', sprite);
spriteEl.object3D.position.set(0, 0, -1);
setTimeout(() => { done(); });
});
sceneEl.appendChild(spriteEl);
});

test('can intersect sprites', function (done) {
spriteEl.addEventListener('raycaster-intersected', function () {
done();
});
sceneEl.object3D.updateMatrixWorld();
component.refreshObjects();
component.tock();
});

test('emits intersection-cleared when looking away from sprite', function (done) {
spriteEl.addEventListener('raycaster-intersected', function () {
el.setAttribute('rotation', '90 0 0');
spriteEl.addEventListener('raycaster-intersected-cleared', function () {
done();
}, {once: true});
component.tock();
}, {once: true});
sceneEl.object3D.updateMatrixWorld();
component.refreshObjects();
component.tock();
});
});

suite('updateOriginDirection', function () {
test('updates ray origin if position changes', function () {
var origin;
Expand Down
Loading