How to blur a background Texture?

Hi everyone,
I have a png image that I want to load it as texture. But, I want to show it blurry, like this:
Original image loaded:


Want to achive this:

This is my source code:

const backgroundImg = './assets/backgroundImg.png';
this.bgTexture = await new TextureLoader().loadAsync(backgroundImg);
// how to make loaded image blurry?
this.scene.background = this.bgTexture;

thanks for the replies

add blur to the image

let image=new RGBLoader().
image.load( 'bird.png' , img => {
	new BlurredEnvMapGenerator( renderer ).generate( img , 0 )
} )

Can you please specify where does BlurredEnvMapGenerator come from?

threejs to bvh BlurredEnvMapGenerator

Its using mipmap level for bluring
gl_FragColor = textureCubeUV( envMap, rayDirection, blur );

1 Like

Just want to add that the code uses the mipmaps from PMREMGenerator. These are different ones than the default mipmaps generated by gl.generateMipmap().

Related: Ability to reuse the PMREM generated texture from scene.environment - #5 by Mugen87

Apart from that, I’m not even sure something like BlurredEnvMapGenerator is useful for the OP since the original post just adds a “normal” texture to Scene.background (so no env map).

It’s probably best if you prepare the texture offline with a tool like GIMP before using it in your app. Alternatively, you can work with render-to-texture and use HorizontalBlurShader and VerticalBlurShader to apply a gaussian blur and then use the resulting render target as the background.

3 Likes

I really appreciate for your replay. Can you show an example how to implement render-to-texture using HorizontalBlurShader and VerticalBlurShader in order to modify the texture?
Thanks

Here is a complete live example: Edit fiddle - JSFiddle - Code Playground

And the plain code:

let camera, scene, renderer;

init().then( render );

async function init() {

  camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);

  scene = new THREE.Scene();
  
  renderer = new THREE.WebGLRenderer({antialias: true});
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  const loader = new THREE.TextureLoader();
  const texture = await loader.loadAsync('https://threejs.org/examples/textures/uv_grid_opengl.jpg');
  //scene.background = texture;

  scene.background = blurTexture( texture );

}

function blurTexture(texture) {

  const width = texture.image.width;
  const height = texture.image.height;

  const cameraRTT = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
  const sceneRTT = new THREE.Scene();

  // render targets

  const renderTargetTemp = new THREE.WebGLRenderTarget(width, height);
  const renderTargetFinal = new THREE.WebGLRenderTarget(width, height);

  // shader materials

  const hBlurMaterial = new THREE.ShaderMaterial({
    vertexShader: THREE.HorizontalBlurShader.vertexShader,
    fragmentShader: THREE.HorizontalBlurShader.fragmentShader,
    uniforms: THREE.UniformsUtils.clone(THREE.HorizontalBlurShader.uniforms)
  });

  hBlurMaterial.uniforms.tDiffuse.value = texture;
  hBlurMaterial.uniforms.h.value = 1 / width;

  const vBlurMaterial = new THREE.ShaderMaterial({
    vertexShader: THREE.VerticalBlurShader.vertexShader,
    fragmentShader: THREE.VerticalBlurShader.fragmentShader,
    uniforms: THREE.UniformsUtils.clone(THREE.VerticalBlurShader.uniforms)
  });

  vBlurMaterial.uniforms.tDiffuse.value = renderTargetTemp.texture;
  vBlurMaterial.uniforms.v.value = 1 / height;

  // fullscreen quad

  const planeGeometry = new THREE.PlaneGeometry(2, 2);

  const fullScreenQuad = new THREE.Mesh(planeGeometry, hBlurMaterial);
  sceneRTT.add(fullScreenQuad);

  // first pass

  renderer.setRenderTarget(renderTargetTemp);
  renderer.render(sceneRTT, cameraRTT);
  renderer.setRenderTarget(null);

  // second pass

  fullScreenQuad.material = vBlurMaterial;

  renderer.setRenderTarget(renderTargetFinal);
  renderer.render(sceneRTT, cameraRTT)
  renderer.setRenderTarget(null);
  
  //
  
  return renderTargetFinal.texture;


}

function render() {

  renderer.render(scene, camera);

}
5 Likes

Looks like the TriangleBlurShader works a lot better. Here’s an fiddle using triangle blur: Edit fiddle - JSFiddle - Code Playground.

The function is:

const {OrthographicCamera, Scene, WebGLRenderTarget, ShaderMaterial, UniformsUtils, PlaneGeometry, Mesh, TriangleBlurShader, Vector2} = THREE

function makeTriangleBlurShader(iterations = 10) {
	// Remove texture, because texture is a reserved word in WebGL 2
	const {texture, ...uniforms} = TriangleBlurShader.uniforms

	const TriangleBlurShader2 = {
		...TriangleBlurShader,

		name: 'TriangleBlurShader2',

		uniforms: {
			...uniforms,

			// Replace texture with blurTexture for WebGL 2
			blurTexture: {value: null},
		},
	}

	// Replace texture with blurTexture for WebGL 2
	TriangleBlurShader2.fragmentShader = TriangleBlurShader2.fragmentShader.replace(
		'uniform sampler2D texture;',
		'uniform sampler2D blurTexture;',
	)
	TriangleBlurShader2.fragmentShader = TriangleBlurShader2.fragmentShader.replace(
		'texture2D( texture',
		'texture2D( blurTexture',
	)

	// Make iterations configurable.
	TriangleBlurShader2.fragmentShader = TriangleBlurShader2.fragmentShader.replace(
		'#define ITERATIONS 10.0',
		'#define ITERATIONS ' + iterations + '.0',
	)

	console.log('shader:', TriangleBlurShader2.fragmentShader)

	return TriangleBlurShader2
}

function triangleBlurTexture(
	renderer,
	texture,
	radius = 10,
	passes = 1,
	iterations = 10,
) {
	const width = texture.image.width
	const height = texture.image.height

	// renderer = new WebGLRenderer()

	const cameraRTT = new OrthographicCamera(-1, 1, 1, -1, 0, 1)
	const sceneRTT = new Scene()

	// render targets

	const renderTarget1 = new WebGLRenderTarget(width, height)
	const renderTarget2 = new WebGLRenderTarget(width, height)

	// shader materials

	const shader = makeTriangleBlurShader(iterations)

	const blurMaterial = new ShaderMaterial({
		vertexShader: shader.vertexShader,
		fragmentShader: shader.fragmentShader,
		uniforms: UniformsUtils.clone(shader.uniforms),
	})

	blurMaterial.uniforms.delta.value = new Vector2(1, 1)

	// fullscreen quad

	const planeGeometry = new PlaneGeometry(2, 2)

	const fullScreenQuad = new Mesh(planeGeometry, blurMaterial)
	sceneRTT.add(fullScreenQuad)

	// passes

	let lastTexture = texture

	while (passes--) {
		// vertical pass

		blurMaterial.uniforms.blurTexture.value = lastTexture
		blurMaterial.uniforms.delta.value = new Vector2(radius / width, 0)

		renderer.setRenderTarget(renderTarget1)
		renderer.render(sceneRTT, cameraRTT)
		renderer.setRenderTarget(null)

		lastTexture = renderTarget1.texture

		// horizontal pass

		blurMaterial.uniforms.blurTexture.value = lastTexture
		blurMaterial.uniforms.delta.value = new Vector2(0, radius / height)

		renderer.setRenderTarget(renderTarget2)
		renderer.render(sceneRTT, cameraRTT)
		renderer.setRenderTarget(null)

		lastTexture = renderTarget2.texture
	}

	//

	return lastTexture
}

The radius is a lot easier to control with the triangle blur, here for example a value of 40: Edit fiddle - JSFiddle - Code Playground

2 Likes