About Monkey 2 › Forums › Monkey 2 Code Library › 3D Starfield Example
This topic contains 3 replies, has 4 voices, and was last updated by
Pakz 9 months ago.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
July 17, 2018 at 10:17 am #15069
A simple and easy 3D starfield effect in 2D:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101Namespace starfield#Import "<std>"#Import "<mojo>"Using std..Using mojo..Const VIRTUAL_SIZE := New Vec2i(1920, 1080)Class MyWindow Extends WindowField timer:TimerField fps:Int = 60Field starSpeed:Float = .25Method New()Super.New("3D Starfield", 1920, 1080, WindowFlags.Resizable)Fullscreen = TrueLayout = "letterbox"timer = New Timer(fps, OnUpdate)Star3D.CreateStars(1000)EndMethod OnUpdate()Star3D.Update(starSpeed, Window.Width, Window.Height)EndMethod OnRender(canvas:Canvas) OverrideApp.RequestRender()Star3D.Render(canvas)EndMethod OnKeyEvent(event:KeyEvent) OverrideIf event.Type = EventType.KeyDown And event.Key = Key.Enter And event.Modifiers & Modifier.AltFullscreen = Not FullscreenEndIf event.Key = Key.EscapeApp.Terminate()EndEndMethod OnMeasure:Vec2i() OverrideReturn VIRTUAL_SIZEEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndClass Star3DGlobal stack:Stack<Star3D> = New Stack<Star3D>Field pos:Vec4fField posXY:Vec2fField color:ColorField size:FloatMethod New()Init()stack.Add(Self)EndMethod Init()pos = New Vec4f(Rnd(-3000, 3000), Rnd(-3000, 3000), Rnd(100, 1000), Rnd(0.5, 5))EndFunction CreateStars(amount:Int)For Local i:Int = 0 Until amountNew Star3D()NextEndMethod Calculate(width:Float, height:Float)Local x := ((pos.x / pos.z) * 100) + (width * .5)Local y := ((pos.y / pos.z) * 100) + (height * .5)posXY = New Vec2f(x, y)Local col := ((1.0 - (pos.z * .001)) * pos.w) * .2color = New Color(col, col, col)size = col * 3EndFunction Update(speed:Float, width:Float, height:Float)For Local s:Star3D = EachIn stacks.pos.z -= (s.pos.w * speed)s.Calculate(width, height)If (s.posXY.x < 0) Or (s.posXY.x > width) Or (s.posXY.y < 0) Or (s.posXY.y > height ) Or (s.pos.z < 1)s.Init()EndNextEndFunction Render(canvas:Canvas)For Local s:Star3D = Eachin stackcanvas.Color = s.colorcanvas.DrawCircle(s.posXY.x, s.posXY.y, s.size)canvas.Color = Color.WhiteNextEndEndJuly 21, 2018 at 9:02 am #15094Cool.
July 21, 2018 at 2:17 pm #15098Nice and always helpful to learn the basics of the language.
July 22, 2018 at 2:39 am #15099Very good! I am learning new things from this also.
-
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic.