-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVehicleCharacter.lua
More file actions
385 lines (297 loc) · 14.3 KB
/
VehicleCharacter.lua
File metadata and controls
385 lines (297 loc) · 14.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
---Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
local VehicleCharacter_mt = Class(VehicleCharacter)
---
function VehicleCharacter.new(vehicle, customMt)
local self = setmetatable({}, customMt or VehicleCharacter_mt)
self.vehicle = vehicle
self.characterNode = nil
self.allowUpdate = true
self.ikChainTargets = {}
self.animationCharsetId = nil
self.animationPlayer = nil
self.useAnimation = false
self.isVisible = false
return self
end
---
function VehicleCharacter:load(xmlFile, xmlNode)
XMLUtil.checkDeprecatedXMLElements(xmlFile, xmlNode .. "#index", xmlNode .. "#node") --FS17 to FS19
self.characterNode = xmlFile:getValue(xmlNode.."#node", nil, self.vehicle.components, self.vehicle.i3dMappings)
if self.characterNode ~= nil then
self.parentComponent = self.vehicle:getParentComponent(self.characterNode)
self.characterCameraMinDistance = xmlFile:getValue(xmlNode.."#cameraMinDistance", 1.5)
self.characterDistanceRefNodeCustom = xmlFile:getValue(xmlNode.."#distanceRefNode", nil, self.vehicle.components, self.vehicle.i3dMappings)
self.characterDistanceRefNode = self.characterDistanceRefNodeCustom or self.characterNode
setVisibility(self.characterNode, false)
self.useAnimation = xmlFile:getValue(xmlNode.."#useAnimation", false)
self.useIdleAnimation = xmlFile:getValue(xmlNode.."#useIdleAnimation", not self.useAnimation and Platform.gameplay.hasVehicleCharacterIdleAnimations)
if not self.useAnimation then
self.ikChainTargets = {}
IKUtil.loadIKChainTargets(xmlFile, xmlNode, self.vehicle.components, self.ikChainTargets, self.vehicle.i3dMappings)
end
self.characterSpineRotationOffset = xmlFile:getValue(xmlNode.."#spineRotationOffset", nil, true)
self.characterSpineSpeedDepended = xmlFile:getValue(xmlNode.."#speedDependedSpine", false)
self.characterSpineNodeMinRot = xmlFile:getValue(xmlNode.."#spineNodeMinRot", 10)
self.characterSpineNodeMaxRot = xmlFile:getValue(xmlNode.."#spineNodeMaxRot", -10)
self.characterSpineNodeMinAcc = xmlFile:getValue(xmlNode.."#spineNodeMinAcc", -1) / (1000*1000)
self.characterSpineNodeMaxAcc = xmlFile:getValue(xmlNode.."#spineNodeMaxAcc", 1) / (1000*1000)
self.characterSpineNodeAccDeadZone = xmlFile:getValue(xmlNode.."#spineNodeAccDeadZone", 0.2) / (1000*1000)
self.characterSpineLastRotation = 0
self:setCharacterVisibility(self.isVisible)
self.maxUpdateDistance = xmlFile:getValue(xmlNode.."#maxUpdateDistance", VehicleCharacter.DEFAULT_MAX_UPDATE_DISTANCE)
setClipDistance(self.characterNode, xmlFile:getValue(xmlNode.."#clipDistance", VehicleCharacter.DEFAULT_CLIP_DISTANCE))
return true
end
return false
end
---
function VehicleCharacter:getParentComponent()
return self.parentComponent
end
---
function VehicleCharacter:loadCharacter(playerStyle, asyncCallbackObject, asyncCallbackFunction, asyncCallbackArguments)
if playerStyle == nil then
asyncCallbackFunction(asyncCallbackObject, false, asyncCallbackArguments)
return
end
if self.playerModel ~= nil then
self.playerModel:delete()
end
self.playerModel = HumanModel.new()
local arguments = {
asyncCallbackObject = asyncCallbackObject,
asyncCallbackFunction = asyncCallbackFunction,
asyncCallbackArguments = asyncCallbackArguments,
playerStyle = playerStyle
}
self.playerModel:load(playerStyle.xmlFilename, false, false, self.useAnimation, self.characterLoaded, self, arguments)
end
---
function VehicleCharacter:characterLoaded(loadingState, arguments)
if loadingState == HumanModelLoadingState.OK then
-- Model was unloaded before finishing loading
if self.playerModel.rootNode == nil then
return
end
self.isStyleLoaded = false
-- Link to the vehicle
local linkNode = Utils.getNoNil(self.characterNode, self.vehicle.rootNode)
link(linkNode, self.playerModel.rootNode)
-- Update IK
for ikChainId, target in pairs(self.ikChainTargets) do
IKUtil.setTarget(self.playerModel:getIKChains(), ikChainId, target)
end
if self.playerModel.thirdPersonHipsNode ~= nil then
local x, y, z = VehicleCharacter.SPINE_ROTATION[1], VehicleCharacter.SPINE_ROTATION[2], VehicleCharacter.SPINE_ROTATION[3]
if self.characterSpineRotationOffset ~= nil then
x, y, z = self.characterSpineRotationOffset[1] + x, self.characterSpineRotationOffset[2] + y, self.characterSpineRotationOffset[3] + z
end
setRotation(self.playerModel.thirdPersonHipsNode, x, y, z)
end
self.characterDistanceRefNode = self.characterDistanceRefNodeCustom or self.playerModel.thirdPersonHeadNode
if self.playerModel.skeleton ~= nil and getNumOfChildren(self.playerModel.skeleton) > 0 then
if self.useAnimation then
local skeleton = self.playerModel.skeleton
local animNode = g_animCache:getNode(AnimationCache.CHARACTER)
cloneAnimCharacterSet(getChildAt(animNode, 0), skeleton)
self.animationCharsetId = getAnimCharacterSet(getChildAt(skeleton, 0))
local animationPlayer = createConditionalAnimation()
if animationPlayer ~= 0 then
self.animationPlayer = animationPlayer
end
-- for i=0, getAnimNumOfClips(self.animationCharsetId)-1 do
-- print(getAnimClipName(self.animationCharsetId, i))
-- end
if self.animationCharsetId == 0 then
self.animationCharsetId = nil
Logging.devError("-- [VehicleCharacter:loadCharacter] Could not load animation CharSet from: [%s/%s]", getName(getParent(skeleton)), getName(skeleton))
printScenegraph(getParent(skeleton))
end
elseif self.useIdleAnimation then
local skeleton = self.playerModel.skeleton
local animNode = g_animCache:getNode(AnimationCache.VEHICLE_CHARACTER)
cloneAnimCharacterSet(getChildAt(animNode, 0), skeleton)
self.animationCharsetId = getAnimCharacterSet(skeleton)
if self.animationCharsetId ~= 0 then
self.idleClipIndex = getAnimClipIndex(self.animationCharsetId, "idle1Source")
clearAnimTrackClip(self.animationCharsetId, 0)
assignAnimTrackClip(self.animationCharsetId, 0, self.idleClipIndex)
setAnimTrackLoopState(self.animationCharsetId, 0, true)
self.idleAnimationState = false
else
self.useIdleAnimation = false
self.animationCharsetId = nil
Logging.devError("-- [VehicleCharacter:loadCharacter] Could not load animation CharSet from: [%s/%s]", getName(getParent(skeleton)), getName(skeleton))
end
end
end
local playerStyle = arguments.playerStyle
self.playerModel:loadFromStyleAsync(playerStyle, function(target, loadingState, args)
if loadingState == HumanModelLoadingState.OK then
self.isStyleLoaded = true
self:setDirty(true)
self:setCharacterVisibility(self.isVisible)
end
end, nil, nil)
else
self.playerModel:delete()
self.playerModel = nil
Logging.error("Failed to load vehicleCharacter")
end
local asyncCallbackObject = arguments.asyncCallbackObject
local asyncCallbackFunction = arguments.asyncCallbackFunction
local asyncCallbackArguments = arguments.asyncCallbackArguments
if asyncCallbackFunction ~= nil then
asyncCallbackFunction(asyncCallbackObject, loadingState, asyncCallbackArguments)
end
end
---
function VehicleCharacter:delete()
self:unloadCharacter()
end
---
function VehicleCharacter:unloadCharacter()
if self.playerModel ~= nil then
self.characterDistanceRefNode = self.characterDistanceRefNodeCustom or self.characterNode
self.playerModel:delete()
self.playerModel = nil
if self.animationPlayer ~= nil then
delete(self.animationPlayer)
self.animationPlayer = nil
end
end
end
---
function VehicleCharacter:setDirty(setAllDirty)
if self.playerModel ~= nil then
for chainId, target in pairs(self.ikChainTargets) do
if target.setDirty or setAllDirty then
IKUtil.setIKChainDirty(self.playerModel:getIKChains(), chainId)
end
end
end
end
---
function VehicleCharacter:updateIKChains()
IKUtil.updateIKChains(self.playerModel:getIKChains(), true)
end
---
function VehicleCharacter:setIKChainPoseByTarget(target, poseId)
if self.playerModel ~= nil then
local ikChains = self.playerModel:getIKChains()
local chain = IKUtil.getIKChainByTarget(ikChains, target)
if chain ~= nil then
IKUtil.setIKChainPose(ikChains, chain.id, poseId)
end
end
end
---
function VehicleCharacter:setSpineDirty(acc)
if math.abs(acc) < self.characterSpineNodeAccDeadZone then
acc = 0
end
local alpha = math.clamp((acc - self.characterSpineNodeMinAcc) / (self.characterSpineNodeMaxAcc - self.characterSpineNodeMinAcc), 0, 1)
local rotation = MathUtil.lerp(self.characterSpineNodeMinRot, self.characterSpineNodeMaxRot, alpha)
if rotation ~= self.characterSpineLastRotation then
self.characterSpineLastRotation = self.characterSpineLastRotation * 0.95 + rotation * 0.05
setRotation(self.player.spineNode, self.characterSpineLastRotation, 0, 0)
self:setDirty()
end
end
---
function VehicleCharacter:updateVisibility()
--#profile RemoteProfiler.zoneBeginN("VehicleCharacter:updateVisibility")
if self.isStyleLoaded and entityExists(self.characterDistanceRefNode) and entityExists(g_cameraManager:getActiveCamera()) then
local dist = calcDistanceFrom(self.characterDistanceRefNode, g_cameraManager:getActiveCamera())
local visible = dist >= self.characterCameraMinDistance
self:setCharacterVisibility(visible)
end
--#profile RemoteProfiler.zoneEnd()
end
---
function VehicleCharacter:setCharacterVisibility(isVisible)
if self.characterNode ~= nil then
setVisibility(self.characterNode, isVisible)
end
if self.playerModel ~= nil and self.playerModel.isLoaded then
self.playerModel:setVisibility(isVisible)
end
self.isVisible = isVisible
end
---
function VehicleCharacter:setAllowCharacterUpdate(state)
self.allowUpdate = state
end
---
function VehicleCharacter:getAllowCharacterUpdate()
return self.allowUpdate
end
---
function VehicleCharacter:update(dt)
--#profile RemoteProfiler.zoneBeginN("VehicleCharacter:update")
if self.playerModel ~= nil and self.playerModel.isLoaded then
if Platform.gameplay.allowVehicleCharacterIKDirtyUpdate then
if self.vehicle.currentUpdateDistance < self.maxUpdateDistance and self.isVisible then
if self:getAllowCharacterUpdate() then
self:setDirty(false)
end
self:updateIKChains()
end
end
if self.useIdleAnimation then
if self.vehicle.currentUpdateDistance < self.maxUpdateDistance then
if not self.idleAnimationState then
self.idleAnimationState = true
enableAnimTrack(self.animationCharsetId, 0)
end
else
if self.idleAnimationState then
self.idleAnimationState = false
disableAnimTrack(self.animationCharsetId, 0)
end
end
end
end
--#profile RemoteProfiler.zoneEnd()
end
---
function VehicleCharacter:getIKChainTargets()
return self.ikChainTargets
end
---
function VehicleCharacter:setIKChainTargets(targets, force)
if self.ikChainTargets ~= targets or force then
self.ikChainTargets = targets
if self.playerModel ~= nil then
for ikChainId, target in pairs(self.ikChainTargets) do
IKUtil.setTarget(self.playerModel:getIKChains(), ikChainId, target)
end
self:setDirty(true)
end
end
end
---
function VehicleCharacter:getPlayerStyle()
if self.playerModel ~= nil then
return self.playerModel.style
end
return nil
end
---
function VehicleCharacter.registerCharacterXMLPaths(schema, basePath, name)
schema:register(XMLValueType.NODE_INDEX, basePath .. "#node", "Character root node")
schema:register(XMLValueType.FLOAT, basePath .. "#cameraMinDistance", "Min. distance until character is hidden", 1.5)
schema:register(XMLValueType.NODE_INDEX, basePath .. "#distanceRefNode", "Distance reference node", "Character root node")
schema:register(XMLValueType.BOOL, basePath .. "#useAnimation", "Use animation instead of ik chains", false)
schema:register(XMLValueType.BOOL, basePath .. "#useIdleAnimation", "Apply character idle animation additionally to ik chain control", "set if #useAnimation not set")
schema:register(XMLValueType.VECTOR_ROT, basePath .. "#spineRotationOffset", "Spine rotation offset")
schema:register(XMLValueType.BOOL, basePath .. "#speedDependedSpine", "Speed dependent spine", false)
schema:register(XMLValueType.ANGLE, basePath .. "#spineNodeMinRot", "Spine node min. rotation", 10)
schema:register(XMLValueType.ANGLE, basePath .. "#spineNodeMaxRot", "Spine node max. rotation", -10)
schema:register(XMLValueType.FLOAT, basePath .. "#spineNodeMinAcc", "Spine node min. acceleration", -1)
schema:register(XMLValueType.FLOAT, basePath .. "#spineNodeMaxAcc", "Spine node max. acceleration", 1)
schema:register(XMLValueType.FLOAT, basePath .. "#spineNodeAccDeadZone", "Spine node acceleration dead zone", 0.2)
schema:register(XMLValueType.FLOAT, basePath .. "#maxUpdateDistance", "Max. distance to vehicle root to update ik chains of character", VehicleCharacter.DEFAULT_MAX_UPDATE_DISTANCE)
schema:register(XMLValueType.FLOAT, basePath .. "#clipDistance", "Clip distance of character", VehicleCharacter.DEFAULT_CLIP_DISTANCE)
IKUtil.registerIKChainTargetsXMLPaths(schema, basePath)
end