TurtyWurty

Untitled

Jul 30th, 2023
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.90 KB | None | 0 0
  1. import math
  2.  
  3. from manim import *
  4. from numpy import interp
  5.  
  6.  
  7. class BlitDemonstration(Scene):
  8.     def construct(self):
  9.         def create_window():
  10.             dotTL = Dot(color=WHITE).shift(UP * 2 + LEFT * 3)
  11.             dotTR = Dot(color=WHITE).shift(UP * 2 + RIGHT * 3)
  12.             dotBR = Dot(color=WHITE).shift(DOWN * 2 + RIGHT * 3)
  13.             dotBL = Dot(color=WHITE).shift(DOWN * 2 + LEFT * 3)
  14.  
  15.             self.play(Create(dotTL), run_time=0.1)
  16.             self.play(Create(dotTR), run_time=0.1)
  17.             self.play(Create(dotBR), run_time=0.1)
  18.             self.play(Create(dotBL), run_time=0.1)
  19.  
  20.             topLine = Line(dotTL.get_center(), dotTR.get_center(), color=WHITE)
  21.             rightLine = Line(dotTR.get_center(), dotBR.get_center(), color=WHITE)
  22.             bottomLine = Line(dotBR.get_center(), dotBL.get_center(), color=WHITE)
  23.             leftLine = Line(dotBL.get_center(), dotTL.get_center(), color=WHITE)
  24.  
  25.             self.play(Create(topLine), run_time=0.5)
  26.             self.play(Create(rightLine), run_time=0.5)
  27.             self.play(Create(bottomLine), run_time=0.5)
  28.             self.play(Create(leftLine), run_time=0.5)
  29.  
  30.             window = VGroup(dotTL, dotTR, dotBL, dotBR, topLine, rightLine, bottomLine, leftLine)
  31.             return window
  32.  
  33.         def show_window_size():
  34.             topLeftDot = Dot(color=RED).shift(UP * 2 + LEFT * 3)
  35.             self.play(Create(topLeftDot), run_time=0.1)
  36.             self.play(topLeftDot.animate.scale(1.5), run_time=0.5)
  37.             topLeftText = Text("(0, 0)", color=RED).next_to(topLeftDot, UP)
  38.             self.play(Write(topLeftText))
  39.             self.play(topLeftText.animate.shift(DOWN * 0.25).scale(0.5), run_time=0.5)
  40.  
  41.             xPos = ValueTracker(0)
  42.             yPos = ValueTracker(0)
  43.  
  44.             topRightDot = always_redraw(
  45.                 lambda: Dot(color=RED)
  46.                 .shift(
  47.                     UP * 2 +
  48.                     LEFT * 3 +
  49.                     RIGHT * interp(xPos.get_value(), [0, 1028], [0, 6]))
  50.             )
  51.  
  52.             topRightText = always_redraw(lambda: Text(
  53.                 f"({int(xPos.get_value())}, 0.0)",
  54.                 color=RED
  55.             ).shift(
  56.                 UP * 2.5 +
  57.                 LEFT * 3 +
  58.                 RIGHT * interp(xPos.get_value(), [0, 1028], [0, 6])
  59.             ).scale(0.5))
  60.  
  61.             self.play(Create(topRightDot), Create(topRightText), run_time=0.1)
  62.             self.play(xPos.animate.set_value(1028), run_time=2.5, rate_func=linear)
  63.             self.play(topRightDot.animate.scale(1.5), run_time=0.5)
  64.  
  65.             self.wait(1)
  66.  
  67.             bottomRightDot = always_redraw(
  68.                 lambda: Dot(color=RED)
  69.                 .shift(
  70.                     UP * 2 +
  71.                     LEFT * 3 +
  72.                     RIGHT * interp(xPos.get_value(), [0, 1028], [0, 6]) +
  73.                     DOWN * interp(yPos.get_value(), [0, 768], [0, 4]))
  74.             )
  75.  
  76.             bottomRightText = always_redraw(lambda: Text(
  77.                 f"({int(xPos.get_value())}, {int(yPos.get_value())})",
  78.                 color=RED
  79.             ).shift(
  80.                 UP * 2.5 +
  81.                 LEFT * 3 +
  82.                 RIGHT * interp(xPos.get_value(), [0, 1028], [0, 6]) +
  83.                 DOWN * interp(yPos.get_value(), [0, 768], [0, 4])
  84.             ).scale(0.5))
  85.  
  86.             self.play(Create(bottomRightDot), Create(bottomRightText), run_time=0.1)
  87.             self.play(yPos.animate.set_value(768), run_time=2.5, rate_func=linear)
  88.             self.play(bottomRightDot.animate.scale(1.5), run_time=0.5)
  89.  
  90.         def demonstrate_blit():
  91.             blitCode = Text(
  92.                 text="blit(location, x, y, u, v, width, height)",
  93.                 font="Consolas"
  94.             ).scale(0.5).to_corner(UL)
  95.  
  96.             self.play(Write(blitCode), run_time=1.5)
  97.             self.wait(1)
  98.             self.play(blitCode.animate.shift(DOWN * 0.5), run_time=0.5)
  99.  
  100.             textureLocation = Text(
  101.                 text="private static final ResourceLocation TEXTURE = new ResourceLocation(TutorialMod.MODID, \"textures/gui.png\");",
  102.                 font="Consolas"
  103.             ).scale(0.325).to_corner(UL)
  104.  
  105.             self.play(Write(textureLocation), run_time=2)
  106.             self.wait(1)
  107.  
  108.             textureReplacedBlitCode = Text(
  109.                 text="blit(TEXTURE, x, y, u, v, width, height)",
  110.                 font="Consolas"
  111.             ).scale(0.5).to_corner(UL).shift(DOWN * 0.5)
  112.  
  113.             self.play(ReplacementTransform(blitCode, textureReplacedBlitCode), run_time=0.5)
  114.  
  115.             valueReplacedBlitCode = Text(
  116.                 text="blit(TEXTURE, 0, 0, 0, 0, 256, 256)",
  117.                 font="Consolas"
  118.             ).scale(0.5).to_corner(UL).shift(DOWN * 0.5)
  119.  
  120.             self.play(ReplacementTransform(textureReplacedBlitCode, valueReplacedBlitCode), run_time=0.5)
  121.  
  122.             self.wait(2)
  123.  
  124.             guiImage = ImageMobject("assets/images/sample_gui.png")
  125.             guiImage.shift(UP * 2 + LEFT * 3 + RIGHT * guiImage.get_width() / 2 + DOWN * guiImage.get_height() / 2)
  126.             self.play(FadeIn(guiImage), run_time=0.5)
  127.             self.wait(1)
  128.  
  129.             xValue = ValueTracker(0)
  130.             yValue = ValueTracker(0)
  131.  
  132.             blitImage = always_redraw(
  133.                 lambda: ImageMobject("assets/images/sample_gui.png")
  134.                 .shift(
  135.                     UP * 2 + DOWN * guiImage.get_height() / 2 +
  136.                     LEFT * 3 + RIGHT * guiImage.get_width() / 2 +
  137.                     RIGHT * interp(
  138.                         xValue.get_value(),
  139.                         [0, 1028],
  140.                         [0, 5.5 - guiImage.get_width()]
  141.                     ) +
  142.                     DOWN * interp(
  143.                         yValue.get_value(),
  144.                         [0, 768],
  145.                         [0, 4 - guiImage.get_height()]
  146.                     )
  147.                 )
  148.             )
  149.  
  150.             updatingPositionBlitCode = always_redraw(lambda: Text(
  151.                 f"blit(TEXTURE, {int(xValue.get_value())}, {int(yValue.get_value())}, 0, 0, 256, 256)",
  152.                 font="Consolas"
  153.             ).scale(0.5).to_corner(UL).shift(DOWN * 0.5))
  154.             self.play(ReplacementTransform(valueReplacedBlitCode, updatingPositionBlitCode), run_time=0.01)
  155.  
  156.             self.play(ReplacementTransform(guiImage, blitImage), run_time=0.5)
  157.             self.wait(1)
  158.             self.play(xValue.animate.set_value(1028), run_time=2.5, rate_func=linear)
  159.             self.play(yValue.animate.set_value(768), run_time=2.5, rate_func=linear)
  160.             self.wait(1)
  161.             self.play(xValue.animate.set_value(0), yValue.animate.set_value(0), run_time=0.75, rate_func=smooth)
  162.             self.wait(1)
  163.  
  164.             widthArrow = DoubleArrow(
  165.                 start=LEFT * 3 + UP * 2 + UP * 0.15,
  166.                 end=RIGHT * 3 + UP * 2 + UP * 0.15,
  167.                 color=BLUE,
  168.                 stroke_width=2,
  169.                 tip_length=0.25
  170.             )
  171.  
  172.             widthText = Text(
  173.                 text="1028",
  174.                 font="Consolas",
  175.                 color=BLUE
  176.             ).scale(0.5).next_to(widthArrow, UP * 0.15)
  177.  
  178.             self.play(Create(widthArrow), Write(widthText), run_time=0.5)
  179.             self.wait(1)
  180.  
  181.             heightArrow = DoubleArrow(
  182.                 start=LEFT * 3 + UP * 2 + LEFT * 0.15,
  183.                 end=LEFT * 3 + DOWN * 2 + LEFT * 0.15,
  184.                 color=GREEN,
  185.                 stroke_width=2,
  186.                 tip_length=0.25
  187.             )
  188.  
  189.             heightText = Text(
  190.                 text="768",
  191.                 font="Consolas",
  192.                 color=GREEN
  193.             ).scale(0.5).next_to(heightArrow, LEFT * 0.05).rotate(PI / 2)
  194.  
  195.             self.play(Create(heightArrow), Write(heightText), run_time=0.5)
  196.  
  197.         window = create_window()
  198.         self.wait(0.5)
  199.         show_window_size()
  200.         self.wait(0.5)
  201.         demonstrate_blit()
  202.         self.wait(0.5)
  203.  
  204.  
Advertisement
Add Comment
Please, Sign In to add comment