Advertisement
Brahvim

Graphics!

Jun 11th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. I have studied that stuff before.
  2.  
  3.  
  4. You should go learn about what "Vectors" are - Professor Daniel Shiffman from https://YouTube.com/TheCodingTrain will explain fine (in his "The Nature of Code!" series).
  5.  
  6.  
  7. Scratch is missing *a lot* of the stuff you need. Variables are still scattered around the place. You can't make dimensional arrays (imagine a list inside a list!). It is *very* hard to pull off 3D graphics in such an environment! Also, it is completely `CPU`-based, so performance experiences a big loss.
  8.  
  9.  
  10.  
  11.  
  12. Now, let me to try to explain a bit of this myself :)
  13.  
  14.  
  15. # Some Math!
  16.  
  17. Skip ahead to the enxt 'section' if you want to know how this Math is being used game engines.
  18.  
  19. The word "vector" comes from Latin, meaning "vehicle". A vector, is "data structure", with numbers in it.
  20.  
  21.  
  22. Imagine a number written on a white board - no, no! A mouse! Where can it move? Well, it can only move up, down, left... and right! ...right?! Haha! This is the idea of the 'dimensionality' of a data structure (just *something* that holds numbers!).
  23.  
  24.  
  25. In a vector, we make a *straight,* up-to-down list of numbers. It is a one-dimensional data structure, since we're traveling downwards.
  26.  
  27.  
  28. In Scratch, all objects have an `(x, y)` position, and guess what? THIS, is also a vector - a list of numbers! You can fit anything - even the `RGB` values for a color! ..or even put a `z`-axis and... all fo a sudden you have `(x, y, z)` coordinates.
  29.  
  30.  
  31. However, our mouse can also move left and right. If we let it move in both axes (as if it is in 2D space), we'd make a "matrix". Vectors only had columns - numbers listed from the too, to the bottom. "Matrices" however, are tables of numbers, where we have both rows and columns. Also, note how a vector is basically a matrix, but it's not taking advantage of the 2D space available!
  32.  
  33.  
  34. This might remind you of our good old ordinary numbers that had none of this 'dimensionality' nonsense. We call them "scalars" with love.
  35.  
  36.  
  37. Now... We can do things such as adding and subtracting with both of these. If you have a `4x4` matrix, then you can indeed, add it to another `4x4` matrix. It's simple - just add or subtract the numbers that are in the same places, and... you'd have the result!
  38.  
  39.  
  40. Same with vectors - as long as you know they're the same size, it works!
  41.  
  42.  
  43. MULTIPLICATION AND DIVISION, however, are very complicated. Matrices only have this thing called a "dot product", whereas vectors have both a "dot" and a "cross" product. Divison in matrices only applies in some cases!
  44.  
  45. Before we discuss those, you need to understand that vectors are something you can... visualise! Remember how I said that a vector is like the `(x, y)` position? Well... yeah! In Scratch, the stage has it's "origin point" (the `(0, 0)` point) at its very center. Imagine if we have something at the position `(144, 72)`. I could, for example... draw an arrow towards it. This arrow, is supposed to 'represent' a vector. Hello, vector!
  46.  
  47.  
  48. Now imagine if we had another pointing to some other `(x, y)` coordinate. If we're to write them out in the mathemtical way, and try to multiply them (AKA take the product of their `x` and `y` 'components'), we'd see these behaviours when using the `dot` and `cross` products:
  49.  
  50. The `dot product` method gives us a SCALAR number - remember? Just an ordinary number!
  51.  
  52. The `cross product`, gives us a "perpendicular vector" - that is, an arrow that is pointing *through* the middle of both of them. For two 2D vectors that are already perpendicular to each other (that is, there is a 90-degree angle between both of the arrows), we'd see a third vector, which... is actually 3D! This looks similar to "Fleming's right hand law" (or... *"left hand law"*) from Physics. **After having looked up a photograph of it on Google** - if you think of the index finger and the thumb as the two 2D arrows we drew, the cross product would give us an arrow that is represented by the thumb.
  53.  
  54.  
  55. Alright, that felt like a math class. What's the good stuff?!
  56.  
  57.  
  58. Well, vectors can use "trigonometry", to *basically* connect these `(x, y)` with the concept of "direction" and "length" - don't our arrows have those? They point in a direction, and are of a some size! Aren't they?
  59.  
  60.  
  61. This gives us *awesome* powers such as being able to find the *direction* between two points on the screen, or making some awesome rotation animations (think of Physics Engines! Although Physics Engines use a bit more math, they HEAVILY use the concept of vectors!).
  62.  
  63.  
  64. Now... we can also multiply a vector by a matrix. It sounds weird, but it is doable. And matrices can do some awesome stuff!
  65.  
  66.  
  67. # And finally, Game Engines!
  68.  
  69. Please search and learn about terms such as "`GPU`s" and "`API`s" on Google, if you don't know what they are.
  70.  
  71.  
  72. ***NOW,*** game engines use a "Graphics API", such as... `OpenGL` ("free" software - anybody can use it, and it is present on all machines - it is maintained by the "Khronos Group"), `DirectX` (made and maintained by Microsoft as a kind of 'wrapper' on `OpenGL`, I guess. Apparently, it's a little faster on Windows.) or `Vulkan` (a newer version of `OpenGL` made by Khronos that came in 2015, promising more control over how memory is used in the `GPU`).
  73.  
  74.  
  75. Scratch 3 *actually* uses `OpenGL` in the form of "`WebGL`", which is a version of `OpenGL` made for browsers so that the same code could be run on browsers on both mobile and desktop devices!
  76.  
  77.  
  78. This "graphics API", allows us to contact the `GPU` on our computer, and ask it to draw things to the screen, and sometimes, we ask it to do some Math for us. It is a device that does everything very fast in both scenarios, since it is designed that way!
  79.  
  80.  
  81. (At some point, you'll study about electric circuits in Physics. There, you'll be told about "series" and "parallel" circuits. The `CPU` is like a series circuit - everything is calculated one-by-one. The `GPU`, does this faster, since it takes the idea of "parallel processing", and *essnetially,* has THOUSANDS of tiny `CPU`-like units that calculate things fast by cutting out on precision and electric power.)
  82.  
  83.  
  84. Recall how animations are an illusion where things move in the screen *little-by-little* after very small amounts of time? Yeah. Our computers do that when we animate things, and during each 'cycle' of animation, we send LOADS of data - images, shape color data and the `(x, y)` positions of the points of every shape we draw, to the `GPU` for it to calculate (yeah, those are vectors!).
  85.  
  86.  
  87. Now, the `GPU` takes this data, and following instructions written in a "shader program", uses this data to draw things to the screen!
  88.  
  89.  
  90. In this data, as stated earlier, we provide the positions for *vertices* (that is, the `(x, y)` positions for the points of a shape), and those mathematical number tables we discussed earlier - "Matrices", to the `GPU`.
  91.  
  92.  
  93. Mainly, we send three of these:
  94.  
  95. - A "projection" matrix,
  96. - A "view"/"eye"/"camera" matrix, and,
  97. - A "model" matrix.
  98.  
  99.  
  100. The "model" matrix has data about the position, rotation and size of a shape, and is used to modify the vector data (`(x, y)` positions) the `GPU` will ultimately give everything we're drawing.
  101.  
  102.  
  103. The "camera" matrix, gives us the illusion of all of these objects being present in a " game world". You can *actually* give the `GPU` numbers to create zoom, rotation and "change in location in a 3D (or 2D) world"-kind of effects.
  104.  
  105.  
  106. The "projection" matrix, is of two types. "Orthographic", and "Perspective". **No, nobody types out these numbers by hand anymore - *everybody* uses already available code for this!**
  107.  
  108.  
  109. This "projection" matrix, is what *scales*, that is, resized everything on the screen, to make sure it fits on every screen size. Orthographic matrices create a view, where everything appears to be the same size even if they are far away somewhere on the `z`-axis. Perspective ones, give us the much wanted 3D illusion - making things that are far away, appear more near the center of the screen, and things that are closer, close to the corners. Now, as I said before - nobody types the numbers for a projection matrix themselves. We mostly use ready-made code for this. That code too, however, needs data, such as:
  110.  
  111. - `FOV` ("**F**ield **O**f **V**iew" - tells the graphics API *how much* of the world we want to able to see at once - imagine sitting in a chair. If you tilt yourself forwards, you see less. If you tilt yourself backwards, you see more at once!)
  112. - the "aspect ratio* of the screen (`width / height`),
  113. - the "near" and "far"... "clipping plane depths" - these tell the computer to *stop* displaying things that are too far away on the `z`-axis ("far plane's depth"), and things that are *too near* ("near plane's depth").
  114.  
  115.  
  116. The problem? You'll *have* to calculate these numbers on Scratch **BY HAND**, which is... no joke, a task very hard for actual engineers, too - real engineers may understand what they're doing, but you can always get it wrong!
  117.  
  118.  
  119. Tools built for actual computer graphics *business* provide you all of this, but Scratch doesn't. Making these is a hard task, given how simple Scratch is, but some people using Scratch ***were*** very hardworking, and... got these done right! Salutes to them!
  120.  
  121.  
  122. The *terrifying*, terrifying part is optimization - these calculations are supposed to be done on a `GPU`, but they do it on the `CPU` on Scratch - oh God!
  123.  
  124. ..it's actually not all that bad, since the actual displaying of the graphics is still done by Scratch using `WebGL`, so it may be fine. But hey - Scratch doesn't let you modify any of it - you're making another layer on whatever exists! Optimization ded!!!1!1!
  125.  
  126. ...
  127.  
  128.  
  129. That's how it works. Took me an hour to write this - uhm, *bye!*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement