Advertisement
Guest User

Codea MV-style 3D -t1

a guest
Mar 25th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.50 KB | None | 0 0
  1.  
  2. --# Main
  3. --# Main
  4. -- 3D Grad
  5.  
  6.  
  7. function setup()
  8.    
  9.     print("Grad30")
  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, cubeVecX, cubeVecY, cubeVecZ)
  583.     local ret = {}
  584.    
  585.     vecRefPoint = cubeSize*vec3(0,0,0)
  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*cubeVecX)+vertVec.x,(cubeSize*cubeVecY)+vertVec.y,(cubeSize*cubeVecZ)+vertVec.z)
  596.         distToRefPoint = vecRefPoint:dist(vecToPoint)
  597.         maxDistAcross = vec3(-shapeWidth,-shapeWidth,-shapeWidth):dist(vec3(shapeWidth,shapeWidth,shapeWidth))
  598.         maxDistAcross = cubeSize * maxDistAcross
  599.         scaleFactor = 1 * cubeSize
  600.        
  601.         baseShadeColr = baseShadeColr:mix(color(0), scaleFactor * distToRefPoint/maxDistAcross) --NOTE: just tried math.min( 1) and doesn't shade
  602.        
  603.         --[[
  604.         if vertIdx == 36 and cubeVecY == 13 and ElapsedTime < 1 then
  605.             print(distToRefPoint/maxDistAcross) --takes really long
  606.         end
  607.         ]]
  608.        
  609.         table.insert(ret, baseShadeColr)
  610.     end
  611.    
  612.     return ret
  613. end
  614.  
  615. --# gradBackground
  616. gradBackground = class()
  617.  
  618. function gradBackground:init()
  619.    
  620.     print("Smooth Background05 two tone")
  621.    
  622.     --[[
  623.     parameter.color("topOuterColr", topBaseColr)
  624.     parameter.color("topInnerColr", self.topInnerInitColr)
  625.    
  626.     parameter.color("bottomOuterColr", bottomBaseColr)
  627.     parameter.color("bottomInnerColr", self.bottomInnerInitColr)
  628.     ]]
  629.     --parameter.action("Switch Colrs", self:switchColrs())
  630.    
  631.    
  632. end
  633.  
  634.  
  635. function gradBackground:draw()
  636.    
  637.     gradBackgrndMesh = gradBackground:generateMesh()
  638.     gradBackgrndMesh:draw()
  639.    
  640. end
  641.  
  642. function gradBackground:drawMeshInImg()
  643.    
  644.     local bgImg = image(WIDTH, HEIGHT)
  645.    
  646.     return bgImg
  647.    
  648. end
  649.  
  650. function gradBackground:generateMesh()
  651.    
  652.     -- Note: these coors are unused at the moment
  653.     topLeftCoor = vec2(0,HEIGHT)
  654.     bottomLeftCoor = vec2(0,0)
  655.     topRightCoor = vec2(WIDTH,HEIGHT)
  656.     bottomRightCoor = vec2(WIDTH,0)
  657.    
  658.     local backgrndMesh = mesh()
  659.     local backgrndMeshVerts = {}
  660.     local backgrndMeshColrs = {}
  661.    
  662.     for xIdx = 0, 1 do
  663.        
  664.         --print("xIdx = "..xIdx)
  665.        
  666.         for triHalfIdx = 0, 1 do
  667.            
  668.             --print("triHalfIdx = "..triHalfIdx)
  669.            
  670.             diagTopVert = vec2()
  671.             diagTopVert.x = WIDTH/2
  672.             diagTopVert.y = HEIGHT
  673.             diagTopVertColr = topInnerColr
  674.             --print("diagTopVertX = "..diagTopVert.x.." diagTopVertY = "..diagTopVert.y)
  675.            
  676.             diagBtmVert = vec2()
  677.             diagBtmVert.x = xIdx * WIDTH
  678.             diagBtmVert.y = 0
  679.             diagBtmVertColr = bottomOuterColr
  680.             --print("diagBtmVertX = "..diagBtmVert.x.." diagBtmVertY = "..diagBtmVert.y)
  681.            
  682.             cornerVert = vec2()
  683.             cornerVertXSign = xIdx*2 - 1
  684.             cornerVert.x = WIDTH/2 + (triHalfIdx * WIDTH/2) * cornerVertXSign
  685.             cornerVert.y = triHalfIdx * HEIGHT
  686.             --print("cornerVertX = "..cornerVert.x.." cornerVertY = "..cornerVert.y)
  687.            
  688.             baseColrList = {bottomInnerColr, topOuterColr}
  689.             cornerVertColr = baseColrList[triHalfIdx + 1]
  690.            
  691.             --Insert set of three verts as a triangle
  692.             table.insert(backgrndMeshVerts, diagTopVert)
  693.             table.insert(backgrndMeshVerts, diagBtmVert)
  694.             table.insert(backgrndMeshVerts, cornerVert)
  695.            
  696.             table.insert(backgrndMeshColrs, diagTopVertColr)
  697.             table.insert(backgrndMeshColrs, diagBtmVertColr)
  698.             table.insert(backgrndMeshColrs, cornerVertColr)
  699.            
  700.         end
  701.        
  702.     end
  703.    
  704.     backgrndMesh.vertices = backgrndMeshVerts
  705.    
  706.     backgrndMesh.colors = backgrndMeshColrs
  707.    
  708.     return backgrndMesh
  709.    
  710. end
  711. --# ColrGenerator
  712. ColrGenerator = class()
  713.  
  714. function ColrGenerator:init()
  715.    
  716.     randomMixAmount = math.random(75,80)/100
  717.     -- mixedTopAndBtmColr = topBaseColr:mix(bottomBaseColr, 0.5)
  718.    
  719.     print("Use the parameters to choose colo(u)rs.") --thanks to Inty :)
  720.    
  721. end
  722.  
  723. function ColrGenerator:draw()
  724.    
  725. end
  726.  
  727. function ColrGenerator:touched(touch)
  728.     -- Codea does not automatically call this method
  729. end
  730.  
  731. function HSB(hue, sat, bri)
  732.     local h = hue % 360
  733.     local s = math.min(math.max(sat, 0), 100)
  734.     local b = math.min(math.max(bri, 0), 100)
  735.     local colr = color(0, 0, 0)
  736.     if h < 60 then
  737.         colr = color(255, h * (17 / 4), 0)
  738.     elseif h < 120 then
  739.         colr = color((120 - h) * (17 / 4), 255, 0)
  740.     elseif h < 180 then
  741.         colr = color(0, 255, (h - 120) * (17 / 4))
  742.     elseif h < 240 then
  743.         colr = color(0, (240 - h) * (17 / 4), 255)
  744.     elseif h < 300 then
  745.         colr = color((h - 240) * (17 / 4), 0, 255)
  746.     else
  747.         colr = color(255, 0, (360 - h) * (17 / 4))
  748.     end
  749.     colr = colr:mix(color(255, 255, 255), s / 100)
  750.     colr = colr:mix(color(0, 0, 0), b / 100)
  751.     return colr.r, colr.g, colr.b
  752. end
  753.  
  754.  
  755. function getRandomRGBColr()
  756.    
  757.     local randomRGBColr = color(math.random(0,255), math.random(0,255), math.random(0,255))
  758.     return randomRGBColr
  759.    
  760. end
  761.  
  762. function getRandomHSBColr(hueMin, hueMax, satMin, satMax, briMin, briMax)
  763.    
  764.     local randomHSBColr = color(HSB(math.random(hueMin, hueMax), math.random(satMin, satMax), math.random(briMin, briMax)))
  765.     return randomHSBColr
  766.    
  767. end
  768.  
  769. function switchColrs()
  770.    
  771.     local currentTopColr = topOuterColr
  772.     local currentBottomColr = bottomOuterColr
  773.    
  774.     topOuterColr = currentBottomColr
  775.     bottomOuterColr = currentTopColr
  776.    
  777. end
  778.  
  779. function ColrGenerator:updateColrs(topBClrArg, btmBClrArg)
  780.  
  781.     topOuterColr = topBClrArg
  782.     bottomOuterColr = btmBClrArg
  783.    
  784.     topInnerColrCalc = topBClrArg:mix(btmBClrArg, randomMixAmount)
  785.     bottomInnerColrCalc = btmBClrArg:mix(topBClrArg, randomMixAmount)
  786.    
  787.     adjustedTopBaseColr = self:adjustBriForColr(topBClrArg)
  788.     adjustedBottomBaseColr = self:adjustBriForColr(btmBClrArg)
  789.    
  790. end
  791.  
  792. function ColrGenerator:mixDarkerColr(colrArg)
  793.    
  794.     local retDarkerColr = colrArg:mix(color(0), math.random(90,100)/100)
  795.     return retDarkerColr
  796.    
  797. end
  798.  
  799. function ColrGenerator:mixLighterColr(colrArg)
  800.    
  801.     local retLighterColr = colrArg:mix(color(255), math.random(90,100)/100)
  802.     return retLighterColr
  803.    
  804. end
  805.  
  806. function ColrGenerator:getBriFrRGBColr(colrArg)
  807.    
  808.     local retBri = 0
  809.     local redArg = colrArg.r
  810.     local greenArg = colrArg.g/255
  811.     local blueArg = colrArg.b/255
  812.    
  813.     retBri = math.max(redArg, greenArg, blueArg)/255 * 100
  814.    
  815.     return retBri
  816. end
  817.  
  818. function ColrGenerator:adjustBriForColr(colrArg)
  819.    
  820.     local retAdjustedColr = colrArg
  821.    
  822.     local briArg = self:getBriFrRGBColr(colrArg)
  823.    
  824.     if briArg >= 75 then
  825.        
  826.         retAdjustedColr = self:mixDarkerColr(colrArg)
  827.        
  828.     elseif briArg <= 25 then
  829.    
  830.         retAdjustedColr = self:mixLighterColr(colrArg)
  831.        
  832.     end
  833.  
  834.     return retAdjustedColr
  835.    
  836. end
  837.  
  838. function ColrGenerator:adjustHueForColr()
  839.    
  840.    
  841.    
  842. end
  843.  
  844. --# ParameterUpdater
  845. function updateColrParameters(topBClrArg, btmBClrArg)
  846.    
  847.     --[[print("topBClrArg:")
  848.     print(topBClrArg)
  849.     print("btmBClrArg:")
  850.     print(btmBClrArg)]]
  851.     colrGenerator:updateColrs(topBClrArg, btmBClrArg)
  852.    
  853.     parameter.color("topInnerColr", topInnerColrCalc)
  854.     parameter.color("bottomInnerColr", bottomInnerColrCalc)
  855.    
  856.     parameter.color("blockColrA", adjustedTopBaseColr)
  857.     parameter.color("blockColrB", adjustedBottomBaseColr)
  858.  
  859. end
  860.  
  861. function updateTopBClrParameter(newColrValue)
  862.    
  863.     if topBClrParamDur >= 1 then
  864.  
  865.         parameter.clear()
  866.        
  867.         topBClrParamInit(newColrValue)
  868.         btmBClrParamInit(bottomBaseColr)
  869.         updateColrParameters(newColrValue, bottomBaseColr)
  870.        
  871.         updateRotationParameters()
  872.         updateCameraParameters()
  873.        
  874.     end
  875.    
  876.    
  877. end
  878.  
  879. function updateBtmBClrParameter(newColrValue)
  880.    
  881.     if btmBClrParamDur >= 1 then
  882.        
  883.         parameter.clear()
  884.        
  885.         topBClrParamInit(topBaseColr)
  886.         btmBClrParamInit(newColrValue)
  887.         updateColrParameters(topBaseColr, newColrValue)
  888.        
  889.         updateRotationParameters()
  890.         updateCameraParameters()
  891.     end
  892.    
  893.    
  894. end
  895.  
  896. function topBClrParamInit(colrArg)
  897.    
  898.     topBClrParamInitTime = ElapsedTime
  899.     updateTopBtmBClrParamDur()
  900.     parameter.color("topBaseColr", colrArg, updateTopBClrParameter)
  901.    
  902. end
  903.  
  904. function btmBClrParamInit(colrArg)
  905.    
  906.     btmBClrParamInitTime = ElapsedTime
  907.     updateTopBtmBClrParamDur()
  908.     parameter.color("bottomBaseColr", colrArg, updateBtmBClrParameter)
  909.    
  910. end
  911.  
  912. function updateTopBtmBClrParamDur()
  913.    
  914.     topBClrParamDur = ElapsedTime - topBClrParamInitTime
  915.     btmBClrParamDur = ElapsedTime - btmBClrParamInitTime
  916.    
  917. end
  918.  
  919. function updateRotationParameters()
  920.    
  921.     parameter.integer("rotationIdx", -24, 24, -3)
  922.     parameter.watch("rotationIdx*15")
  923.    
  924. end
  925.  
  926. function updateCameraParameters()
  927.    
  928.     parameter.number("cameraX", -shapeWidth, shapeWidth, 0)
  929.     parameter.number("cameraHeight", -5, 5, 3.55)
  930.     parameter.number("cameraZ", 0, 5, 14)
  931.    
  932. end
  933.  
  934. function updateAllParameters()
  935.    
  936.     parameter.clear()
  937.     updateColrParameters()
  938.     updateRotationParameters()
  939.     updateCameraParameters()
  940.    
  941. end
  942. --# Credits
  943. -- Remix from t1:
  944. -- slight shading on blocks further from light source
  945. -- gradient background
  946. -- and some re-organisation (which is all over the place 😜😂)
  947.  
  948. -- Original 3D spinning shapes project by Valgo
  949. -- See here for code http://forum.gethopscotch.com/t/valgos-ama-ask-me-anything/20408/161
  950.  
  951. -- Thanks to yojimbo2000 for help with getting isometric camera angle and removing perspective
  952. -- See here for discussion https://codea.io/talk/discussion/comment/70587#Comment_70587
  953.  
  954. -- "Smooth background" concept thanks to Hopscotch
  955.  
  956. -- Thanks to Intellection74 and everyone on Hopscotch for support and everything <3
  957. -- Thanks to Matthew for helping me get started with coding in Codea too
  958.  
  959. -- And thanks to ustwogames™ for bringing us the beautiful Monument Valley game ❤️❤️❤️
  960. -- This is just fan-made stuff inspired by their work; none of this is associated with them in any way
  961.  
  962. -- And this is free to use/remix with credit; just let me know if you do for some reason decide to use it :)
  963. --# DevNotes
  964.  
  965. -- Dev notes
  966.  
  967. -- These are just tracking the titles of the projects
  968. -- (I have other notes elsewhere)
  969.  
  970. -- 3D Grad11 sync colrs
  971. -- 3D Grad10 camera
  972. -- 3D Grad08 remove some history
  973.  
  974. -- 3D Grad07 background
  975. -- could do radial
  976. -- do data[x][y][z] = 0, then can make shapes and set to 1
  977. -- project image on mesh
  978. -- 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
  979.  
  980. -- 3D 2 by Valgo
  981. -- slight gradient remix from t1
  982.  
  983. -- 3D Grad06 changed refPoint
  984.  
  985. --3D Grad06 -- trying refPoint as -scaleWidth rather than origin
  986.  
  987. -- 3D Grad05 -- scaled cubeVecX, cubeVecY, cubeVecZ by cubeSize in CubeColors()
  988.  
  989. -- 3D Grad04 --no setContext
  990.  
  991. -- 3D by Valgo Gradient03 why shading individual
  992. --setContext cuts off part
  993.  
  994. -- 3D by Valgo Gradient02
  995.  
  996. -- 3D 2 by Valgo try gradient, -refactor
  997.  
  998. -- 3D 2 by Valgo refactor2
  999. -- small remix with refactoring & spherical shape from t1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement