You can control each pixel via transparency of box or any geometry, by smoothstep function you can calculate transparency based on x-coords of the fragment. Then alpha value determines transparency of fragment.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 3;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry(2, 1, 1);
const settings = {
fade: 1.0,
fadePoint: 0.0
};
const material = new THREE.ShaderMaterial({
uniforms: {
uFade: {
value: settings.fade
},
uFadePoint: {
value: settings.fadePoint
},
uFadeRange: {
value: 1.0
}
},
vertexShader: `
varying vec3 vPosition;
void main() {
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uFade;
uniform float uFadePoint;
uniform float uFadeRange;
varying vec3 vPosition;
void main() {
float alpha = smoothstep(uFadePoint, uFadePoint + uFadeRange, vPosition.x) * uFade;
gl_FragColor = vec4(0.0, 0.5, 1.0, alpha);
}
`,
transparent: true,
depthWrite: false
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
const gui = new GUI();
gui.add(settings, 'fade', 0, 1).name('Fade Strength').onChange(value => {
material.uniforms.uFade.value = value;
});
gui.add(settings, 'fadePoint', -3, 3).name('Fade Point (World X)').onChange(value => {
material.uniforms.uFadePoint.value = value;
});
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
https://codepen.io/Lucas_Mas/full/yyLxWEM
floatattribute for each vertex, which is 0 on one end of the z-axis and 1 on the other end. Set thisfloatas output for the VS and input for the FS, where these will arrive interpolated (from 0 to 1) along the z-axis and could then be used as the alpha value. Depending on how the geometry (i.e. positions) of the box are defined, you could also use the position's z-values in the VS directly to set the outputfloat. Does this answer your question?