mucuk6547 Posted June 4, 2023 Share Posted June 4, 2023 local vehLiTable = { leftEN ={}, rightEN ={}, left={},right={}, color ={}, isBlown={}, vehType={}, strOut={} } local gWorldSelfShadow = true -- enables object self shadowing ( may be bugged for rotated objects on a custom map) local gLightTheta = math.rad(6) -- Theta is the inner cone angle (6) local gLightPhi = math.rad(30) -- Phi is the outer cone angle (18) local gLightFalloff = 1.5 -- light intensity attenuation between the phi and theta areas local gLightAttenuation = 20 -- 15 local flTimerUpdate = 200 -- the effect update time interval local lightMaxDistance = 90 local excludeVehicleId = {592,577,511,512,593,520,553,476,519,460,513,548,425,417,487,488,497,563,447,469,472,473,493,595,484,430,453,452, 446,454,581,509,481,462,521,463,510,522,461,448,468,586,602,496,401,518,527,589,419,587,533,526,474,545,517,410,600,436,439,549,491,445,604, 507,585,466,492,546,551,516,467,426,547,405,580,409,550,566,540,421,529,485,431,438,437,574,420,525,408,552,433,490,528,407,544,470,432,601, 428,523,499,609,498,524,532,578,486,406,573,455,588,403,423,414,443,515,514,531,456,459,422,482,605,530,418,572,582,413,440,543,583,478,554, 579,400,404,489,505,479,442,458,536,575,534,567,535,576,412,402,542,603,475,429,541,415,480,562,565,434,494,502,503,411,559,561,560,506,451, 558,555,477,441,464,594,501,465,564,606,607,610,584,611,608,435,450,591,590,538,570,569,537,449,568,424,504,457,483,508,571,500,444,556,557, 471,495,539} function getPositionFromElementOffset(element,offX,offY,offZ) local m = getElementMatrix ( element ) -- Get the matrix local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1] -- Apply transform local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2] local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3] return x, y, z -- Return the transformed point end addEventHandler("onClientPreRender", root, function() for index,this in ipairs(getElementsByType("vehicle")) do if vehLiTable.leftEN[this] then local rx, ry, rz = getElementRotation(this, "ZXY") ry = ry - 4 rz = rz - 90 local x1,y1,z1,x2,y2,z2 local col = vehLiTable.color[this] local vehType = vehLiTable.vehType[this] if vehType == "Bike" then x1,y1,z1 = getPositionFromElementOffset(this,0,0.8,0.1) -- (left,front,above) elseif vehType == "Train" then x1,y1,z1 = getPositionFromElementOffset(this,-0.7,15.0,-0.9) -- (left,front,above) x2,y2,z2 = getPositionFromElementOffset(this,0.7,15.0,-0.9) -- (left,front,above) else x1,y1,z1 = getPositionFromElementOffset(this,-1.0,0.5,-0.1) -- (left,front,above) x2,y2,z2 = getPositionFromElementOffset(this,1.0,-5.0,-0.1) -- (left,front,above) end if vehLiTable.leftEN[this] then exports.dynamic_lighting:setLightDirection(vehLiTable.left[this],rx,ry,rz,true) exports.dynamic_lighting:setLightPosition(vehLiTable.left[this],x1,y1,z1) exports.dynamic_lighting:setLightColor(vehLiTable.left[this],col[1],col[2],col[3],1) end if vehLiTable.rightEN[this] and vehType~="Bike" then exports.dynamic_lighting:setLightDirection(vehLiTable.right[this],rx,ry,rz,true) exports.dynamic_lighting:setLightPosition(vehLiTable.right[this],x2,y2,z2) exports.dynamic_lighting:setLightColor(vehLiTable.right[this],col[1],col[2],col[3],1) end end end for index,this in ipairs(getElementsByType("vehicle")) do if vehLiTable.rightEN[this] then local rx, ry, rz = getElementRotation(this, "ZXY") ry = ry - 4 rz = rz - -90 local x1,y1,z1,x2,y2,z2,x3,y3,z3 local col = vehLiTable.color[this] local vehType = vehLiTable.vehType[this] if vehType == "Bike" then x1,y1,z1 = getPositionFromElementOffset(this,0,0.8,0.1) -- (left,front,above) elseif vehType == "Train" then x1,y1,z1 = getPositionFromElementOffset(this,-0.7,15.0,-0.9) -- (left,front,above) x2,y2,z2 = getPositionFromElementOffset(this,0.7,15.0,-0.9) -- (left,front,above) x3,y3,z3 = getPositionFromElementOffset(this,1.0,0.5,-0.1) -- (left,front,above) else x1,y1,z1 = getPositionFromElementOffset(this,-1.0,0.5,-0.1) -- (left,front,above) x2,y2,z2 = getPositionFromElementOffset(this,1.0,0.5,-0.1) -- (left,front,above) x3,y3,z3 = getPositionFromElementOffset(this,1.0,3.5,-0.1) -- (left,front,above) end if vehLiTable.leftEN[this] then exports.dynamic_lighting:setLightDirection(vehLiTable.left[this],rx,ry,rz,true) exports.dynamic_lighting:setLightPosition(vehLiTable.left[this],x1,y1,z1) exports.dynamic_lighting:setLightColor(vehLiTable.left[this],col[1],col[2],col[3],1) end if vehLiTable.rightEN[this] and vehType~="Bike" then exports.dynamic_lighting:setLightDirection(vehLiTable.right[this],rx,ry,rz,true) exports.dynamic_lighting:setLightPosition(vehLiTable.right[this],x2,y2,z2) exports.dynamic_lighting:setLightColor(vehLiTable.right[this],col[1],col[2],col[3],1) end end end end ) function createWorldLight() return exports.dynamic_lighting:createSpotLight(0,0,3,0,0,0,0,0,0,0,true,gLightFalloff,gLightTheta,gLightPhi,gLightAttenuation,gWorldSelfShadow) end function destroyWorldLight(this) return exports.dynamic_lighting:destroyLight(this) end function isInNightTime() local hour, minutes = getTime() return (hour>=22 or hour<=5) end function lightEffectStop(thisVeh) if vehLiTable.leftEN[thisVeh] then destroyWorldLight(vehLiTable.left[thisVeh]) vehLiTable.leftEN[thisVeh]=false end if vehLiTable.rightEN[thisVeh] then destroyWorldLight(vehLiTable.right[thisVeh]) vehLiTable.rightEN[thisVeh]=false end end function lightEffectManage(thisVeh,vehType) if vehLiTable.isBlown[thisVeh] then return end local posX,posY,posZ = getElementPosition(thisVeh) local camX,camY,camZ = getCameraMatrix() local camDist = getDistanceBetweenPoints3D ( posX,posY,posZ,camX,camY,camZ ) local lightSwitched = false local ispastDist = lightMaxDistance < camDist if ispastDist then if vehLiTable.leftEN[thisVeh] then destroyWorldLight(vehLiTable.left[thisVeh]) vehLiTable.leftEN[thisVeh]=false end if vehLiTable.rightEN[thisVeh] then destroyWorldLight(vehLiTable.right[thisVeh]) vehLiTable.rightEN[thisVeh]=false end return end if ((getVehicleLightState ( thisVeh, 0 )==1 or getVehicleOverrideLights(thisVeh)==1)) and vehLiTable.leftEN[thisVeh] then destroyWorldLight(vehLiTable.left[thisVeh]) vehLiTable.leftEN[thisVeh]=false end if ((getVehicleLightState ( thisVeh, 1 )==1 or getVehicleOverrideLights(thisVeh)==1)) and vehLiTable.rightEN[thisVeh] then destroyWorldLight(vehLiTable.right[thisVeh]) vehLiTable.rightEN[thisVeh]=false end if getVehicleEngineState(thisVeh) then if (getVehicleLightState ( thisVeh, 0 )==0 and getVehicleOverrideLights(thisVeh)==0) then if isInNightTime() then if vehLiTable.leftEN[thisVeh]~=true and (getVehicleOccupant( thisVeh ,0 ) or not vehLiTable.strOut[thisVeh]) then if not ispastDist then vehLiTable.left[thisVeh] = createWorldLight() vehLiTable.leftEN[thisVeh]=true lightSwitched=true end end else if vehLiTable.leftEN[thisVeh]==true then destroyWorldLight(vehLiTable.left[thisVeh]) vehLiTable.leftEN[thisVeh]=false end end end if (getVehicleLightState ( thisVeh, 1 )==0 and getVehicleOverrideLights(thisVeh)==0 and vehType~="Bike") then if isInNightTime() then if vehLiTable.rightEN[thisVeh]~=true and (getVehicleOccupant( thisVeh ,0 ) or not vehLiTable.strOut[thisVeh]) then if not ispastDist then vehLiTable.right[thisVeh] = createWorldLight() vehLiTable.rightEN[thisVeh]=true lightSwitched=true end end else if vehLiTable.rightEN[thisVeh]==true then destroyWorldLight(vehLiTable.right[thisVeh]) vehLiTable.rightEN[thisVeh]=false end end end end if (getVehicleLightState ( thisVeh, 0 )==0 and getVehicleOverrideLights(thisVeh)==2) and vehLiTable.leftEN[thisVeh]~=true then if not ispastDist then vehLiTable.left[thisVeh] = createWorldLight() vehLiTable.leftEN[thisVeh]=true end end if (getVehicleLightState ( thisVeh, 1 )==0 and getVehicleOverrideLights(thisVeh)==2) and vehLiTable.rightEN[thisVeh]~=true and vehType~="Bike" then if not ispastDist then vehLiTable.right[thisVeh] = createWorldLight() vehLiTable.rightEN[thisVeh]=true end end if lightSwitched then vehLiTable.strOut[thisVeh]=false end if vehLiTable.rightEN[thisVeh] or vehLiTable.leftEN[thisVeh] then local r,g,b = getVehicleHeadLightColor(thisVeh) vehLiTable.color[thisVeh] = {r/255,g/255,b/255} end end addEventHandler( "onClientElementStreamOut", getRootElement( ),function () if getElementType( source ) == "vehicle" then vehLiTable.strOut[source] = true end end ) addEventHandler( "onClientElementStreamIn", getRootElement( ),function () if getElementType( source ) == "vehicle" then --vehLiTable.strOut[source] = false end end ) addEventHandler("onClientResourceStart", getResourceRootElement( getThisResource()), function() if FLenTimer then return end local shTeNul = dxCreateShader ( "shaders/shader_null.fx",0,0,false ) engineApplyShaderToWorldTexture(shTeNul, "headlight*" ) for index,thisVeh in ipairs(getElementsByType("vehicle")) do vehLiTable.isBlown[thisVeh] = isVehicleBlown(thisVeh) vehLiTable.strOut[thisVeh] = not isElementStreamedIn(thisVeh) end FLenTimer = setTimer( function() for index,thisVeh in ipairs(getElementsByType("vehicle")) do if isElementStreamedIn(thisVeh) then local vehType = getVehicleType(thisVeh) local vehID = getVehicleID(thisVeh) local isMatch = true for index,name in ipairs(excludeVehicleId) do if vehID==name then isMatch = false end end vehLiTable.vehType[thisVeh] = vehType if isMatch and ((vehType == "Automobile") or (vehType == "Monster Truck") or (vehType == "Bike") or (vehType == "Train")) then if isElementStreamedIn(thisVeh) then lightEffectManage(thisVeh,vehType) elseif not isElementStreamedIn(thisVeh) then lightEffectStop(thisVeh) end end end end end ,flTimerUpdate,0 ) end ) addEventHandler("onClientResourceStop", getResourceRootElement( getThisResource()), function() if not FLenTimer then return end killTimer(FLenTimer) for index,thisVeh in ipairs(getElementsByType("vehicle")) do lightEffectStop(thisVeh) end end ) addEventHandler("onClientVehicleExplode", getRootElement(), function() if getElementType(source) == "vehicle" then vehLiTable.isBlown[source] = true lightEffectStop(source) end end ) addEventHandler("onClientElementDestroy", getRootElement(), function () if getElementType(source) == "vehicle" then lightEffectStop(source) end end ) I can't add bindKey to this code. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now