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
171
172
173
174
175
176
177
178
179
180
181
182
/**************************************************************************/
/*  lod_manager.cpp                                                       */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "lod_manager.h"

#include "scene/3d/camera.h"
#include "scene/3d/lod.h"

bool LODManager::_enabled = true;

void LODManager::register_camera(Camera *p_camera) {
	DEV_ASSERT(p_camera);
	data.cameras.push_back(p_camera);
}

void LODManager::remove_camera(Camera *p_camera) {
	data.cameras.erase(p_camera);
}

void LODManager::register_lod(LOD *p_lod, uint32_t p_queue_id) {
	ERR_FAIL_UNSIGNED_INDEX(p_queue_id, NUM_LOD_QUEUES);
	data.queues[p_queue_id].lods.push_back(p_lod);
}

void LODManager::unregister_lod(LOD *p_lod, uint32_t p_queue_id) {
	ERR_FAIL_UNSIGNED_INDEX(p_queue_id, NUM_LOD_QUEUES);
	data.queues[p_queue_id].lods.erase(p_lod);
}

void LODManager::_update_queue(uint32_t p_queue_id, const Vector3 *p_camera_positions, uint32_t p_num_cameras) {
	// Some local aliases.
	Queue &queue = data.queues[p_queue_id];
	LocalVector<LOD *> &lods = queue.lods;

	uint32_t total_lods = lods.size();

	if (!total_lods) {
		return;
	}

	// Wraparound.
	queue.lod_iterator %= total_lods;

	uint32_t first_lod = queue.lod_iterator;

	uint32_t num_lods_to_check = 1;
	uint32_t num_lods_to_change = 1;

	switch (p_queue_id) {
		case 4: {
			num_lods_to_check = 3125;
			num_lods_to_change = 32;
		} break;
		case 3: {
			num_lods_to_check = 256;
			num_lods_to_change = 8;
		} break;
		case 2: {
			num_lods_to_check = 27;
			num_lods_to_change = 4;
		} break;
		case 1: {
			num_lods_to_check = 4;
			num_lods_to_change = 1;
		} break;
		default: {
		} break;
	}

	// No point updating more lods than the total.
	num_lods_to_check = MIN(num_lods_to_check, total_lods);
	num_lods_to_change = MIN(num_lods_to_change, total_lods);

	// Find minimum distances to cameras...
	uint32_t changed = 0;

	for (uint32_t l = 0; l < num_lods_to_check; l++) {
		uint32_t lod_id = (first_lod + l) % total_lods;
		LOD *lod = lods[lod_id];
		Vector3 lod_pos = lod->get_global_translation();

		float min_dist = FLT_MAX;
		for (uint32_t c = 0; c < p_num_cameras; c++) {
			float dist = (lod_pos - p_camera_positions[c]).length_squared();
			min_dist = MIN(min_dist, dist);
		}

		if (lod->_lod_update(min_dist)) {
			changed++;
			if (changed >= num_lods_to_change) {
				// Only update the iterator to where we got to.
				num_lods_to_check = l + 1;
				break;
			}
		}
	}

	queue.lod_iterator += num_lods_to_check;
}

void LODManager::notify_saving(bool p_active) {
	// When saving in the editor, to prevent file delta due to
	// different visibilities from LOD childs, we standardize
	// to showing the first LOD.
	MutexLock lock(data.mutex);
	data.saving = p_active;

	if (p_active) {
		for (uint32_t n = 0; n < NUM_LOD_QUEUES; n++) {
			Queue &queue = data.queues[n];
			LocalVector<LOD *> &lods = queue.lods;

			for (uint32_t l = 0; l < lods.size(); l++) {
				lods[l]->_lod_pre_save();
			}
		}
	}
}

void LODManager::update() {
	if (!_enabled) {
		return;
	}

	MutexLock lock(data.mutex);

	// We don't want to change the visibilities while saving from the editor.
	if (data.saving) {
		return;
	}

	// Get all camera positions
	// Reserve enough for all cameras, some may not be used.
	Vector3 *camera_positions = (Vector3 *)alloca(data.cameras.size() * sizeof(Vector3));

	uint32_t num_cameras = 0;
	for (uint32_t c = 0; c < data.cameras.size(); c++) {
		const Camera *camera = data.cameras[c];

		// Ignore ortho cameras for LOD.
		if (!camera->get_affect_lod() || !camera->is_current() || (camera->get_projection() == Camera::PROJECTION_ORTHOGONAL)) {
			continue;
		}

		camera_positions[num_cameras++] = camera->get_global_transform().origin;
	}

	if (!num_cameras) {
		return;
	}

	for (uint32_t n = 0; n < NUM_LOD_QUEUES; n++) {
		_update_queue(n, camera_positions, num_cameras);
	}
}