# Line opacity rendering issue with transparent spheres

**URL:** <https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287>\
**Category:** Questions\
**Tags:** materials\
**Created:** [May 2, 2017, 6:50am UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287 "2017-05-02T06:50:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasturiano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vasturiano/32/143_2.png) [@vasturiano](https://discourse.threejs.org/u/vasturiano)\
**Post date:** [May 2, 2017, 6:50am UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/1 "2017-05-02T06:50:41Z")

</div>

I’ve ran into an issue in which lines with an opacity setting don’t get rendered properly on top of Mesh objects (in this case spheres) also containing a transparency setting. But only sometimes…

There is an example here: [https://bl.ocks.org/vasturiano/raw/02affe306ce445e423f992faeea13521/](https://bl.ocks.org/vasturiano/raw/02affe306ce445e423f992faeea13521/)

As you zoom in you’ll notice that some spheres have solid line segments on top of them while others don’t. Also by rotating/panning around this may change and a sphere that was fine suddenly shows the glitch and vice-versa.

The component which has the ThreeJS code is here:

> <https://github.com/vasturiano/3d-force-graph/blob/master/src/3d-force-graph.js>

The relevant code which generates the Three objects is:

```auto
mySphere = new THREE.Mesh(
    new THREE.SphereGeometry(myRadius, 8, 8),
    new THREE.MeshLambertMaterial({ color: myColor, transparent: true, opacity: 0.75 })
);

myLine = new THREE.Line(
    new THREE.Geometry(), 
    new THREE.LineBasicMaterial({ color: 0xf0f0f0, transparent: true, opacity: 0.2 })
);

```

The renderer is used with all the default options: `new THREE.WebGLRenderer()`

I’m at a complete loss as to what is causing this glitch, any help/pointers as to what may be the cause will be much appreciated!

---

<div class="post-metadata">

**Author:** ![INF1N1T](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/inf1n1t/32/79_2.png) [@INF1N1T](https://discourse.threejs.org/u/INF1N1T)\
**Post date:** [May 2, 2017, 10:23am UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/2 "2017-05-02T10:23:30Z")

</div>

Hello asturiano,

to make sure we are all talking about the same “glitch”, from the pix below can you tell us if the glitch occurs in case 1 or case 2 or neither cases:

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/threejs/original/1X/790985bcf283afaea43883650df965e3cb6ec4ce.jpg)

Thx

---

<div class="post-metadata">

**Author:** ![vasturiano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vasturiano/32/143_2.png) [@vasturiano](https://discourse.threejs.org/u/vasturiano)\
**Post date:** [May 2, 2017, 6:06pm UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/3 "2017-05-02T18:06:15Z")

</div>

Hi Inf1n1t, thanks for jumping in.

And sorry, I should’ve added a screenshot to clarify the issue. The glitch I’m referring to is in neither cases 1 or 2. Here’s what it looks like:

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/threejs/original/1X/2ee1450039129aefe31c740a0f04cf663d0936fe.png)

I’m referring to those dark line segments on the large sphere, which doesn’t occur in the other spheres and even appears/disappears as the objects are rotated.

---

<div class="post-metadata">

**Author:** ![INF1N1T](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/inf1n1t/32/79_2.png) [@INF1N1T](https://discourse.threejs.org/u/INF1N1T)\
**Post date:** [May 2, 2017, 6:56pm UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/4 "2017-05-02T18:56:46Z")

</div>

Re asturiano,

As you can see in the following pix, the “dark line segment” is the actual line on top of the object;  
 ![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/threejs/original/1X/68ddfa22dedc09931fea686f3b463ee9a24f0f57.jpg)

The renderer may need a blending instruction to accomplish what you want.

I am not sure which one from ([https://threejs.org/examples/?q=blending#webgl\_materials\_blending](https://threejs.org/examples/?q=blending#webgl_materials_blending)) will accomplish the final color you want thought.

Try this in your line material definition and see if you have any improvement:

`blending: THREE.AdditiveBlending`

---

<div class="post-metadata">

**Author:** ![vasturiano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vasturiano/32/143_2.png) [@vasturiano](https://discourse.threejs.org/u/vasturiano)\
**Post date:** [May 2, 2017, 9:12pm UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/5 "2017-05-02T21:12:38Z")

</div>

Thanks for the tip Inf1n1t.

What’s puzzling is the way it renders a line on top of the object is not consistent. In fact, if you look at the two smaller spheres in the my previous screenshot, those lines are actually “in front” of the object, yet they are rendered the way I’d expect it, by mixing the two opacities.  
It just seems that for some camera angles that opacity mixing is misbehaving, causing some line segments to be completely solid.

I’ve tried the blending settings you suggested, both on the lines and/or sphere objects. No combination of blending makes any difference except when selecting NoBlending (effectively cancelling the object’s transparency) or SubtractiveBlending which makes matters worse by always drawing dark lines on top of the spheres:  
 ![](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/1X/a3ab78c37929e4f6df26c40ae4b58983e7493e2d.png)

---

<div class="post-metadata">

**Author:** ![INF1N1T](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/inf1n1t/32/79_2.png) [@INF1N1T](https://discourse.threejs.org/u/INF1N1T)\
**Post date:** [May 2, 2017, 11:14pm UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/6 "2017-05-02T23:14:34Z")

</div>

If you have not tried it already, you could force the depth-buffer to render the line first (or vise-versa) with something like this:

```
mySphere.renderOrder=2;
myLine.renderOrder=1;

```

Just make sure that all the spheres have the same renderOrder value or have a superior value (than the lines).

---

<div class="post-metadata">

**Author:** ![vasturiano](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vasturiano/32/143_2.png) [@vasturiano](https://discourse.threejs.org/u/vasturiano)\
**Post date:** [May 3, 2017, 12:02am UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/7 "2017-05-03T00:02:08Z")

</div>

Thanks so much Inf1n1t, it worked!!

By setting the lines to render after all the spheres, the issue is gone. I’m not quite certain why rendering the lines mixed in with the spheres (I suppose that’s what was happening) would cause that visual anomaly in the mixed opacity.

But, the bottomline is it worked!! 🎉

Thanks for your help. Here’s the example again without the issue:  
[https://bl.ocks.org/vasturiano/raw/02affe306ce445e423f992faeea13521/](https://bl.ocks.org/vasturiano/raw/02affe306ce445e423f992faeea13521/)

---

<div class="post-metadata">

**Author:** ![INF1N1T](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/inf1n1t/32/79_2.png) [@INF1N1T](https://discourse.threejs.org/u/INF1N1T)\
**Post date:** [May 3, 2017, 12:44am UTC](https://discourse.threejs.org/t/line-opacity-rendering-issue-with-transparent-spheres/287/8 "2017-05-03T00:44:47Z")

</div>

Great!

Depending on the view, the renderer was using the background only to define the line color (with opacity). Setting the order, tells the renderer to layer the objects with the proper hierarchy and thus have all the colors to blend.
