23 | function WindBending.initSpecialization() |
24 | local schema = Vehicle.xmlSchema |
25 | schema:setXMLSpecializationType("WindBending") |
26 | |
27 | schema:register(XMLValueType.NODE_INDEX, "vehicle.windBending.windBendingNodes.windBendingNode(?)#node", "Shader node") |
28 | schema:register(XMLValueType.NODE_INDEX, "vehicle.windBending.windBendingNodes.windBendingNode(?)#decalNode", "Extra node that gets the exact same shader parameters") |
29 | schema:register(XMLValueType.NODE_INDEX, "vehicle.windBending.windBendingNodes.windBendingNode(?)#speedReferenceNode", "Reference node to calculate speed of wind") |
30 | schema:register(XMLValueType.FLOAT, "vehicle.windBending.windBendingNodes.windBendingNode(?)#maxBending", "Bending in meters", 0.15) |
31 | schema:register(XMLValueType.FLOAT, "vehicle.windBending.windBendingNodes.windBendingNode(?)#maxBendingNeg", "Negative bending in meters (used if the vehicle drives in reverse)", "same as #maxBending") |
32 | schema:register(XMLValueType.FLOAT, "vehicle.windBending.windBendingNodes.windBendingNode(?)#maxBendingSpeed", "Speed at which max bending state is reached in kmph", 20) |
33 | |
34 | schema:setXMLSpecializationType() |
35 | end |
52 | function WindBending:onLoad(savegame) |
53 | local spec = self.spec_windBending |
54 | |
55 | if self.isClient then |
56 | spec.windBending = {} |
57 | |
58 | self.xmlFile:iterate("vehicle.windBending.windBendingNodes.windBendingNode", function(_, key) |
59 | local entry = {} |
60 | entry.node = self.xmlFile:getValue(key .. "#node", nil, self.components, self.i3dMappings) |
61 | if entry.node ~= nil then |
62 | if getHasClassId(entry.node, ClassIds.SHAPE) and getHasShaderParameter(entry.node, "directionBend") then |
63 | entry.speedReferenceNode = self.xmlFile:getValue(key .. "#speedReferenceNode", entry.node, self.components, self.i3dMappings) |
64 | entry.parentComponent = self:getParentComponent(entry.speedReferenceNode) |
65 | |
66 | entry.maxBending = self.xmlFile:getValue(key .. "#maxBending", 0.15) |
67 | entry.maxBendingNeg = self.xmlFile:getValue(key .. "#maxBendingNeg", entry.maxBending) |
68 | entry.maxBendingSpeed = self.xmlFile:getValue(key .. "#maxBendingSpeed", 20) |
69 | |
70 | entry.decalNode = self.xmlFile:getValue(key .. "#decalNode", nil, self.components, self.i3dMappings) |
71 | |
72 | entry.lastPosition = nil |
73 | |
74 | table.insert(spec.windBending, entry) |
75 | else |
76 | Logging.xmlError(self.xmlFile, "Unable to load wind bending node from xml. Node has not shader parameter 'directionBend'. '%s'", key) |
77 | end |
78 | else |
79 | Logging.xmlError(self.xmlFile, "Unable to load wind bending node from xml. Node not found. '%s'", key) |
80 | end |
81 | end) |
82 | end |
83 | |
84 | if not self.isClient or #spec.windBending == 0 then |
85 | SpecializationUtil.removeEventListener(self, "onUpdate", WindBending) |
86 | end |
87 | end |
94 | function WindBending:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
95 | local spec = self.spec_windBending |
96 | |
97 | local lastSpeed = self:getLastSpeed() |
98 | for i=1, #spec.windBending do |
99 | local windBendingNode = spec.windBending[i] |
100 | |
101 | local x, y, z = localToWorld(windBendingNode.speedReferenceNode, 0, 0, 0) |
102 | if windBendingNode.lastPosition == nil then |
103 | windBendingNode.lastPosition = {x, y, z} |
104 | end |
105 | |
106 | local vx = (x - windBendingNode.lastPosition[1]) * 10 |
107 | local vy = (y - windBendingNode.lastPosition[2]) * 10 |
108 | local vz = (z - windBendingNode.lastPosition[3]) * 10 |
109 | |
110 | if vx ~= 0 or vy ~= 0 or vz ~= 0 then |
111 | vx, vy, vz = worldDirectionToLocal(getParent(windBendingNode.node), vx, vy, vz) |
112 | vx, vy, vz = MathUtil.vector3Normalize(vx, vy, vz) |
113 | |
114 | local maxBending = self.movingDirection >= 0 and windBendingNode.maxBending or windBendingNode.maxBendingNeg |
115 | local bendingAmount = maxBending * (lastSpeed / windBendingNode.maxBendingSpeed) |
116 | g_animationManager:setPrevShaderParameter(windBendingNode.node, "directionBend", vx, vy, vz, bendingAmount, false, "prevDirectionBend") |
117 | |
118 | if windBendingNode.decalNode ~= nil then |
119 | g_animationManager:setPrevShaderParameter(windBendingNode.decalNode, "directionBend", vx, vy, vz, bendingAmount, false, "prevDirectionBend") |
120 | end |
121 | end |
122 | |
123 | windBendingNode.lastPosition[1] = x |
124 | windBendingNode.lastPosition[2] = y |
125 | windBendingNode.lastPosition[3] = z |
126 | end |
127 | end |