Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.23 KB | None | 0 0
  1.  
  2. --# Main
  3. --# Main
  4. -- 3D Grad
  5.  
  6.  
  7. function setup()
  8.    
  9.     print("Grad24")
  10.     --displayMode(FULLSCREEN)
  11.    
  12.     local topBaseColrInit = getRandomHSBColr(1, 360, 0, 100, 50, 100)
  13.     local bottomBaseColrInit = getRandomHSBColr(1, 360, 0, 100, 50, 100)
  14.    
  15.    
  16.     colrGenerator = ColrGenerator()
  17.    
  18.     topBClrParamInitTime = 0
  19.     btmBClrParamInitTime = 0
  20.     topBClrParamInit(topBaseColrInit)
  21.     btmBClrParamInit(bottomBaseColrInit)
  22.    
  23.     --[[print("topBaseColrInit:")
  24.     print(topBaseColrInit)
  25.     print("bottomBaseColrInit:")
  26.     print(bottomBaseColrInit)]]
  27.     updateColrParameters(topBaseColrInit, bottomBaseColrInit)
  28.    
  29.     backgrnd = gradBackground()
  30.     -- or could set img
  31.    
  32.     updateRotationParameters()
  33.     t = 0
  34.     cubeSize = 25
  35.     shapeWidth = 29
  36.    
  37.     -- Cube mesh data
  38.     cubeMeshModel = CubeMeshModel()
  39.     cubeMeshModel:setupCubeMesh()
  40.    
  41.     -- Block types and structures
  42.     blockTypes = BlockTypes()
  43.     blockStructures = BlockStructures()
  44.    
  45.     -- Scene data
  46.     sceneDataModel = SceneDataModel()
  47.     data = sceneDataModel:setupShapeData()
  48.    
  49.     sceneRenderer = SceneRenderer()
  50.    
  51.     -- setImg()
  52.    
  53.     updateCameraParameters()
  54.    
  55.    
  56. end
  57.  
  58. -- This function gets called once every frame
  59. function draw()
  60.    
  61.     background(255,0)
  62.    
  63.     updateTopBtmBClrParamDur()
  64.    
  65.     backgrnd:draw()
  66.    
  67.     translate(WIDTH / 2, -HEIGHT/7, -700)
  68.    
  69.     rotate(rotationIdx*15, 0, 1, 0)
  70.    
  71.     ortho(0, WIDTH, 0, HEIGHT, -100, 2000)
  72.    
  73.     camera(cameraX, cameraHeight, cameraZ, 0,0,0, 0,1,0)
  74.    
  75.     sceneRenderer:renderSceneData(data)
  76.    
  77.     setContext()
  78.     resetMatrix()
  79.     --t = t + 1
  80. end
  81.  
  82. function setImg()
  83.     --Draw into an image
  84.    
  85.     --Do shape
  86.     shapeImg = image(shapeWidth*cubeSize,shapeWidth*cubeSize)
  87.    
  88.     setContext(shapeImg)
  89.     --Do drawing here
  90.    
  91.     setContext()
  92.    
  93. end
  94.  
  95. --# SceneDataModel
  96. SceneDataModel = class()
  97.  
  98. function SceneDataModel:init()
  99.     -- you can accept and set parameters here
  100.      
  101. end
  102.  
  103. function SceneDataModel:draw()
  104.     -- Codea does not automatically call this method
  105. end
  106.  
  107. function SceneDataModel:touched(touch)
  108.     -- Codea does not automatically call this method
  109. end
  110.  
  111. function SceneDataModel:setupShapeData()
  112.    
  113.     local retShapeData = {}
  114.    
  115.     -- Generate scene of empty air blocks
  116.     for x = -1 * shapeWidth, shapeWidth do
  117.         retShapeData[x] = {}
  118.         for y = -1 * shapeWidth, shapeWidth do
  119.             retShapeData[x][y] = {}
  120.             for z = -1 * shapeWidth, shapeWidth do
  121.                
  122.                 local retAirCube = Block()
  123.                
  124.                 retAirCube.doesExist = false
  125.                 retAirCube.colr = color(255,0)
  126.                 retAirCube.x = x
  127.                 retAirCube.y = y
  128.                 retAirCube.z = z
  129.                
  130.                 retShapeData[x][y][z] = retAirCube
  131.                
  132.                
  133.             end
  134.         end
  135.     end
  136.    
  137.     for level = 1,3 do
  138.        
  139.         local planeXCoor1, planeXCoor2 = 0 + (level-1), 7 - (level-1)
  140.         local planeYCoor = (level-1) * 5
  141.         local planeZCoor1, planeZCoor2 = 0 + (level-1), 7 - (level-1)
  142.         local planeBlockType = blockTypes:blockTypeColrB()
  143.        
  144.         retShapeData = blockStructures:spwnPlaneBtwnPnts(planeXCoor1, planeYCoor, planeZCoor1, planeXCoor2, planeYCoor, planeZCoor2, planeBlockType, retShapeData)
  145.        
  146.         for column = 1,4 do
  147.            
  148.             local colBaseYCoor = (level-1) * 5 + 1
  149.             local colBaseXCoor1, colBaseXCoor2 = 0 + (level), 7 - (level)
  150.             local colBaseZCoor1, colBaseZCoor2 = 0 + (level), 7 - (level)
  151.            
  152.             --Default
  153.             local colBaseXCoor = colBaseXCoor1
  154.             local colBaseZCoor = colBaseZCoor1
  155.            
  156.             local colBaseXZCoors = {}
  157.            
  158.             for colBaseXIdx = colBaseXCoor1, colBaseXCoor2, colBaseXCoor2 - colBaseXCoor1 do
  159.                
  160.                 for colBaseZIdx = colBaseZCoor1, colBaseZCoor2, colBaseZCoor2 - colBaseZCoor1 do
  161.                    
  162.                     local XZCoor = {x= colBaseXIdx, z= colBaseZIdx}
  163.                     table.insert(colBaseXZCoors, XZCoor)
  164.                 end
  165.                
  166.             end
  167.            
  168.             colBaseXCoor = colBaseXZCoors[column].x
  169.             colBaseZCoor = colBaseXZCoors[column].z
  170.            
  171.             local colHeight = 4
  172.             local colBlockType = blockTypes:blockTypeColrA()
  173.             --print(colBase)
  174.            
  175.             retShapeData = blockStructures:spwnColumnFrBasePnt(colBaseXCoor, colBaseYCoor, colBaseZCoor, colHeight, colBlockType, retShapeData)
  176.            
  177.            
  178.         end
  179.        
  180.     end
  181.    
  182.     return retShapeData
  183. end
  184. --# Block
  185. Block = class()
  186.  
  187. function Block:init()
  188.     -- you can accept and set parameters here
  189.     self.doesExist = false
  190.     self.colr = ShapeColr
  191.     self.type = ""
  192.     self.x = 0
  193.     self.y = 0
  194.     self.z = 0
  195.    
  196. end
  197.  
  198. function Block:draw()
  199.     -- Codea does not automatically call this method
  200. end
  201.  
  202. function Block:touched(touch)
  203.     -- Codea does not automatically call this method
  204. end
  205.  
  206. --# BlockTypes
  207. BlockTypes = class()
  208.  
  209. function BlockTypes:init()
  210.     -- you can accept and set parameters here
  211.    
  212.     --TODO: BlockTypeTable
  213.     --self.blockTypeTable = self:updateBlockTypeTable()
  214.    
  215. end
  216.  
  217. function BlockTypes:draw()
  218.     -- Codea does not automatically call this method
  219. end
  220.  
  221. function BlockTypes:touched(touch)
  222.     -- Codea does not automatically call this method
  223. end
  224.  
  225. function BlockTypes:updateBlockType(blockArg)
  226.    
  227.     local retBlock = blockArg
  228.     local typeArg = blockArg.type
  229.    
  230.     if typeArg == "blockColrA" then
  231.        
  232.         retBlock.colr = blockColrA
  233.        
  234.     elseif typeArg == "blockColrB" then
  235.        
  236.         retBlock.colr = blockColrB
  237.        
  238.     end
  239.    
  240.     return retBlock
  241.    
  242. end
  243.  
  244. function BlockTypes:updateBlockTypeTable()
  245.     --[[
  246.     local retBlockTypeTable = {
  247.     "blockColrA" = {Block().colr = blockColrA}}
  248.    
  249.     return retBlockTypeTable
  250.     ]]
  251. end
  252.  
  253. function BlockTypes:blockTypeColrA()
  254.     return "blockColrA"
  255. end
  256.  
  257. function BlockTypes:blockTypeColrB()
  258.     return "blockColrB"
  259. end
  260.  
  261. --# BlockStructures
  262. BlockStructures = class()
  263. --currently not in use, just for reference
  264.  
  265. function BlockStructures:structures()
  266.    
  267.     -- Make a plane
  268.     for x = 0,5 do
  269.         for z = 0,5 do
  270.            
  271.             local cube = retShapeData[x][0][z]
  272.             cube.doesExist = true
  273.             cube.type = blockTypes:blockTypeColrB()
  274.             cube = blockTypes:updateBlockType(cube)
  275.            
  276.             retShapeData[x][0][z] = cube
  277.            
  278.         end
  279.     end
  280.    
  281.     --[[ Make a diagonal streak at specific coordinates
  282.     local setCoors = {
  283.         {x=3, y=1, z=0},
  284.         {x=2, y=1, z=1},
  285.         {x=1, y=1, z=2},
  286.         {x=0, y=1, z=3}
  287.     }
  288.    
  289.     for coorIdx,coor in ipairs(setCoors) do
  290.        
  291.         local setCoorCube = retShapeData[coor.x][coor.y][coor.z]
  292.         setCoorCube.doesExist = true
  293.         setCoorCube.type = blockTypes:blockTypeColrB()
  294.         setCoorCube = blockTypes:updateBlockType(setCoorCube)
  295.            
  296.         retShapeData[coor.x][coor.y][coor.z] = setCoorCube
  297.        
  298.     end ]]
  299.    
  300.     -- Try spawning for the impossible triangle :)
  301.     local impossibleTriCoors = {
  302.        
  303.         {x=3, y=1, z=3},
  304.         {x=3, y=2, z=3},
  305.         {x=3, y=3, z=3},
  306.         {x=3, y=4, z=3},
  307.         {x=3, y=5, z=3},
  308.         {x=3, y=6, z=3},
  309.    
  310.         {x=3, y=1, z=2},
  311.         {x=3, y=1, z=1},
  312.         {x=3, y=1, z=0},
  313.         {x=3, y=1, z=-1},
  314.         {x=3, y=1, z=-2},
  315.    
  316.         {x=4, y=6, z=3},
  317.         {x=5, y=6, z=3},
  318.         {x=6, y=6, z=3},
  319.         {x=7, y=6, z=3}
  320.     }
  321.    
  322.     for coorIdx,coor in ipairs(impossibleTriCoors) do
  323.        
  324.         local setCoorCube = retShapeData[coor.x][coor.y][coor.z]
  325.         setCoorCube.doesExist = true
  326.         setCoorCube.type = blockTypes:blockTypeColrA()
  327.         setCoorCube = blockTypes:updateBlockType(setCoorCube)
  328.            
  329.         retShapeData[coor.x][coor.y][coor.z] = setCoorCube
  330.        
  331.     end
  332.    
  333.    
  334. end
  335.  
  336. function BlockStructures:spwnCubeAtPnt(xCoor, yCoor, zCoor, cubeTypeArg, dataArg)
  337.    
  338.     local dataRet = dataArg
  339.    
  340.     local cube = dataRet[xCoor][yCoor][zCoor]
  341.     cube.doesExist = true
  342.     cube.type = cubeTypeArg
  343.     cube = blockTypes:updateBlockType(cube)
  344.    
  345.     dataRet[xCoor][yCoor][zCoor] = cube
  346.    
  347.     return dataRet
  348.    
  349. end
  350.  
  351. function BlockStructures:spwnColumnFrBasePnt(baseXCoor, baseYCoor, baseZCoor, height, cubeTypeArg, dataArg)
  352.    
  353.     local dataRet = dataArg
  354.    
  355.     -- Make a column
  356.     for yIdx = baseYCoor, baseYCoor + height do
  357.        
  358.         dataRet = self:spwnCubeAtPnt(baseXCoor, yIdx, baseZCoor, cubeTypeArg, dataRet)
  359.        
  360.     end
  361.    
  362.     return dataRet
  363.    
  364. end
  365.  
  366. function BlockStructures:spwnPlaneBtwnPnts(xCoor1, yCoor1, zCoor1, xCoor2, yCoor2, zCoor2, cubeTypeArg, dataArg)
  367.    
  368.     local dataRet = dataArg
  369.    
  370.     --Checks which coor is constant
  371.     --Note: one of coors must be constant for it to be a plane parallel to one of the axes
  372.    
  373.     if yCoor1 == yCoor2 then
  374.        
  375.         local yConstant = math.random(yCoor1, yCoor2)
  376.        
  377.         for xIdx = xCoor1, xCoor2 do
  378.        
  379.             for zIdx = zCoor1, zCoor2 do
  380.                
  381.                 dataRet = self:spwnCubeAtPnt(xIdx, yConstant, zIdx, cubeTypeArg, dataRet)
  382.                
  383.             end
  384.        
  385.         end
  386.        
  387.     end
  388.    
  389.     return dataRet
  390.    
  391. end
  392.  
  393. --# CubeMeshModel
  394. CubeMeshModel = class()
  395.  
  396. function CubeMeshModel:init()
  397.     -- you can accept and set parameters here
  398. end
  399.  
  400. function CubeMeshModel:draw()
  401.     -- Codea does not automatically call this method
  402. end
  403.  
  404. function CubeMeshModel:touched(touch)
  405.     -- Codea does not automatically call this method
  406. end
  407.  
  408.  
  409. function CubeMeshModel:setupCubeMesh()
  410.    
  411.     cubeCrnrs = self:setupCubeCrnrs(cubeSize)
  412.    
  413.     cubeFacesVerts = self:setupCubeFaceVerts(cubeCrnrs)
  414.    
  415.     cubeMeshVerts = self:triangulateSixFaces(cubeFacesVerts)
  416.    
  417.     cubeMesh = mesh()
  418.     cubeMesh.vertices = cubeMeshVerts
  419.    
  420. end
  421.  
  422. function CubeMeshModel:setupCubeCrnrs(size)
  423.    
  424.     local cbCrnrs = {}
  425.    
  426.     for z = 0,1 do
  427.         for y = 0,1 do
  428.             for x = 0,1 do
  429.                 table.insert(cbCrnrs, size * vec3(x,y,z))
  430.             end
  431.         end
  432.     end
  433.    
  434.     return cbCrnrs
  435.     --Does this: {vec3(0, 0, 0), vec3(50, 0, 0), vec3(0, 50, 0), vec3(50, 50, 0), vec3(0, 0, 50), vec3(50, 0, 50), vec3(0, 50, 50), vec3(50, 50, 50)}
  436.    
  437. end
  438.  
  439.  
  440. function CubeMeshModel:setupCubeFaceVerts(cubeCrnrArg)
  441.    
  442.     --Set up vertices for 6 faces of a cube
  443.    
  444.     local facesVerts = {}
  445.    
  446.     --Face1,2,3,4 & Face5,6,7,8 print cubeCrnrArgIdx1 to see
  447.     for oppSide = 0,1 do
  448.        
  449.         for crnrIdx = 1,4 do
  450.            
  451.             local faceVertsIdx1 = crnrIdx + 4*oppSide*3 -- 3 is number of pairs of opposite faces, 4 is for number of vertices
  452.             local cubeCrnrArgIdx1 = crnrIdx + (oppSide*4) -- 4 is specific to above (1,2,3,4 & 5,6,7,8)
  453.        
  454.             facesVerts[faceVertsIdx1] = cubeCrnrArg[cubeCrnrArgIdx1]
  455.            
  456.         end
  457.        
  458.     end
  459.    
  460.     --Face1,3,5,7 & Face2,4,6,8 print cubeCrnrArgIdx2 to see
  461.     for oppSide = 0,1 do
  462.         for crnrIdx = 1,4 do
  463.             faceVertsIdx2 = crnrIdx + 4*(oppSide*3 + 1)
  464.             cubeCrnrArgIdx2 = (crnrIdx*2) - 1 + (oppSide)
  465.             facesVerts[faceVertsIdx2] = cubeCrnrArg[cubeCrnrArgIdx2]
  466.         end
  467.     end
  468.    
  469.     --Face1,2,5,6 & Face3,4,7,8,print cubeCrnrArgIdx3 to see
  470.     faceVertsIdx3 = 0
  471.  
  472.     for oppSide = 0,1 do
  473.         faceVertsIdx3 = faceVertsIdx3 + 4*2 --second time, 8 ahead?
  474.  
  475.         for set = 0,1 do
  476.             for partner = 1,2 do
  477.                 cubeCrnrArgIdx3 = partner + (set*4) + (oppSide*2)
  478.                 faceVertsIdx3 = faceVertsIdx3 + 1
  479.                 facesVerts[faceVertsIdx3] = cubeCrnrArg[cubeCrnrArgIdx3]
  480.             end
  481.         end
  482.     end
  483.    
  484.     return facesVerts
  485.    
  486.      --Does this:
  487.     --[[
  488.     faceVerts = {cubeCrnrArg[1], cc[2], cc[3], cc[4],
  489.     cc[1], cc[3], cc[5], cc[7],
  490.     cc[1], cc[2], cc[5], cc[6],
  491.     cc[5], cc[6], cc[7], cc[8],
  492.     cc[2], cc[4], cc[6], cc[8],
  493.     cc[3], cc[4], cc[7], cc[8]}
  494.     ]]
  495.    
  496. end
  497.  
  498.  
  499. function CubeMeshModel:triangulateSingleFace(faceVertices)
  500.    
  501.     --Splits set of quadrilateral vertices into two triangle halfs
  502.     local triVertices = {}
  503.    
  504.     for triangleHalf = 0,1 do  
  505.         for triVertex = 1,3 do
  506.             table.insert(triVertices, faceVertices[triVertex + triangleHalf])
  507.         end
  508.     end
  509.     return triVertices
  510.    
  511.     --does this: {cc[1], cc[2], cc[3], cc[4]} –> {cc[1], cc[2], cc[3], cc[2], cc[3], cc[4]}
  512. end
  513.  
  514.  
  515. function CubeMeshModel:triangulateSixFaces(facesVerts)
  516.    
  517.     --Take faceVerts, separate into faces and triangulate each
  518.     local retCubeMeshVerts = {}
  519.    
  520.     for face = 1,6 do  
  521.         local faceVertices = {}
  522.         for polygonCorner = 1,4 do
  523.             table.insert(faceVertices, facesVerts[polygonCorner + (face-1) * 4 ])
  524.         end
  525.         triFaceVertices = {}
  526.         triFaceVertices = self:triangulateSingleFace(faceVertices)
  527.         for i, vertex in ipairs(triFaceVertices) do
  528.             table.insert(retCubeMeshVerts, vertex)
  529.         end
  530.     end
  531.    
  532.     return retCubeMeshVerts
  533.    
  534.     --does this: {cc[1], cc[2], cc[3], cc[4]} —> {cc[1], cc[2], cc[3], cc[2], cc[3], cc[4]}
  535.    
  536. end
  537.  
  538.  
  539. --# SceneRenderer
  540. SceneRenderer = class()
  541.  
  542. function SceneRenderer:init(x)
  543.     -- you can accept and set parameters here
  544. end
  545.  
  546. function SceneRenderer:draw()
  547.     -- Codea does not automatically call this method
  548. end
  549.  
  550. function SceneRenderer:touched(touch)
  551.     -- Codea does not automatically call this method
  552. end
  553.  
  554. function SceneRenderer:renderSceneData(sceneDataArg)
  555.    
  556.     for x = -1 * shapeWidth, shapeWidth do
  557.         for y = -1 * shapeWidth, shapeWidth do
  558.             for z = -1 * shapeWidth, shapeWidth do
  559.                
  560.                 local cubeAtPoint = data[x][y][z]
  561.                 local cubeAtPoint = blockTypes:updateBlockType(cubeAtPoint)
  562.                
  563.                 if cubeAtPoint.doesExist == true then
  564.                    
  565.                     translate(x * cubeSize, y * cubeSize, z * cubeSize)
  566.                    
  567.                     --shift m.colors depending on x, y, z
  568.                     --set m.color(self.colr:mix(black, distance from corner))
  569.                    
  570.                     cubeMesh.colors = self:cubeColor(cubeAtPoint.colr, x, y, z)
  571.                    
  572.                     cubeMesh:draw()
  573.                    
  574.                     translate(x * -1 * cubeSize, y * -1 * cubeSize, z * -1 * cubeSize)
  575.                 end
  576.             end
  577.         end
  578.     end
  579.    
  580. end
  581.  
  582. function SceneRenderer:cubeColor(col, vecX, vecY, vecZ)
  583.     local ret = {}
  584.    
  585.     vecRefPoint = vec3(-shapeWidth,-shapeWidth,-shapeWidth)
  586.    
  587.    
  588.     for vertIdx, vertVec in ipairs(cubeMeshVerts) do
  589.        
  590.         --Base shading for perspective
  591.         local baseShadeColr = col:mix(color(0, 0, 0, col.a), 1 - math.floor(((vertIdx - 1) % 18) / 6) * 0.15)
  592.        
  593.    
  594.         --Shading for gradient
  595.         vecToPoint = vec3((cubeSize*vecX)+vertVec.x,(cubeSize*vecY)+vertVec.y,(cubeSize*vecZ)+vertVec.z)
  596.         distToRefPoint = vecRefPoint:dist(vecToPoint)
  597.         maxDistAcross = vec3(-shapeWidth,-shapeWidth,-shapeWidth):dist(vec3(shapeWidth,shapeWidth,shapeWidth))
  598.        
  599.        
  600.         baseShadeColr = baseShadeColr:mix(color(0), distToRefPoint/(maxDistAcross)) --NOTE: just tried math.min( 1) and doesn't shade
  601.         --print(distToRefPoint/maxDistAcross) --takes really long
  602.        
  603.         table.insert(ret, baseShadeColr)
  604.     end
  605.    
  606.     return ret
  607. end
  608.  
  609. --# gradBackground
  610. gradBackground = class()
  611.  
  612. function gradBackground:init()
  613.    
  614.     print("Smooth Background05 two tone")
  615.    
  616.     --[[
  617.     parameter.color("topOuterColr", topBaseColr)
  618.     parameter.color("topInnerColr", self.topInnerInitColr)
  619.    
  620.     parameter.color("bottomOuterColr", bottomBaseColr)
  621.     parameter.color("bottomInnerColr", self.bottomInnerInitColr)
  622.     ]]
  623.     --parameter.action("Switch Colrs", self:switchColrs())
  624.    
  625.    
  626. end
  627.  
  628.  
  629. function gradBackground:draw()
  630.    
  631.     gradBackgrndMesh = gradBackground:generateMesh()
  632.     gradBackgrndMesh:draw()
  633.    
  634. end
  635.  
  636. function gradBackground:drawMeshInImg()
  637.    
  638.     local bgImg = image(WIDTH, HEIGHT)
  639.    
  640.     return bgImg
  641.    
  642. end
  643.  
  644. function gradBackground:generateMesh()
  645.    
  646.     -- Note: these coors are unused at the moment
  647.     topLeftCoor = vec2(0,HEIGHT)
  648.     bottomLeftCoor = vec2(0,0)
  649.     topRightCoor = vec2(WIDTH,HEIGHT)
  650.     bottomRightCoor = vec2(WIDTH,0)
  651.    
  652.     local backgrndMesh = mesh()
  653.     local backgrndMeshVerts = {}
  654.     local backgrndMeshColrs = {}
  655.    
  656.     for xIdx = 0, 1 do
  657.        
  658.         --print("xIdx = "..xIdx)
  659.        
  660.         for triHalfIdx = 0, 1 do
  661.            
  662.             --print("triHalfIdx = "..triHalfIdx)
  663.            
  664.             diagTopVert = vec2()
  665.             diagTopVert.x = WIDTH/2
  666.             diagTopVert.y = HEIGHT
  667.             diagTopVertColr = topInnerColr
  668.             --print("diagTopVertX = "..diagTopVert.x.." diagTopVertY = "..diagTopVert.y)
  669.            
  670.             diagBtmVert = vec2()
  671.             diagBtmVert.x = xIdx * WIDTH
  672.             diagBtmVert.y = 0
  673.             diagBtmVertColr = bottomOuterColr
  674.             --print("diagBtmVertX = "..diagBtmVert.x.." diagBtmVertY = "..diagBtmVert.y)
  675.            
  676.             cornerVert = vec2()
  677.             cornerVertXSign = xIdx*2 - 1
  678.             cornerVert.x = WIDTH/2 + (triHalfIdx * WIDTH/2) * cornerVertXSign
  679.             cornerVert.y = triHalfIdx * HEIGHT
  680.             --print("cornerVertX = "..cornerVert.x.." cornerVertY = "..cornerVert.y)
  681.            
  682.             baseColrList = {bottomInnerColr, topOuterColr}
  683.             cornerVertColr = baseColrList[triHalfIdx + 1]
  684.            
  685.             --Insert set of three verts as a triangle
  686.             table.insert(backgrndMeshVerts, diagTopVert)
  687.             table.insert(backgrndMeshVerts, diagBtmVert)
  688.             table.insert(backgrndMeshVerts, cornerVert)
  689.            
  690.             table.insert(backgrndMeshColrs, diagTopVertColr)
  691.             table.insert(backgrndMeshColrs, diagBtmVertColr)
  692.             table.insert(backgrndMeshColrs, cornerVertColr)
  693.            
  694.         end
  695.        
  696.     end
  697.    
  698.     backgrndMesh.vertices = backgrndMeshVerts
  699.    
  700.     backgrndMesh.colors = backgrndMeshColrs
  701.    
  702.     return backgrndMesh
  703.    
  704. end
  705. --# ColrGenerator
  706. ColrGenerator = class()
  707.  
  708. function ColrGenerator:init()
  709.    
  710.     randomMixAmount = math.random(75,80)/100
  711.     -- mixedTopAndBtmColr = topBaseColr:mix(bottomBaseColr, 0.5)
  712.    
  713.     print("Use the parameters to choose colo(u)rs.") --thanks to Inty :)
  714.    
  715. end
  716.  
  717. function ColrGenerator:draw()
  718.    
  719. end
  720.  
  721. function ColrGenerator:touched(touch)
  722.     -- Codea does not automatically call this method
  723. end
  724.  
  725. function HSB(hue, sat, bri)
  726.     local h = hue % 360
  727.     local s = math.min(math.max(sat, 0), 100)
  728.     local b = math.min(math.max(bri, 0), 100)
  729.     local colr = color(0, 0, 0)
  730.     if h < 60 then
  731.         colr = color(255, h * (17 / 4), 0)
  732.     elseif h < 120 then
  733.         colr = color((120 - h) * (17 / 4), 255, 0)
  734.     elseif h < 180 then
  735.         colr = color(0, 255, (h - 120) * (17 / 4))
  736.     elseif h < 240 then
  737.         colr = color(0, (240 - h) * (17 / 4), 255)
  738.     elseif h < 300 then
  739.         colr = color((h - 240) * (17 / 4), 0, 255)
  740.     else
  741.         colr = color(255, 0, (360 - h) * (17 / 4))
  742.     end
  743.     colr = colr:mix(color(255, 255, 255), s / 100)
  744.     colr = colr:mix(color(0, 0, 0), b / 100)
  745.     return colr.r, colr.g, colr.b
  746. end
  747.  
  748.  
  749. function getRandomRGBColr()
  750.    
  751.     local randomRGBColr = color(math.random(0,255), math.random(0,255), math.random(0,255))
  752.     return randomRGBColr
  753.    
  754. end
  755.  
  756. function getRandomHSBColr(hueMin, hueMax, satMin, satMax, briMin, briMax)
  757.    
  758.     local randomHSBColr = color(HSB(math.random(hueMin, hueMax), math.random(satMin, satMax), math.random(briMin, briMax)))
  759.     return randomHSBColr
  760.    
  761. end
  762.  
  763. function switchColrs()
  764.    
  765.     local currentTopColr = topOuterColr
  766.     local currentBottomColr = bottomOuterColr
  767.    
  768.     topOuterColr = currentBottomColr
  769.     bottomOuterColr = currentTopColr
  770.    
  771. end
  772.  
  773. function ColrGenerator:updateColrs(topBClrArg, btmBClrArg)
  774.  
  775.     topOuterColr = topBClrArg
  776.     bottomOuterColr = btmBClrArg
  777.    
  778.     topInnerColrCalc = topBClrArg:mix(btmBClrArg, randomMixAmount)
  779.     bottomInnerColrCalc = btmBClrArg:mix(topBClrArg, randomMixAmount)
  780.    
  781.     adjustedTopBaseColr = self:adjustBriForColr(topBClrArg)
  782.     adjustedBottomBaseColr = self:adjustBriForColr(btmBClrArg)
  783.    
  784. end
  785.  
  786. function ColrGenerator:mixDarkerColr(colrArg)
  787.    
  788.     local retDarkerColr = colrArg:mix(color(0), math.random(90,100)/100)
  789.     return retDarkerColr
  790.    
  791. end
  792.  
  793. function ColrGenerator:mixLighterColr(colrArg)
  794.    
  795.     local retLighterColr = colrArg:mix(color(255), math.random(90,100)/100)
  796.     return retLighterColr
  797.    
  798. end
  799.  
  800. function ColrGenerator:getBriFrRGBColr(colrArg)
  801.    
  802.     local retBri = 0
  803.     local redArg = colrArg.r
  804.     local greenArg = colrArg.g/255
  805.     local blueArg = colrArg.b/255
  806.    
  807.     retBri = math.max(redArg, greenArg, blueArg)/255 * 100
  808.    
  809.     return retBri
  810. end
  811.  
  812. function ColrGenerator:adjustBriForColr(colrArg)
  813.    
  814.     local retAdjustedColr = colrArg
  815.    
  816.     local briArg = self:getBriFrRGBColr(colrArg)
  817.    
  818.     if briArg >= 75 then
  819.        
  820.         retAdjustedColr = self:mixDarkerColr(colrArg)
  821.        
  822.     elseif briArg <= 25 then
  823.    
  824.         retAdjustedColr = self:mixLighterColr(colrArg)
  825.        
  826.     end
  827.  
  828.     return retAdjustedColr
  829.    
  830. end
  831.  
  832. function ColrGenerator:adjustHueForColr()
  833.    
  834.    
  835.    
  836. end
  837.  
  838. --# ParameterUpdater
  839. function updateColrParameters(topBClrArg, btmBClrArg)
  840.    
  841.     --[[print("topBClrArg:")
  842.     print(topBClrArg)
  843.     print("btmBClrArg:")
  844.     print(btmBClrArg)]]
  845.     colrGenerator:updateColrs(topBClrArg, btmBClrArg)
  846.    
  847.     parameter.color("topInnerColr", topInnerColrCalc)
  848.     parameter.color("bottomInnerColr", bottomInnerColrCalc)
  849.    
  850.     parameter.color("blockColrA", adjustedTopBaseColr)
  851.     parameter.color("blockColrB", adjustedBottomBaseColr)
  852.  
  853. end
  854.  
  855. function updateTopBClrParameter(newColrValue)
  856.    
  857.     if topBClrParamDur >= 1 then
  858.  
  859.         parameter.clear()
  860.        
  861.         topBClrParamInit(newColrValue)
  862.         btmBClrParamInit(bottomBaseColr)
  863.         updateColrParameters(newColrValue, bottomBaseColr)
  864.        
  865.         updateRotationParameters()
  866.         updateCameraParameters()
  867.        
  868.     end
  869.    
  870.    
  871. end
  872.  
  873. function updateBtmBClrParameter(newColrValue)
  874.    
  875.     if btmBClrParamDur >= 1 then
  876.        
  877.         parameter.clear()
  878.        
  879.         topBClrParamInit(topBaseColr)
  880.         btmBClrParamInit(newColrValue)
  881.         updateColrParameters(topBaseColr, newColrValue)
  882.        
  883.         updateRotationParameters()
  884.         updateCameraParameters()
  885.     end
  886.    
  887.    
  888. end
  889.  
  890. function topBClrParamInit(colrArg)
  891.    
  892.     topBClrParamInitTime = ElapsedTime
  893.     updateTopBtmBClrParamDur()
  894.     parameter.color("topBaseColr", colrArg, updateTopBClrParameter)
  895.    
  896. end
  897.  
  898. function btmBClrParamInit(colrArg)
  899.    
  900.     btmBClrParamInitTime = ElapsedTime
  901.     updateTopBtmBClrParamDur()
  902.     parameter.color("bottomBaseColr", colrArg, updateBtmBClrParameter)
  903.    
  904. end
  905.  
  906. function updateTopBtmBClrParamDur()
  907.    
  908.     topBClrParamDur = ElapsedTime - topBClrParamInitTime
  909.     btmBClrParamDur = ElapsedTime - btmBClrParamInitTime
  910.    
  911. end
  912.  
  913. function updateRotationParameters()
  914.    
  915.     parameter.integer("rotationIdx", -24, 24, -3)
  916.     parameter.watch("rotationIdx*15")
  917.    
  918. end
  919.  
  920. function updateCameraParameters()
  921.    
  922.     parameter.number("cameraX", -shapeWidth, shapeWidth, 0)
  923.     parameter.number("cameraHeight", -5, 5, 3.55)
  924.     parameter.number("cameraZ", 0, 5, 14)
  925.    
  926. end
  927.  
  928. function updateAllParameters()
  929.    
  930.     parameter.clear()
  931.     updateColrParameters()
  932.     updateRotationParameters()
  933.     updateCameraParameters()
  934.    
  935. end
  936. --# Credits
  937. -- Original 3D spinning shapes project by Valgo
  938. -- See here for code http://forum.gethopscotch.com/t/valgos-ama-ask-me-anything/20408/161
  939.  
  940. -- Remix from t1:
  941. -- slight shading on blocks further from light source
  942. -- gradient background
  943. -- and some re-organisation (which is all over the place 😜😂)
  944.  
  945. -- Thanks to yojimbo2000 for help with getting isometric camera angle and removing perspective
  946. -- See here for discussion https://codea.io/talk/discussion/comment/70587#Comment_70587
  947.  
  948. -- Thanks to Intellection74 and everyone on Hopscotch for support and everything <3
  949. -- Thanks to Matthew C for helping me get started with coding in Codea too
  950.  
  951. -- And thanks to ustwogames™ for bringing us the beautiful Monument Valley game ❤️❤️❤️
  952. -- This is just fan-made stuff inspired by their work; none of this is associated with them in any way
  953.  
  954. -- And this is free to use/remix with credit; just let me (t1) know if you do for some reason decide to use it :)
  955. --# DevNotes
  956.  
  957. -- Dev notes
  958.  
  959. -- These are just tracking the titles of the projects
  960. -- (I have other notes elsewhere)
  961.  
  962. -- 3D Grad11 sync colrs
  963. -- 3D Grad10 camera
  964. -- 3D Grad08 remove some history
  965.  
  966. -- 3D Grad07 background
  967. -- could do radial
  968. -- do data[x][y][z] = 0, then can make shapes and set to 1
  969. -- project image on mesh
  970. -- set specific block types e.g. ladder, colourBlock from data[x][y][z] then in CubeColors() get block type from x, y, z of data array, then blockType[] = color(brown) | blockTypes = {"ladder" = etc, "blueBlock" = color(0,0,255)} | or separate for img mesh and colors
  971.  
  972. -- 3D 2 by Valgo
  973. -- slight gradient remix from t1
  974.  
  975. -- 3D Grad06 changed refPoint
  976.  
  977. --3D Grad06 -- trying refPoint as -scaleWidth rather than origin
  978.  
  979. -- 3D Grad05 -- scaled vecX, vecY, vecZ by cubeSize in CubeColors()
  980.  
  981. -- 3D Grad04 --no setContext
  982.  
  983. -- 3D by Valgo Gradient03 why shading individual
  984. --setContext cuts off part
  985.  
  986. -- 3D by Valgo Gradient02
  987.  
  988. -- 3D 2 by Valgo try gradient, -refactor
  989.  
  990. -- 3D 2 by Valgo refactor2
  991. -- small remix with refactoring & spherical shape from t1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement