File size: 9,593 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
184
185
186
if FirstLoad then -- hopefully noone changes those before us, so we can fetch them safely
	ThreePointLightingDefaults = {}
	ThreePointLightingDefaults.KeyLightAngle = hr.KeyLightAngle
	ThreePointLightingDefaults.KeyLightDirX = hr.KeyLightDirX
	ThreePointLightingDefaults.KeyLightDirY = hr.KeyLightDirY
	ThreePointLightingDefaults.KeyLightDirZ = hr.KeyLightDirZ
	ThreePointLightingDefaults.KeyLightColor = hr.KeyLightColor
	ThreePointLightingDefaults.KeyLightIntensity = hr.KeyLightIntensity
	ThreePointLightingDefaults.FillLightAngle = hr.FillLightAngle
	ThreePointLightingDefaults.FillLightDirX = hr.FillLightDirX
	ThreePointLightingDefaults.FillLightDirY = hr.FillLightDirY
	ThreePointLightingDefaults.FillLightDirZ = hr.FillLightDirZ
	ThreePointLightingDefaults.FillLightColor = hr.FillLightColor
	ThreePointLightingDefaults.FillLightIntensity = hr.FillLightIntensity
	ThreePointLightingDefaults.BackLightAngle = hr.BackLightAngle
	ThreePointLightingDefaults.BackLightDirX = hr.BackLightDirX
	ThreePointLightingDefaults.BackLightDirY = hr.BackLightDirY
	ThreePointLightingDefaults.BackLightDirZ = hr.BackLightDirZ
	ThreePointLightingDefaults.BackLightColor = hr.BackLightColor
	ThreePointLightingDefaults.BackLightIntensity = hr.BackLightIntensity
	ThreePointLightingDefaults.TPLCameraAngle = hr.TPLCameraAngle
	ThreePointLightingDefaults.TPLCameraDirX = hr.TPLCameraDirX
	ThreePointLightingDefaults.TPLCameraDirY = hr.TPLCameraDirY
	ThreePointLightingDefaults.TPLCameraDirZ = hr.TPLCameraDirZ
end

DefineClass.ThreePointLighting = {
	__parents = {"PersistedRenderVars"},
	group = "ThreePointLightingRenderVars",
	StoreAsTable = false,
	PresetClass = "ThreePointLighting",
	EditorMenubarName = "ThreePointLighting",

	properties = {
		{ hr = true, name = "Angle",			id = "KeyLightAngle",		category = "Key Light",		editor = "number",	default = ThreePointLightingDefaults.KeyLightAngle,	read_only = true },
		{ hr = true, name = "Direction X", 	id = "KeyLightDirX", 		category = "Key Light", 	editor = "number", 	default = ThreePointLightingDefaults.KeyLightDirX, 	read_only = true },
		{ hr = true, name = "Direction Y", 	id = "KeyLightDirY", 		category = "Key Light", 	editor = "number", 	default = ThreePointLightingDefaults.KeyLightDirY, 	read_only = true },
		{ hr = true, name = "Direction Z", 	id = "KeyLightDirZ", 		category = "Key Light", 	editor = "number", 	default = ThreePointLightingDefaults.KeyLightDirZ,		read_only = true },
		{ hr = true, name = "Color", 			id = "KeyLightColor",		category = "Key Light",		editor = "color",	default = ThreePointLightingDefaults.KeyLightColor, 	alpha = false },	
		{ hr = true, name = "Intensity",		id = "KeyLightIntensity",	category = "Key Light", 	editor = "number", 	default = ThreePointLightingDefaults.KeyLightIntensity, slider = true, min = 0, max = 255, scale = 255 },

		{ hr = true, name = "Angle",			id = "FillLightAngle",		category = "Fill Light",	editor = "number",	default = ThreePointLightingDefaults.FillLightAngle,	read_only = true },
		{ hr = true, name = "Direction X",	id = "FillLightDirX", 		category = "Fill Light", 	editor = "number", 	default = ThreePointLightingDefaults.FillLightDirX,	read_only = true },
		{ hr = true, name = "Direction Y",	id = "FillLightDirY",		category = "Fill Light", 	editor = "number", 	default = ThreePointLightingDefaults.FillLightDirY,	read_only = true },
		{ hr = true, name = "Direction Z",	id = "FillLightDirZ", 		category = "Fill Light", 	editor = "number", 	default = ThreePointLightingDefaults.FillLightDirZ,	read_only = true },
		{ hr = true, name = "Color", 			id = "FillLightColor",		category = "Fill Light",	editor = "color",	default = ThreePointLightingDefaults.FillLightColor, 	alpha = false },	
		{ hr = true, name = "Intensity",		id = "FillLightIntensity",	category = "Fill Light",	editor = "number", 	default = ThreePointLightingDefaults.FillLightIntensity, slider = true, min = 0, max = 255, scale = 255 },
		
		{ hr = true, name = "Angle",			id = "BackLightAngle",		category = "Back Light",	editor = "number",	default = ThreePointLightingDefaults.BackLightAngle,	read_only = true },
		{ hr = true, name = "Direction X",	id = "BackLightDirX", 		category = "Back Light", 	editor = "number", 	default = ThreePointLightingDefaults.BackLightDirX,	read_only = true },
		{ hr = true, name = "Direction Y",	id = "BackLightDirY", 		category = "Back Light", 	editor = "number", 	default = ThreePointLightingDefaults.BackLightDirY,	read_only = true },
		{ hr = true, name = "Direction Z",	id = "BackLightDirZ", 		category = "Back Light", 	editor = "number", 	default = ThreePointLightingDefaults.BackLightDirZ,	read_only = true },
		{ hr = true, name = "Color", 			id = "BackLightColor",		category = "Back Light",	editor = "color",	default = ThreePointLightingDefaults.BackLightColor, 	alpha = false },	
		{ hr = true, name = "Intensity",		id = "BackLightIntensity",	category = "Back Light",	editor = "number", 	default = ThreePointLightingDefaults.BackLightIntensity, slider = true, min = 0, max = 255, scale = 255 },
	
		{				name = "View Type", 	id = "ViewType", 			category = "Camera", 		editor = "choice",	default = 2, items = { { text = "Disable", value = 0 }, { text = "Camera", value = 1 }, { text = "Reference", value = 2 } }, dont_save = true },
		{ hr = true, name = "Angle",			id = "TPLCameraAngle",		category = "Camera",		editor = "number",	default = ThreePointLightingDefaults.TPLCameraAngle,	read_only = true },
		{ hr = true, name = "Direction X",	id = "TPLCameraDirX",		category = "Camera",		editor = "number",	default = ThreePointLightingDefaults.TPLCameraDirX, 	read_only = true },
		{ hr = true, name = "Direction Y",	id = "TPLCameraDirY",		category = "Camera",		editor = "number",	default = ThreePointLightingDefaults.TPLCameraDirY, 	read_only = true },
		{ hr = true, name = "Direction Z",	id = "TPLCameraDirZ",		category = "Camera",		editor = "number",	default = ThreePointLightingDefaults.TPLCameraDirZ, 	read_only = true },
	},
	
	KeyLight = false,
	FillLight = false,
	BackLight = false,
	Camera = false,
	Model1 = false,
	Model2 = false,
}

DefineClass.TPLControlObj = {
	__parents = { "EditorVisibleObject", "EditorCallbackObject", "Object" },
	entity = "PointLight",
	Name = ""
}

if FirstLoad then
	g_TPLEditor = false
	g_LastViewType = 2
end

function TPLControlObj:Init()
	if IsEditorActive() then
		self:EditorEnter()
	else
		self:EditorExit()
	end
	self:SetScale(40)
	self:SetAxis(
		g_TPLEditor:GetProperty(self.Name .. "DirX"),
		g_TPLEditor:GetProperty(self.Name .. "DirY"),
		g_TPLEditor:GetProperty(self.Name .. "DirZ")
	)
	self:SetAngle(
		g_TPLEditor:GetProperty(self.Name .. "Angle")
	)
end

function TPLControlObj:EditorCallbackRotate()
	g_TPLEditor:SetProperty(self.Name .. "DirX", self:GetAxis():x())
	g_TPLEditor:SetProperty(self.Name .. "DirY", self:GetAxis():y())
	g_TPLEditor:SetProperty(self.Name .. "DirZ", self:GetAxis():z())
	g_TPLEditor:SetProperty(self.Name .. "Angle", self:GetAngle())
	g_TPLEditor:Apply()
	ObjModified(g_TPLEditor)
end

function OnMsg.AfterLightmodelChange(_, lightmodel, _, prev_lightmodel)
	if g_TPLEditor then
		g_TPLEditor:Apply()
	end
end

function ThreePointLighting:OnEditorSelect(selected)
	if selected then
		g_TPLEditor = self
		self:Apply()
		
		self.ViewType = g_LastViewType
		table.change(hr, "ThreePointLighting", {
			EnableThreePointLighting = self.ViewType
		})
		
		local central_pos = GetTerrainCursorXY(UIL.GetScreenSize()/2)
		self.KeyLight = PlaceObject("TPLControlObj", { Name = "KeyLight" })
		self.KeyLight:SetPos(central_pos + point(-2, -2, 1) * guim)
		local key_light_text = Text:new{ editor_ignore = true }
		key_light_text:SetText("Key Light")
		self.KeyLight:Attach(key_light_text)
		
		self.FillLight = PlaceObject("TPLControlObj", { Name = "FillLight" })
		self.FillLight:SetPos(central_pos + point(2, -2, 1) * guim)
		local fill_light_text = Text:new{ editor_ignore = true }
		fill_light_text:SetText("Fill Light")
		self.FillLight:Attach(fill_light_text)
		
		self.BackLight = PlaceObject("TPLControlObj", { Name = "BackLight" })
		self.BackLight:SetPos(central_pos + point(0, 8, 1) * guim)
		local back_light_text = Text:new{ editor_ignore = true }
		back_light_text:SetText("Back Light")
		self.BackLight:Attach(back_light_text)
		
		self.Camera = PlaceObject("TPLControlObj", { Name = "TPLCamera" })
		self.Camera:ChangeEntity("Camera")
		self.Camera:SetPos(central_pos + point(0, -2, 1) * guim)
		local camera_text = Text:new{ editor_ignore = true }
		camera_text:SetText("Camera")
		self.Camera:Attach(camera_text)
		
		local setup_model = function(model)
			model:SetAngle(-90 * 60)
			model:SetHierarchyGameFlags(const.gofUnitLighting)
		end
		
		self.Model1 = PlaceObject("AppearanceObject")
		self.Model1:ApplyAppearance("Barry")
		self.Model1:SetPos(central_pos + point(1, 0) * guim)
		self.Model1:SetAngle(-90 * 60)
		setup_model(self.Model1)
		self.Model2 = PlaceObject("AppearanceObject")
		self.Model2:ApplyAppearance("Buns")
		self.Model2:SetPos(central_pos + point(-1, 0) * guim)
		setup_model(self.Model2)
	else
		g_TPLEditor = false
		g_LastViewType = self.ViewType
		table.restore(hr, "ThreePointLighting")
		DoneObject(self.Camera)
		DoneObject(self.KeyLight)
		DoneObject(self.FillLight)
		DoneObject(self.BackLight)
		DoneObject(self.Model1)
		DoneObject(self.Model2)
		if LastSetLightmodel then
			SetLightmodel(1, LastSetLightmodel[1], 0)
		end
	end
end

function ThreePointLighting:SetViewType(value)
	self.ViewType = value	
	table.restore(hr, "ThreePointLighting")
	table.change(hr, "ThreePointLighting", {
		EnableThreePointLighting = value
	})
end