Daniele Bartolini 9 سال پیش
والد
کامیت
5fb40920db

+ 2 - 9
samples/01-physics/core/level_editor/level_editor.lua

@@ -1170,10 +1170,9 @@ function LevelEditor:init()
 	self._pw = World.physics_world(self._world)
 	self._pw = World.physics_world(self._world)
 	self._rw = World.render_world(self._world)
 	self._rw = World.render_world(self._world)
 	self._sg = World.scene_graph(self._world)
 	self._sg = World.scene_graph(self._world)
-	self._camera_unit = World.spawn_unit(self._world, "core/units/camera")
 	self._lines_no_depth = World.create_debug_line(self._world, false)
 	self._lines_no_depth = World.create_debug_line(self._world, false)
 	self._lines = World.create_debug_line(self._world, true)
 	self._lines = World.create_debug_line(self._world, true)
-	self._fpscamera = FPSCamera(self._world, self._camera_unit)
+	self._fpscamera = FPSCamera(self._world, World.spawn_unit(self._world, "core/units/camera"))
 	self._mouse = { x = 0, y = 0, dx = 0, dy = 0, button = { left = false, middle = false, right = false }, wheel = { delta = 0 }}
 	self._mouse = { x = 0, y = 0, dx = 0, dy = 0, button = { left = false, middle = false, right = false }, wheel = { delta = 0 }}
 	self._keyboard = { ctrl = false, shift = false }
 	self._keyboard = { ctrl = false, shift = false }
 	self._grid = { size = 1 }
 	self._grid = { size = 1 }
@@ -1194,7 +1193,7 @@ function LevelEditor:init()
 	self.tool = self.place_tool
 	self.tool = self.place_tool
 
 
 	-- Spawn camera
 	-- Spawn camera
-	local camera_transform = SceneGraph.transform_instances(self._sg, self._camera_unit)
+	local camera_transform = SceneGraph.transform_instances(self._sg, self._fpscamera:unit())
 	local pos = Vector3(20, 20, -20)
 	local pos = Vector3(20, 20, -20)
 	local dir = Vector3.normalize(Vector3.zero() - pos)
 	local dir = Vector3.normalize(Vector3.zero() - pos)
 	SceneGraph.set_local_rotation(self._sg, camera_transform, Quaternion.look(dir))
 	SceneGraph.set_local_rotation(self._sg, camera_transform, Quaternion.look(dir))
@@ -1235,12 +1234,6 @@ function LevelEditor:update(dt)
 end
 end
 
 
 function LevelEditor:render(dt)
 function LevelEditor:render(dt)
-	-- Update window's viewport
-	local win_w, win_h = Device.resolution()
-	local aspect = win_w / win_h
-	local camera = World.camera(self._world, self._camera_unit)
-	World.set_camera_aspect(self._world, camera, win_w/win_h)
-
 	Device.render(self._world, self._fpscamera:camera())
 	Device.render(self._world, self._fpscamera:camera())
 end
 end
 
 

+ 31 - 30
samples/01-physics/core/unit_preview/unit_preview.lua

@@ -4,19 +4,12 @@ UnitPreview = UnitPreview or {}
 
 
 function UnitPreview:init()
 function UnitPreview:init()
 	self._world = Device.create_world()
 	self._world = Device.create_world()
-	self._physics_world = World.physics_world(self._world)
 	self._sg = World.scene_graph(self._world)
 	self._sg = World.scene_graph(self._world)
-	self._camera_unit = World.spawn_unit(self._world, "core/units/camera")
-	self._fpscamera = FPSCamera(self._world, self._camera_unit)
-	self._unit = nil
-	self._rotation = 0
-
-	-- Spawn camera
-	local camera_transform = SceneGraph.transform_instances(self._sg, self._camera_unit)
-	local pos = Vector3(5, 5, -5)
-	local dir = Vector3.normalize(Vector3.zero() - pos)
-	SceneGraph.set_local_rotation(self._sg, camera_transform, Quaternion.look(dir))
-	SceneGraph.set_local_position(self._sg, camera_transform, pos)
+	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))
 	World.spawn_unit(self._world, "core/units/light", Vector3(1000, 1000, -1000))
 end
 end
@@ -24,25 +17,29 @@ end
 function UnitPreview:update(dt)
 function UnitPreview:update(dt)
 	World.update(self._world, dt)
 	World.update(self._world, dt)
 
 
-	if self._unit then
-		local tr = SceneGraph.transform_instances(self._sg, self._unit)
-		if tr then
-			SceneGraph.set_local_rotation(self._sg, tr, Quaternion(Vector3(0, 1, 0), self._rotation))
+	if self._unit_id then
+		local meshes = RenderWorld.mesh_instances(self._rw, self._unit_id)
+		if meshes then
+			local tm, hext = RenderWorld.mesh_obb(self._rw, meshes[1])
+
+			local radius = Vector3.length(hext)
+			radius = radius <   1 and   1 or radius
+			radius = radius > 100 and 100 or radius
+
+			local camera_unit = self._fpscamera:unit()
+			local tr = SceneGraph.transform_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, camera_unit, Quaternion.look(Vector3.normalize(target_pos - camera_pos)))
+			SceneGraph.set_local_position(self._sg, camera_unit, camera_pos)
 		end
 		end
 	end
 	end
 
 
-	self._rotation = self._rotation + dt
-
 	self._fpscamera:update(0, 0, {})
 	self._fpscamera:update(0, 0, {})
 end
 end
 
 
 function UnitPreview:render(dt)
 function UnitPreview:render(dt)
-	-- Update window's viewport
-	local win_w, win_h = Device.resolution()
-	local aspect = win_w / win_h
-	local camera = World.camera(self._world, self._camera_unit)
-	World.set_camera_aspect(self._world, camera, win_w/win_h)
-
 	Device.render(self._world, self._fpscamera:camera())
 	Device.render(self._world, self._fpscamera:camera())
 end
 end
 
 
@@ -51,15 +48,19 @@ function UnitPreview:shutdown()
 end
 end
 
 
 function UnitPreview:set_preview_unit(unit)
 function UnitPreview:set_preview_unit(unit)
-	if self._unit then
-		World.destroy_unit(self._world, self._unit)
+	if self._unit_name == unit then
+		return
+	end
+
+	if self._unit_id then
+		World.destroy_unit(self._world, self._unit_id)
 	end
 	end
 
 
-	self._rotation = 0
-	self._unit = World.spawn_unit(self._world, unit)
+	self._unit_name = unit
+	self._unit_id = World.spawn_unit(self._world, unit)
 
 
-	local actor = PhysicsWorld.actor_instances(self._physics_world, self._unit)
+	local actor = PhysicsWorld.actor_instances(self._pw, self._unit_id)
 	if actor then
 	if actor then
-		PhysicsWorld.set_actor_kinematic(self._physics_world, actor, true)
+		PhysicsWorld.set_actor_kinematic(self._pw, actor, true)
 	end
 	end
 end
 end

+ 0 - 3
samples/01-physics/lua/game.lua

@@ -88,9 +88,6 @@ function update(dt)
 end
 end
 
 
 function render(dt)
 function render(dt)
-	local win_w, win_h = Device.resolution()
-	World.set_camera_aspect(wd, camera, win_w/win_h)
-
 	Device.render(wd, fpscamera:camera())
 	Device.render(wd, fpscamera:camera())
 end
 end