File size: 1,609 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
import { WebGLLights } from './WebGLLights.js';

function WebGLRenderState(extensions, capabilities) {
	const lights = new WebGLLights(extensions, capabilities);

	const lightsArray = [];
	const shadowsArray = [];

	function init() {
		lightsArray.length = 0;
		shadowsArray.length = 0;
	}

	function pushLight(light) {
		lightsArray.push(light);
	}

	function pushShadow(shadowLight) {
		shadowsArray.push(shadowLight);
	}

	function setupLights(physicallyCorrectLights) {
		lights.setup(lightsArray, physicallyCorrectLights);
	}

	function setupLightsView(camera) {
		lights.setupView(lightsArray, camera);
	}

	const state = {
		lightsArray: lightsArray,
		shadowsArray: shadowsArray,

		lights: lights,
	};

	return {
		init: init,
		state: state,
		setupLights: setupLights,
		setupLightsView: setupLightsView,

		pushLight: pushLight,
		pushShadow: pushShadow,
	};
}

function WebGLRenderStates(extensions, capabilities) {
	let renderStates = new WeakMap();

	function get(scene, renderCallDepth = 0) {
		let renderState;

		if (renderStates.has(scene) === false) {
			renderState = new WebGLRenderState(extensions, capabilities);
			renderStates.set(scene, [renderState]);
		} else {
			if (renderCallDepth >= renderStates.get(scene).length) {
				renderState = new WebGLRenderState(extensions, capabilities);
				renderStates.get(scene).push(renderState);
			} else {
				renderState = renderStates.get(scene)[renderCallDepth];
			}
		}

		return renderState;
	}

	function dispose() {
		renderStates = new WeakMap();
	}

	return {
		get: get,
		dispose: dispose,
	};
}

export { WebGLRenderStates };