Advertisement
Guest User

VR Scripts

a guest
Aug 21st, 2021
29,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.68 KB | None | 0 0
  1. --https://v3rmillion.net/member.php?action=profile&uid=2602
  2.  
  3. local cam = workspace.Camera
  4. local p
  5. local headObject
  6. local events = {}
  7.  
  8. local controls = {
  9. [Enum.UserInputType.MouseButton1] = {"move", "leftHand"},
  10. [Enum.UserInputType.MouseMovement] = {"updateTarget"}, -- do not change
  11. [Enum.UserInputType.MouseWheel] = {"adjustOffset"},
  12. [Enum.UserInputType.MouseButton2] = {"move", "rightHand"},
  13. [Enum.KeyCode.W] = {"move", "head", Vector3.new(0, 1, 0)},
  14. [Enum.KeyCode.A] = {"move", "head", Vector3.new(-1, 0, 0)},
  15. [Enum.KeyCode.S] = {"move", "head", Vector3.new(0, -1, 0)},
  16. [Enum.KeyCode.D] = {"move", "head", Vector3.new(1, 0, 0)},
  17. [Enum.KeyCode.Q] = {"move", "head", Vector3.new(0, 0, -1)},
  18. [Enum.KeyCode.E] = {"move", "head", Vector3.new(0, 0, 1)},
  19. [Enum.KeyCode.LeftShift] = {"gesture", "Fist"},
  20. [Enum.KeyCode.LeftControl] = {"gesture", "Index"},
  21. [Enum.KeyCode.LeftAlt] = {"gesture", "Thumb"},
  22. [Enum.KeyCode.Z] = {"toggle", "canOffset"},
  23. [Enum.KeyCode.R] = {"toggle", "rotationManager"},
  24. [Enum.KeyCode.Tab] = {"selectAxis", "rotationManager"},
  25. [Enum.KeyCode.Left] = {"updateAxisMover", "rotationManager", -1},
  26. [Enum.KeyCode.Right] = {"updateAxisMover", "rotationManager", 1}
  27. }
  28.  
  29. local services = {}
  30.  
  31. setmetatable(services, {
  32. __index = function(t, k)
  33. services[k] = rawget(services, k) or game:GetService(k)
  34. return services[k]
  35. end
  36. })
  37.  
  38. local function angleBetween(vecx, vecy)
  39. return math.acos(vecx:Dot(vecy))
  40. end
  41.  
  42. local hand = {}
  43. hand.__index = hand
  44.  
  45. function hand:gesture(gestureName, keyState)
  46. if keyState ~= 1 then
  47. return
  48. end
  49.  
  50. headObject.realHeadset[self.id .. gestureName] = 1 - headObject.realHeadset[self.id .. gestureName]
  51. end
  52.  
  53. function hand:getMouseRay()
  54. local mousePos = services.UserInputService:GetMouseLocation()
  55. return CFrame.new(cam:ViewportPointToRay(mousePos.x, mousePos.y).Direction * (1.5 + headObject.handOffset))
  56. end
  57.  
  58. function hand:calculateEndRotation()
  59. return CFrame.Angles(cam.CFrame:ToEulerAnglesXYZ()) * CFrame.Angles(self.rotation[1] * (math.pi/6), self.rotation[2] * (math.pi/6), self.rotation[3] * (math.pi/6))
  60. end
  61.  
  62. function hand:updateTarget()
  63.  
  64. local camLook = (cam.CFrame.lookVector * Vector3.new(1,0,1)).unit
  65.  
  66. local theta = angleBetween(Vector3.new(-1, 0, 0), camLook)
  67. theta = camLook.z > 0 and (2 * math.pi) - theta or theta
  68.  
  69. local relativeAngle = (self.id == "l" and -headObject.handAngle or headObject.handAngle)
  70. local startPosition = CFrame.new(-math.cos(theta + relativeAngle), -0.5, -math.sin(theta + relativeAngle)) + (camLook * headObject.handOffset)
  71.  
  72. self.targetPosition = startPosition:Lerp(self:getMouseRay(), self.alpha) * self:calculateEndRotation()
  73.  
  74. self.replicatePosition:Fire()
  75. end
  76.  
  77. function hand:beginMove(minAlpha, maxAlpha)
  78. local distAlpha = (maxAlpha - minAlpha) / 2
  79.  
  80. self.moving = true
  81. repeat
  82. services.RunService.RenderStepped:wait()
  83. self:updateTarget()
  84. self.alpha += self.alphaMultiplier
  85. until math.abs(self.alpha - minAlpha) < 0.05 or math.abs(maxAlpha - self.alpha) < 0.05 or self.alpha ~= self.alpha
  86.  
  87. if self.alpha ~= self.alpha then
  88. self.alpha = 0
  89. end
  90.  
  91. self.moving = false
  92. end
  93.  
  94. function hand:move(keyState)
  95.  
  96. if headObject.rotationManager.active then
  97. if self.id == "l" then
  98. headObject.rotationManager.held = keyState == 1
  99.  
  100. headObject.rotationManager.selectedAxis = 0
  101. end
  102. return
  103. end
  104.  
  105. self.alphaMultiplier = keyState == 1 and 0.05 or -0.05
  106.  
  107. if not self.moving and not (keyState == 1 and self.alpha >= 1) then
  108. if keyState == 1 then
  109. headObject.recentHand = self
  110. end
  111.  
  112. self:beginMove(0, 1)
  113. end
  114. end
  115.  
  116. function hand:new(direction, realHand)
  117. local newHand = setmetatable({direction = direction, realHand = realHand}, hand)
  118. newHand.alpha = 0
  119. newHand.alphaMultiplier = 0.05
  120. newHand.id = direction and "r" or "l"
  121. newHand.targetPosition = CFrame.new()
  122. newHand.moving = false
  123. newHand.rotation = {0, 0, 0}
  124. newHand.replicatePosition = Instance.new("BindableEvent")
  125. newHand.replicatePosition.Event:connect(function()
  126. events.UserCFrameChanged:Fire(newHand.direction and Enum.UserCFrame.RightHand or Enum.UserCFrame.LeftHand, newHand.targetPosition)
  127. end)
  128.  
  129. return newHand
  130. end
  131.  
  132. local head = {}
  133. head.__index = head
  134.  
  135. function head:handleInput(input, keyState)
  136. local bind = controls[input.KeyCode] or controls[input.UserInputType]
  137.  
  138. if bind then
  139. if bind[1] == "updateTarget" and not self.rotationManager.active then
  140. self.leftHand:updateTarget()
  141. self.rightHand:updateTarget()
  142. elseif bind[2] and type(self[bind[2]]) == "table" then
  143. self[bind[2]][bind[1]](self[bind[2]], keyState, bind[3])
  144. elseif bind[3] then
  145. self[bind[1]](self, bind[3], keyState)
  146. elseif bind[2] and self.recentHand[bind[1]] then
  147. self.recentHand[bind[1]](self.recentHand, bind[2], keyState)
  148. else
  149. self[bind[1]](self, bind[2], keyState)
  150. end
  151. end
  152. end
  153.  
  154. function head:adjustOffset(_, keyState)
  155. if self.canOffset then
  156. self.handOffset += keyState * 0.04
  157. self.leftHand:updateTarget()
  158. self.rightHand:updateTarget()
  159. end
  160. end
  161.  
  162. function head:move(vec, keyState)
  163. if vec.z ~= 0 then
  164. self.realHeadset.Stick2 = math.clamp(self.realHeadset.Stick2 + (vec.z * keyState), -1, 1)
  165. else
  166. self.realHeadset.StickPosition += Vector3.new(vec.x, vec.y, 0) * keyState
  167. end
  168. end
  169.  
  170. function head:freezeCam(b)
  171. local dist = (self.realHeadset.Head.PrimaryPart.Position - cam.CFrame.p).magnitude
  172.  
  173. p.CameraMinZoomDistance = b and dist or 0.5
  174. p.CameraMaxZoomDistance = b and dist or 128
  175. end
  176.  
  177. function head:toggle(stat, keyState)
  178. if keyState ~= 1 then
  179. return
  180. end
  181.  
  182. self[stat] = not self[stat]
  183.  
  184. if stat == "canOffset" then
  185. self:freezeCam(self[stat])
  186. end
  187. end
  188.  
  189. local rotation = {}
  190. rotation.__index = rotation
  191.  
  192. function rotation:new()
  193. local newRotation = setmetatable({}, rotation)
  194.  
  195. newRotation.rotationLookup = {{}, {}, {}}
  196. newRotation.lineLookup = {{}, {}, {}}
  197. newRotation.active = false
  198. newRotation.held = false
  199. newRotation.selectedAxis = 0
  200. newRotation.angleLookup = {1, 1, 1}
  201.  
  202. for i=1,3 do
  203. local ref = {}
  204. ref[i] = function() return 0 end
  205. ref[i + 1 > 3 and ((i + 1) % 4) + 1 or i + 1] = math.cos
  206. ref[i + 2 > 3 and ((i + 2) % 4) + 1 or i + 2] = math.sin
  207.  
  208. local color = Color3.fromRGB(i == 1 and 255 or 0, i == 2 and 255 or 0, i == 3 and 255 or 0)
  209.  
  210. for j=1,13 do
  211. if j < 13 then
  212. local step = math.pi * (j/6)
  213. newRotation.rotationLookup[i][j] = Vector3.new(ref[1](step), ref[2](step), ref[3](step))
  214. end
  215.  
  216. local line = Drawing.new("Line")
  217. line.Visible = true
  218. line.Thickness = 5
  219. line.Color = color
  220. newRotation.lineLookup[i][j] = line
  221. end
  222.  
  223. local circle = Drawing.new("Circle")
  224. circle.Visible = true
  225. circle.Color = color
  226. circle.Filled = true
  227. circle.Radius = 10
  228. circle.Position = Vector2.new(-2000, -2000)
  229. newRotation.lineLookup[i][14] = circle
  230.  
  231. local text = Drawing.new("Text")
  232. text.Visible = true
  233. text.Font = Drawing.Fonts.System
  234. text.Size = 18
  235. text.Color = Color3.new():lerp(color, 0.3)
  236. text.Outline = false
  237. newRotation.lineLookup[i][15] = text
  238. end
  239.  
  240. return newRotation
  241. end
  242.  
  243. function rotation:selectAxis(keyState)
  244. self.tabActivated = keyState == 1
  245. if self.tabActivated then
  246. self.selectedAxis = math.clamp((self.selectedAxis + 1) % 4, 1, 3)
  247. end
  248. end
  249.  
  250. function rotation:toggle(keyState)
  251. if keyState == 1 and headObject.recentHand.alpha < 0.05 and not self.held then
  252. self.active = not self.active
  253.  
  254. if not self.active then
  255. self:updateAxes(0)
  256. self.selectedAxis = 0
  257. self.held = false
  258. headObject.recentHand:updateTarget()
  259. end
  260. end
  261. end
  262.  
  263. function rotation:updateAxes(visibleOverride)
  264. local basePos = headObject.recentHand.realHand.Base.Position
  265. local basePoint = cam:WorldToViewportPoint(basePos)
  266.  
  267. for i=1,3 do
  268. for j=1,12 do
  269. local vec, visible = cam:WorldToViewportPoint(basePos + (self.rotationLookup[i][j] * 10))
  270. local vec2, visible2 = cam:WorldToViewportPoint(basePos + ((self.rotationLookup[i][j + 1] or self.rotationLookup[i][1]) * 10))
  271.  
  272. local line = self.lineLookup[i][j]
  273.  
  274. line.Transparency = visibleOverride or ((visible and visible2) and 1 or 0)
  275. line.From = Vector2.new(vec.x, vec.y)
  276. line.To = Vector2.new(vec2.x, vec2.y)
  277. end
  278. local axisRotation = headObject.recentHand.rotation[i]
  279.  
  280. local axisMover = self.lineLookup[i][13]
  281. local axisCircle = self.lineLookup[i][14]
  282. local axisText = self.lineLookup[i][15]
  283.  
  284. axisMover.From = Vector2.new(basePoint.x, basePoint.y)
  285. axisMover.To = self.lineLookup[i][math.clamp(axisRotation < 1 and 12 or axisRotation, 1, 12)].From -- sorry, lazy
  286. axisMover.Transparency = visibleOverride or 1
  287.  
  288. axisCircle.Position = self.lineLookup[i][13].To
  289. axisCircle.Transparency = visibleOverride or 1
  290.  
  291.  
  292.  
  293. axisText.Position = self.lineLookup[2][3].From + axisText.TextBounds
  294. axisText.Text = string.char(87 + self.selectedAxis) .. ": " .. math.deg(axisRotation * (math.pi / 6))
  295. axisText.Transparency = visibleOverride or ((self.held or self.tabActivated) and self.selectedAxis == i and 1 or 0)
  296.  
  297. local mousePos = services.UserInputService:GetMouseLocation()
  298. if self.held and (self.lineLookup[i][14].Position - mousePos).magnitude < 10 and self.selectedAxis == 0 then
  299. self.selectedAxis = i
  300. end
  301. end
  302. end
  303.  
  304. function rotation:updateAxisMover(keyState, direction)
  305. if keyState and direction then
  306. if self.tabActivated and keyState == 1 then
  307. headObject.recentHand.rotation[self.selectedAxis] = (headObject.recentHand.rotation[self.selectedAxis] + direction) % 12
  308. headObject.recentHand:updateTarget()
  309. end
  310. return
  311. end
  312.  
  313. if self.selectedAxis ~= 0 then
  314. local mousePos = services.UserInputService:GetMouseLocation()
  315.  
  316. local circlePos = self.lineLookup[self.selectedAxis][12].From
  317.  
  318. local handPos = self.lineLookup[self.selectedAxis][13].From
  319.  
  320. local mouseDir = self.selectedAxis == 1 and headObject.recentHand.id == "r" and (Vector2.new(-mousePos.x + (handPos.x * 2), mousePos.y) - handPos).unit or (mousePos - handPos).unit
  321. -- mouse angle relating to the red circle is reflected on the right hand, so i "re-reflect" it. bad practice
  322.  
  323. local circleDir = (circlePos - handPos).unit
  324.  
  325. local rotationTheta = angleBetween(mouseDir, circleDir)
  326.  
  327. local direction = mouseDir:Cross(circleDir)
  328.  
  329. rotationTheta = direction > 0 and rotationTheta or (2 * math.pi) - rotationTheta
  330.  
  331. if rotationTheta == rotationTheta then
  332. headObject.recentHand.rotation[self.selectedAxis] = math.floor(6 * rotationTheta / math.pi)
  333.  
  334. local hand = headObject.recentHand
  335.  
  336. hand.targetPosition *= CFrame.Angles(hand.targetPosition:inverse():ToEulerAnglesXYZ()) * hand:calculateEndRotation()
  337. hand.replicatePosition:Fire()
  338. end
  339. end
  340. end
  341.  
  342. function head:new(realHeadset)
  343. local newHead = setmetatable({realHeadset = realHeadset}, head)
  344.  
  345. newHead.leftHand = hand:new(false, realHeadset.lHand)
  346. newHead.rightHand = hand:new(true, realHeadset.rHand)
  347. newHead.canOffset = false
  348. newHead.recentHand = newHead.leftHand
  349. newHead.handOffset = 0
  350. newHead.handAngle = math.pi / 4
  351.  
  352. newHead.rotationManager = rotation:new()
  353.  
  354. newHead.chatRemote = debug.getupvalue(realHeadset.ButtonPressed, 3)
  355.  
  356. cam:GetPropertyChangedSignal("CameraSubject"):connect(function()
  357. if cam.CameraSubject ~= headObject.realHeadset.Head then
  358. cam.CameraType = Enum.CameraType.Custom
  359. end
  360. end)
  361.  
  362. services.UserInputService.WindowFocused:connect(function()
  363. newHead.realHeadset.StickPosition = Vector3.new(0, 0, 0)
  364. newHead.realHeadset.Stick2 = 0
  365. end)
  366.  
  367. return newHead
  368. end
  369.  
  370. local ind, nc, nind
  371.  
  372. local realVrService = game:GetService("VRService")
  373.  
  374. local fakeVrService = setmetatable({
  375. VREnabled = true,
  376. SetTouchpadMode = function()
  377. end,
  378. RecenterUserHeadCFrame = function()
  379. end,
  380. GetUserCFrameEnabled = function(cf)
  381. return true
  382. end,
  383. GetUserCFrame = function(cf)
  384. return CFrame.new()
  385. end
  386.  
  387. }, {
  388. __index = function(t, k)
  389. local real = ind(realVrService, k)
  390. if typeof(real) == "RBXScriptSignal" then
  391. events[k] = events[k] or {
  392. Name = k,
  393. Connect = function(t, f)
  394. t.Function = f
  395.  
  396. if t.Name == "UserCFrameChanged" then
  397. headObject = head:new(debug.getupvalue(t.Function, 1))
  398.  
  399. services.UserInputService.InputBegan:connect(function(i)
  400. headObject:handleInput(i, 1)
  401. end)
  402.  
  403. services.UserInputService.InputChanged:connect(function(i)
  404. headObject:handleInput(i, i.UserInputType == Enum.UserInputType.MouseWheel and i.Position.z or 0)
  405. end)
  406.  
  407. services.UserInputService.InputEnded:connect(function(i)
  408. headObject:handleInput(i, -1)
  409. end)
  410. end
  411.  
  412. end,
  413. Fire = function(t, ...)
  414. return t.Function(...)
  415. end
  416. }
  417.  
  418. return events[k]
  419. end
  420.  
  421. return real
  422. end,
  423. __call = function(t, method, vr, ...)
  424. return t[method](...)
  425. end
  426. })
  427.  
  428. ind = hookmetamethod(game, "__index", function(...)
  429. local t, k = ...
  430.  
  431. local scr = getcallingscript()
  432.  
  433. if t == realVrService and not (scr and ind(scr, "Name") == "CameraModule") then
  434. return fakeVrService[k]
  435. end
  436.  
  437. return ind(...)
  438. end)
  439.  
  440. nc = hookmetamethod(game, "__namecall", function(...)
  441. local t = ...
  442.  
  443. if t == realVrService then
  444. local method = getnamecallmethod()
  445. return fakeVrService(method, ...)
  446. elseif t == game.GetService(game, "StarterGui") and game.IsLoaded(game) then
  447. return
  448. end
  449.  
  450. return nc(...)
  451. end)
  452.  
  453. nind = hookmetamethod(game, "__newindex", function(...)
  454. local t, k, v = ...
  455.  
  456. local scr = getcallingscript()
  457.  
  458. if t == cam and headObject then
  459. if k == "CFrame" and events.UserCFrameChanged then
  460.  
  461. events.UserCFrameChanged:Fire(Enum.UserCFrame.Head, CFrame.Angles(cam.CFrame:ToEulerAnglesXYZ()))
  462.  
  463. if headObject.rotationManager.active then
  464.  
  465. headObject.rotationManager:updateAxes()
  466.  
  467. if headObject.rotationManager.held then
  468. headObject.rotationManager:updateAxisMover()
  469. end
  470. end
  471.  
  472. if headObject.rotationManager.tabActivated and services.UserInputService:IsKeyDown(Enum.KeyCode.Left) or services.UserInputService:IsKeyDown(Enum.KeyCode.Right) then -- prevent controls from messing with camera
  473. return
  474. end
  475. elseif k == "CameraType" then
  476. nind(t, k, Enum.CameraType.Custom)
  477. nind(t, "CameraSubject", headObject.realHeadset.Head)
  478. headObject.leftHand:updateTarget()
  479. headObject.rightHand:updateTarget()
  480. end
  481. if not (scr and scr.Name == "CameraModule") and not checkcaller() then
  482. return
  483. end
  484. end
  485.  
  486. nind(t, k, v)
  487. end)
  488.  
  489. p = services.Players.LocalPlayer or (function()
  490. services.Players:GetPropertyChangedSignal("LocalPlayer"):wait() -- this doesnt return anything for some reason??
  491. return services.Players.LocalPlayer
  492. end)()
  493.  
  494. p.Chatted:connect(function(c)
  495. services.ReplicatedStorage.COM.Chat:FireServer("Chat", c)
  496. end)
  497.  
  498.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement