<commit_before>require "core/editors/level_editor/camera"

UnitPreview = UnitPreview or {}

function UnitPreview:init()
	self._world = Device.create_world()
	self._sg = World.scene_graph(self._world)
	self._rw = World.render_world(self._world)
	self._pw = World.physics_world(self._world)
	self._fpscamera = FPSCamera(self._world, World.spawn_unit(self._world, "core/units/camera"))
	self._unit_name = nil
	self._unit_id = nil

	World.spawn_unit(self._world, "core/units/light", Vector3(1000, 1000, -1000))
end

function UnitPreview:update(dt)
	World.update(self._world, dt)

	if self._unit_id then
		local mesh_component = RenderWorld.mesh_instances(self._rw, self._unit_id)
		if mesh_component then
			local tm, hext = RenderWorld.mesh_obb(self._rw, mesh_component)

			local radius = Vector3.length(hext)
			radius = math.ceil(radius / 4)  * 4
			radius = radius <   1 and   1 or radius
			radius = radius > 100 and 100 or radius

			local camera_unit = self._fpscamera:unit()
			local tr = SceneGraph.instances(self._sg, camera_unit)
			local pos = Vector3(radius, radius, -radius) * 2
			local camera_pos = Matrix4x4.translation(tm) + pos
			local target_pos = Matrix4x4.translation(tm)
			SceneGraph.set_local_rotation(self._sg, tr, Quaternion.look(Vector3.normalize(target_pos - camera_pos)))
			SceneGraph.set_local_position(self._sg, tr, camera_pos)
		end
	end

	self._fpscamera:update(0, 0, {})
end

function UnitPreview:render(dt)
	Device.render(self._world, self._fpscamera:camera())
end

function UnitPreview:shutdown()
	Device.destroy_world(self._world)
end

function UnitPreview:set_preview_resource(type, name)
	local unit = nil
	if type == "unit" then
		unit = name
	elseif type == "sound" then
		unit = "core/units/sound"
	else
		return<commit_msg>Units without mesh deserve preview too<commit_after>require "core/editors/level_editor/camera"

UnitPreview = UnitPreview or {}

function UnitPreview:init()
	self._world = Device.create_world()
	self._sg = World.scene_graph(self._world)
	self._rw = World.render_world(self._world)
	self._pw = World.physics_world(self._world)
	self._fpscamera = FPSCamera(self._world, World.spawn_unit(self._world, "core/units/camera"))
	self._unit_name = nil
	self._unit_id = nil

	World.spawn_unit(self._world, "core/units/light", Vector3(1000, 1000, -1000))
end

function UnitPreview:update(dt)
	World.update(self._world, dt)

	if self._unit_id then
		local mesh_component = RenderWorld.mesh_instances(self._rw, self._unit_id)
		local tm, hext = mesh_component
			and RenderWorld.mesh_obb(self._rw, mesh_component)
			or Matrix4x4.identity(), Vector3(1, 1, 1)

		local radius = Vector3.length(hext)
		radius = math.ceil(radius / 2)  * 2
		radius = radius <   1 and   1 or radius
		radius = radius > 100 and 100 or radius

		local camera_unit = self._fpscamera:unit()
		local tr = SceneGraph.instances(self._sg, camera_unit)
		local pos = Vector3(radius, radius, -radius) * 2
		local camera_pos = Matrix4x4.translation(tm) + pos
		local target_pos = Matrix4x4.translation(tm)
		SceneGraph.set_local_rotation(self._sg, tr, Quaternion.look(Vector3.normalize(target_pos - camera_pos)))
		SceneGraph.set_local_position(self._sg, tr, camera_pos)
	end

	self._fpscamera:update(0, 0, {})
end

function UnitPreview:render(dt)
	Device.render(self._world, self._fpscamera:camera())
end

function UnitPreview:shutdown()
	Device.destroy_world(self._world)
end

function UnitPreview:set_preview_resource(type, name)
	local unit = nil
	if type == "unit" then
		unit = name
	elseif type == "sound" then
		unit = "core/units/sound"
	else
		return
{'fraction_non_alphanumeric': 0.11393465512426697, 'fraction_numerical': 0.02010611561016476, 'mean_word_length': 4.910891089108911, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1364615', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1235, 'n_words': 267}