# How to control opacity of an object with shaders?

**URL:** <https://discourse.threejs.org/t/how-to-control-opacity-of-an-object-with-shaders/63703>\
**Category:** Questions\
**Tags:** threejs, textures, materials, geometry, shaders\
**Created:** [April 5, 2024, 11:36pm UTC](https://discourse.threejs.org/t/how-to-control-opacity-of-an-object-with-shaders/63703 "2024-04-05T23:36:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Somgolie\_Ejinkeonye](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/somgolie_ejinkeonye/32/46127_2.png) [@Somgolie\_Ejinkeonye](https://discourse.threejs.org/u/Somgolie_Ejinkeonye)\
**Post date:** [April 5, 2024, 11:36pm UTC](https://discourse.threejs.org/t/how-to-control-opacity-of-an-object-with-shaders/63703/1 "2024-04-05T23:36:30Z")

</div>

I’m trying to make a simple box slightly transparent using shaders. like glass, But I can’t get it to work. I’m not sure what exactly I’m doing wrong?

```javascript
	   var geometry = new THREE.BoxGeometry( 3, 6, 6 );//length,height,width

           var cubeShader = new THREE.ShaderMaterial({
            uniforms: {
                colorA: {type: 'vec3', value: new THREE.Color(0xffff00)},
                colorB: {type: 'vec3', value: new THREE.Color(0xffA500)}
            },
            vertexShader: vertexShader(),
            fragmentShader: fragmentShader()
          });
		cubeShader.opacity= 0.3;
		 cubeShader.transparent = true;
           var wall3 = new THREE.Mesh(geometry,cubeShader);
		   
		  
          wall3.castShadow=true;
           wall3.position.x+=30;
	  wall3.position.z+=7;
			
		  
           scene.add(wall3);

		
function fragmentShader() {
  return `
      uniform vec3 colorA; 
      uniform vec3 colorB; 
      varying vec3 vUv;

      void main() {
	 
        gl_FragColor = vec4(mix(colorA, colorB, vUv.z), 1.0);
      }
  `
}
function vertexShader() {
  return `
    varying vec3 vUv; 

    void main() {
      vUv = position; 

      vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
      gl_Position = projectionMatrix * modelViewPosition; 
    }
  `
}
		
           			//render 
             function render(){
               requestAnimationFrame(render);
			   fpcontrols.update(0.1);
              renderer.render( scene, camera );
            }
			
      

  
			render();

```

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [April 5, 2024, 11:57pm UTC](https://discourse.threejs.org/t/how-to-control-opacity-of-an-object-with-shaders/63703/2 "2024-04-05T23:57:42Z")

</div>

You’d need to take opacity into account in your fragment shader - it’s not applied automatically, your final gl\_FragColor vec4 value should contain opacity as the 4th component.

---

<div class="post-metadata">

**Author:** ![Somgolie\_Ejinkeonye](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/somgolie_ejinkeonye/32/46127_2.png) [@Somgolie\_Ejinkeonye](https://discourse.threejs.org/u/Somgolie_Ejinkeonye)\
**Post date:** [April 6, 2024, 12:22am UTC](https://discourse.threejs.org/t/how-to-control-opacity-of-an-object-with-shaders/63703/3 "2024-04-06T00:22:41Z")

</div>

```javascript
function fragmentShader() {
  return `
      uniform vec3 colorA; 
      uniform vec3 colorB; 
      varying vec3 vUv;

      void main() {
	 
        gl_FragColor = vec4(mix(colorA, colorB, vUv.z), 0.5);
      }
  `
}

```

ong it was that easy… thank you
