Skip to content

Commit 41079d2

Browse files
authored
Fix raycaster overriding line default color (#4743)
* Let Line component define the default color * Fix default value * Add lineOpacity option to raycaster * Update documentation
1 parent aed99f5 commit 41079d2

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

docs/components/laser-controls.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ See [*Raycaster: Customizing the Line*][customize].
8181
For example:
8282

8383
```html
84-
<a-entity laser-controls line="color: red; opacity: 0.75"></a-entity>
84+
<a-entity laser-controls raycaster="lineColor: red; lineOpacity: 0.5"></a-entity>
8585
```

docs/components/raycaster.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ examples: []
1414
The raycaster component provides line-based intersection testing with a
1515
[raycaster][wiki-raycasting]. Raycasting is the method of extending a line from
1616
an origin towards a direction, and checking whether that line intersects with
17-
other entites.
17+
other entities.
1818

1919
The raycaster component uses the [three.js raycaster][3ray]. The raycaster
2020
checks for intersections at a certain interval against a list of objects, and
@@ -65,8 +65,9 @@ AFRAME.registerComponent('collider-check', {
6565
| enabled | Whether raycaster is actively checking for intersections. | true |
6666
| far | Maximum distance under which resulting entities are returned. Cannot be lower than `near`. | Infinity |
6767
| interval | Number of milliseconds to wait in between each intersection test. Lower number is better for faster updates. Higher number is better for performance. Intersection tests are performed at most once per frame. | 0 |
68-
| lineColor | Raycaster line color if showLine is enabled. | white |
69-
| near | Minimum distance over which resuilting entities are returned. Cannot be lower than 0. | 0 |
68+
| lineColor | Raycaster line color if showLine is enabled. | white |
69+
| lineOpacity | Raycaster line opacity if showLine is enabled. | white |
70+
| near | Minimum distance over which resulting entities are returned. Cannot be lower than 0. | 0 |
7071
| objects | Query selector to pick which objects to test for intersection. If not specified, all entities will be tested. Note that only objects attached via `.setObject3D` and their recursive children will be tested. | null |
7172
| origin | Vector3 coordinate of where the ray should originate from relative to the entity's origin. | 0, 0, 0 |
7273
| showLine | Whether or not to display the raycaster visually with the [line component][line]. | false |
@@ -205,11 +206,10 @@ mutations (e.g., some entity changes its `class`).
205206

206207
If `showLine` is set to `true`, the raycaster will configure the line given the
207208
raycaster's `origin`, `direction`, and `far` properties. To customize the line
208-
appearance provided by the `showLine: true` property, we configure the [line
209-
component][line]:
209+
appearance provided by the `showLine: true` property, we can use the `lineColor` and `lineOpacity`:
210210

211211
```html
212-
<a-entity raycaster="showLine: true; far: 100" line="color: orange; opacity: 0.5"></a-entity>
212+
<a-entity raycaster="showLine: true; far: 100; lineColor: red; lineOpacity: 0.5"></a-entity>
213213
```
214214

215215
The line length is the raycaster's `far` property when the raycaster is not

src/components/raycaster.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports.Component = registerComponent('raycaster', {
5151
origin: {type: 'vec3'},
5252
showLine: {default: false},
5353
lineColor: {default: 'white'},
54+
lineOpacity: {default: 1},
5455
useWorldCoordinates: {default: false}
5556
},
5657

@@ -200,7 +201,7 @@ module.exports.Component = registerComponent('raycaster', {
200201
},
201202

202203
/**
203-
* Raycast for intersections and emit events for current and cleared inersections.
204+
* Raycast for intersections and emit events for current and cleared intersections.
204205
*/
205206
checkIntersections: function () {
206207
var clearedIntersectedEls = this.clearedIntersectedEls;
@@ -378,6 +379,7 @@ module.exports.Component = registerComponent('raycaster', {
378379
this.lineData.start = data.origin;
379380
this.lineData.end = endVec3.copy(this.unitLineEndVec3).multiplyScalar(length);
380381
this.lineData.color = data.lineColor;
382+
this.lineData.opacity = data.lineOpacity;
381383
el.setAttribute('line', this.lineData);
382384
},
383385

@@ -386,7 +388,7 @@ module.exports.Component = registerComponent('raycaster', {
386388
* Children are flattened by one level, removing the THREE.Group wrapper,
387389
* so that non-recursive raycasting remains useful.
388390
*
389-
* Only push children defined as component attachemnts (e.g., setObject3D),
391+
* Only push children defined as component attachements (e.g., setObject3D),
390392
* NOT actual children in the scene graph hierarchy.
391393
*
392394
* @param {Array<Element>} els

tests/components/laser-controls.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ suite('laser-controls', function () {
5959
});
6060
});
6161

62-
test('respects set line color', function (done) {
63-
el.setAttribute('line', 'color', 'white');
62+
test('respects set line color and opacity', function (done) {
63+
el.setAttribute('raycaster', 'lineColor', 'red');
64+
el.setAttribute('raycaster', 'lineOpacity', '0.5');
6465
el.emit('controllerconnected', {name: 'daydream-controls'});
6566
setTimeout(() => {
66-
assert.equal(el.getAttribute('line').color, 'white');
67+
assert.equal(el.getAttribute('raycaster').lineColor, 'red');
68+
assert.equal(el.getAttribute('raycaster').lineOpacity, '0.5');
6769
done();
6870
});
6971
});

0 commit comments

Comments
 (0)