File size: 5,144 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { BackSide, FrontSide, CubeUVReflectionMapping } from '../../constants.js';
import { BoxGeometry } from '../../geometries/BoxGeometry.js';
import { PlaneGeometry } from '../../geometries/PlaneGeometry.js';
import { ShaderMaterial } from '../../materials/ShaderMaterial.js';
import { Color } from '../../math/Color.js';
import { Mesh } from '../../objects/Mesh.js';
import { ShaderLib } from '../shaders/ShaderLib.js';
import { cloneUniforms } from '../shaders/UniformsUtils.js';

function WebGLBackground(renderer, cubemaps, state, objects, alpha, premultipliedAlpha) {
	const clearColor = new Color(0x000000);
	let clearAlpha = alpha === true ? 0 : 1;

	let planeMesh;
	let boxMesh;

	let currentBackground = null;
	let currentBackgroundVersion = 0;
	let currentTonemapping = null;

	function render(renderList, scene) {
		let forceClear = false;
		let background = scene.isScene === true ? scene.background : null;

		if (background && background.isTexture) {
			background = cubemaps.get(background);
		}

		// Ignore background in AR
		// TODO: Reconsider this.

		const xr = renderer.xr;
		const session = xr.getSession && xr.getSession();

		if (session && session.environmentBlendMode === 'additive') {
			background = null;
		}

		if (background === null) {
			setClear(clearColor, clearAlpha);
		} else if (background && background.isColor) {
			setClear(background, 1);
			forceClear = true;
		}

		if (renderer.autoClear || forceClear) {
			renderer.clear(renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil);
		}

		if (background && (background.isCubeTexture || background.mapping === CubeUVReflectionMapping)) {
			if (boxMesh === undefined) {
				boxMesh = new Mesh(
					new BoxGeometry(1, 1, 1),
					new ShaderMaterial({
						name: 'BackgroundCubeMaterial',
						uniforms: cloneUniforms(ShaderLib.cube.uniforms),
						vertexShader: ShaderLib.cube.vertexShader,
						fragmentShader: ShaderLib.cube.fragmentShader,
						side: BackSide,
						depthTest: false,
						depthWrite: false,
						fog: false,
					})
				);

				boxMesh.geometry.deleteAttribute('normal');
				boxMesh.geometry.deleteAttribute('uv');

				boxMesh.onBeforeRender = function (renderer, scene, camera) {
					this.matrixWorld.copyPosition(camera.matrixWorld);
				};

				// enable code injection for non-built-in material
				Object.defineProperty(boxMesh.material, 'envMap', {
					get: function () {
						return this.uniforms.envMap.value;
					},
				});

				objects.update(boxMesh);
			}

			boxMesh.material.uniforms.envMap.value = background;
			boxMesh.material.uniforms.flipEnvMap.value = background.isCubeTexture && background.isRenderTargetTexture === false ? -1 : 1;

			if (currentBackground !== background || currentBackgroundVersion !== background.version || currentTonemapping !== renderer.toneMapping) {
				boxMesh.material.needsUpdate = true;

				currentBackground = background;
				currentBackgroundVersion = background.version;
				currentTonemapping = renderer.toneMapping;
			}

			// push to the pre-sorted opaque render list
			renderList.unshift(boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null);
		} else if (background && background.isTexture) {
			if (planeMesh === undefined) {
				planeMesh = new Mesh(
					new PlaneGeometry(2, 2),
					new ShaderMaterial({
						name: 'BackgroundMaterial',
						uniforms: cloneUniforms(ShaderLib.background.uniforms),
						vertexShader: ShaderLib.background.vertexShader,
						fragmentShader: ShaderLib.background.fragmentShader,
						side: FrontSide,
						depthTest: false,
						depthWrite: false,
						fog: false,
					})
				);

				planeMesh.geometry.deleteAttribute('normal');

				// enable code injection for non-built-in material
				Object.defineProperty(planeMesh.material, 'map', {
					get: function () {
						return this.uniforms.t2D.value;
					},
				});

				objects.update(planeMesh);
			}

			planeMesh.material.uniforms.t2D.value = background;

			if (background.matrixAutoUpdate === true) {
				background.updateMatrix();
			}

			planeMesh.material.uniforms.uvTransform.value.copy(background.matrix);

			if (currentBackground !== background || currentBackgroundVersion !== background.version || currentTonemapping !== renderer.toneMapping) {
				planeMesh.material.needsUpdate = true;

				currentBackground = background;
				currentBackgroundVersion = background.version;
				currentTonemapping = renderer.toneMapping;
			}

			// push to the pre-sorted opaque render list
			renderList.unshift(planeMesh, planeMesh.geometry, planeMesh.material, 0, 0, null);
		}
	}

	function setClear(color, alpha) {
		state.buffers.color.setClear(color.r, color.g, color.b, alpha, premultipliedAlpha);
	}

	return {
		getClearColor: function () {
			return clearColor;
		},
		setClearColor: function (color, alpha = 1) {
			clearColor.set(color);
			clearAlpha = alpha;
			setClear(clearColor, clearAlpha);
		},
		getClearAlpha: function () {
			return clearAlpha;
		},
		setClearAlpha: function (alpha) {
			clearAlpha = alpha;
			setClear(clearColor, clearAlpha);
		},
		render: render,
	};
}

export { WebGLBackground };