Advertisement
nonogamer9

bubble nvidia geforce 256 tech demo for opencomputers (version 0.5)

Aug 4th, 2024 (edited)
179
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.69 KB | Software | 1 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local event = require("event")
  4. local gpu = component.gpu
  5. local math = require("math")
  6.  
  7. if gpu.maxDepth() < 8 then
  8.     error("This script requires a Tier 3 GPU.")
  9. end
  10.  
  11. local w, h = gpu.maxResolution()
  12. gpu.setResolution(w, h)
  13.  
  14. local buffer1 = gpu.allocateBuffer(w, h)
  15. local buffer2 = gpu.allocateBuffer(w, h)
  16. local currentBuffer = buffer1
  17.  
  18. local rotationX, rotationY = 0, 0
  19.  
  20. local blob = {
  21.     x = w / 2,
  22.     y = h / 2,
  23.     radius = math.floor(math.min(w, h) / 4),
  24.     baseColor = 0x7F7F7F,
  25.     touchPoint = nil,
  26.     stretchVector = {x = 0, y = 0},
  27.     stretchFactor = 0,
  28.     recoilFactor = 0,
  29.     velocity = {x = 0, y = 0},
  30.     springConstant = 0.1,
  31.     damping = 0.8
  32. }
  33.  
  34. local background = {
  35.     stars = {},
  36.     numStars = 100
  37. }
  38.  
  39. for i = 1, background.numStars do
  40.     table.insert(background.stars, {
  41.         x = math.random(-100, 100),
  42.         y = math.random(-100, 100),
  43.         z = math.random(-100, 0),
  44.         color = math.random(0x444444, 0xFFFFFF)
  45.     })
  46. end
  47.  
  48. local function blendColors(c1, c2, factor)
  49.     local r1, g1, b1 = c1 >> 16, (c1 >> 8) & 0xFF, c1 & 0xFF
  50.     local r2, g2, b2 = c2 >> 16, (c2 >> 8) & 0xFF, c2 & 0xFF
  51.     local r = math.floor((r1 * (1 - factor) + r2 * factor) + 0.5) & 0xFF
  52.     local g = math.floor((g1 * (1 - factor) + g2 * factor) + 0.5) & 0xFF
  53.     local b = math.floor((b1 * (1 - factor) + b2 * factor) + 0.5) & 0xFF
  54.     return (r << 16) | (g << 8) | b
  55. end
  56.  
  57. local function rotate3D(x, y, z, ax, ay)
  58.     local sy, cy = math.sin(ay), math.cos(ay)
  59.     local sx, cx = math.sin(ax), math.cos(ax)
  60.     local nx = x * cy + z * sy
  61.     local ny = y * cx - (x * sy - z * cy) * sx
  62.     local nz = -y * sx + (x * sy - z * cy) * cx
  63.     return nx, ny, nz
  64. end
  65.  
  66. local function updateBlobDeformation(dt)
  67.     local recoilSpeed = 0.2
  68.     local stretchDecay = 0.9
  69.    
  70.     if blob.touchPoint then
  71.         local dx, dy = blob.touchPoint.x - blob.x, blob.touchPoint.y - blob.y
  72.         local distance = math.sqrt(dx*dx + dy*dy)
  73.         blob.stretchFactor = math.min(distance / (blob.radius * 2), 1) * 0.5
  74.         blob.stretchVector.x, blob.stretchVector.y = dx / distance, dy / distance
  75.         blob.recoilFactor = 0
  76.     else
  77.         -- Spring-like behavior
  78.         local force = {
  79.             x = -blob.springConstant * blob.stretchVector.x * blob.stretchFactor,
  80.             y = -blob.springConstant * blob.stretchVector.y * blob.stretchFactor
  81.         }
  82.         blob.velocity.x = blob.velocity.x + force.x * dt
  83.         blob.velocity.y = blob.velocity.y + force.y * dt
  84.         blob.velocity.x = blob.velocity.x * blob.damping
  85.         blob.velocity.y = blob.velocity.y * blob.damping
  86.        
  87.         blob.stretchVector.x = blob.stretchVector.x + blob.velocity.x * dt
  88.         blob.stretchVector.y = blob.stretchVector.y + blob.velocity.y * dt
  89.        
  90.         blob.stretchFactor = math.sqrt(blob.stretchVector.x^2 + blob.stretchVector.y^2)
  91.         if blob.stretchFactor > 0 then
  92.             blob.stretchVector.x = blob.stretchVector.x / blob.stretchFactor
  93.             blob.stretchVector.y = blob.stretchVector.y / blob.stretchFactor
  94.         end
  95.        
  96.         blob.stretchFactor = blob.stretchFactor * stretchDecay
  97.         if blob.stretchFactor < 0.01 then
  98.             blob.stretchFactor = 0
  99.             blob.stretchVector.x, blob.stretchVector.y = 0, 0
  100.             blob.velocity.x, blob.velocity.y = 0, 0
  101.         end
  102.     end
  103. end
  104.  
  105. local function drawBlob()
  106.     local time = computer.uptime()
  107.     local wobble = math.sin(time * 2) * 1
  108.     local aspectRatio = w / h
  109.    
  110.     for screenY = 1, h do
  111.         for screenX = 1, w do
  112.             local dx = (screenX - blob.x) / aspectRatio
  113.             local dy = screenY - blob.y
  114.             local distanceSquared = dx*dx + dy*dy
  115.            
  116.             if distanceSquared <= blob.radius * blob.radius then
  117.                 local z = math.sqrt(blob.radius*blob.radius - distanceSquared)
  118.                
  119.                 local nx = dx / blob.radius
  120.                 local ny = dy / blob.radius
  121.                 local nz = z / blob.radius
  122.                
  123.                 local stretchX = blob.stretchVector.x * blob.stretchFactor * nx
  124.                 local stretchY = blob.stretchVector.y * blob.stretchFactor * ny
  125.                
  126.                 local px = nx + stretchX
  127.                 local py = ny + stretchY
  128.                 local pz = nz * (1 - blob.stretchFactor * 0.5)
  129.                
  130.                 local rx, ry, rz = rotate3D(px, py, pz, rotationX, rotationY)
  131.                
  132.                 -- Lighting
  133.                 local lightDir = {x = 0.5, y = -0.5, z = 0.7}
  134.                 local lightIntensity = math.max(0, rx * lightDir.x + ry * lightDir.y + rz * lightDir.z)
  135.                
  136.                 -- Reflection
  137.                 local viewDir = {x = -rx, y = -ry, z = -rz}
  138.                 local reflectDir = {
  139.                     x = viewDir.x - 2 * (viewDir.x * rx + viewDir.y * ry + viewDir.z * rz) * rx,
  140.                     y = viewDir.y - 2 * (viewDir.x * rx + viewDir.y * ry + viewDir.z * rz) * ry,
  141.                     z = viewDir.z - 2 * (viewDir.x * rx + viewDir.y * ry + viewDir.z * rz) * rz
  142.                 }
  143.                 local reflectionIntensity = math.max(0, reflectDir.x * lightDir.x + reflectDir.y * lightDir.y + reflectDir.z * lightDir.z) ^ 8
  144.                
  145.                 local lightFactor = lightIntensity * 0.7 + 0.3
  146.                 local reflectionFactor = reflectionIntensity * 0.3
  147.                
  148.                 local color = blendColors(blob.baseColor, 0xFFFFFF, lightFactor)
  149.                 color = blendColors(color, 0xFFFFFF, reflectionFactor)
  150.                
  151.                 local finalY = screenY + wobble * (1 - math.sqrt(distanceSquared) / blob.radius)
  152.                
  153.                 if finalY > 0 and finalY <= h then
  154.                     gpu.setBackground(color)
  155.                     gpu.set(screenX, math.floor(finalY), " ")
  156.                 end
  157.             end
  158.         end
  159.     end
  160. end
  161.  
  162. local function drawBackground()
  163.     for _, star in ipairs(background.stars) do
  164.         local x, y, z = rotate3D(star.x, star.y, star.z, rotationX, rotationY)
  165.         local scale = 200 / (z + 200)
  166.         local screenX, screenY = math.floor(w/2 + x * scale), math.floor(h/2 + y * scale)
  167.        
  168.         if screenX > 0 and screenX <= w and screenY > 0 and screenY <= h then
  169.             local brightness = math.floor(255 * scale) & 0xFF
  170.             local color = blendColors(0x000000, star.color, brightness / 255)
  171.             gpu.setBackground(color)
  172.             gpu.set(screenX, screenY, " ")
  173.         end
  174.     end
  175. end
  176.  
  177. local function render()
  178.     gpu.setActiveBuffer(currentBuffer)
  179.     gpu.setBackground(0x000000)
  180.     gpu.fill(1, 1, w, h, " ")
  181.     drawBackground()
  182.     drawBlob()
  183.     gpu.setActiveBuffer(0)
  184.     gpu.bitblt(0, 1, 1, w, h, currentBuffer)
  185.     currentBuffer = (currentBuffer == buffer1) and buffer2 or buffer1
  186. end
  187.  
  188. local function handleTouch(_, _, x, y, button)
  189.     local dx, dy = x - blob.x, y - blob.y
  190.     if button == 0 then  -- Left mouse button for rotation
  191.         rotationY = rotationY + (x - w/2) * 0.01
  192.         rotationX = rotationX + (y - h/2) * 0.01
  193.     elseif button == 1 and dx*dx + dy*dy <= blob.radius*blob.radius then  -- Right mouse button for deformation
  194.         blob.touchPoint = {x = x, y = y}
  195.         blob.velocity.x, blob.velocity.y = 0, 0
  196.     end
  197. end
  198.  
  199. local function handleDrag(_, _, x, y, button)
  200.     if button == 0 then  -- Left mouse button for rotation
  201.         rotationY = rotationY + (x - w/2) * 0.01
  202.         rotationX = rotationX + (y - h/2) * 0.01
  203.     elseif button == 1 and blob.touchPoint then  -- Right mouse button for deformation
  204.         blob.touchPoint.x, blob.touchPoint.y = x, y
  205.     end
  206. end
  207.  
  208. local function handleRelease(_, _, _, _, button)
  209.     if button == 1 then
  210.         blob.touchPoint = nil
  211.     end
  212. end
  213.  
  214. local running = true
  215. local lastRender = computer.uptime()
  216. local targetFPS = 30
  217.  
  218. event.listen("touch", handleTouch)
  219. event.listen("drag", handleDrag)
  220. event.listen("drop", handleRelease)
  221.  
  222. while running do
  223.     local currentTime = computer.uptime()
  224.     local deltaTime = currentTime - lastRender
  225.    
  226.     if deltaTime >= 1 / targetFPS then
  227.         updateBlobDeformation(deltaTime)
  228.         render()
  229.         lastRender = currentTime
  230.        
  231.         local e = {event.pull(0)}
  232.         if e[1] == "interrupted" then
  233.             running = false
  234.         end
  235.     else
  236.         os.sleep(0.001)
  237.     end
  238. end
  239.  
  240. event.ignore("touch", handleTouch)
  241. event.ignore("drag", handleDrag)
  242. event.ignore("drop", handleRelease)
  243. gpu.setBackground(0x000000)
  244. gpu.setForeground(0xFFFFFF)
  245. gpu.fill(1, 1, w, h, " ")
  246. gpu.freeBuffer(buffer1)
  247. gpu.freeBuffer(buffer2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement