본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][Scala.js] Scala.js / Three.js 3D 게임 프로그래밍 샘플 코드

by byungwoo733 2023. 1. 22.
728x90
반응형
SMALL

Scala.js / Three.js 3D 게임 프로그래밍 샘플 코드

/*Here is an example of a simple 3D game program written in Scala.js using the Three.js library*/
import org.scalajs.dom._
import scala.scalajs.js.annotation.JSExport
import org.scalajs.dom.html.Canvas
import scala.scalajs.js.timers._
import scala.scalajs.js
import org.scalajs.dom.raw.WebGLRenderingContext._
import org.scalajs.dom.html.Element
import org.scalajs.threejs._

@JSExport
object Game {
  val canvas = document.createElement("canvas").asInstanceOf[Canvas]
  val renderer = new WebGLRenderer(js.Dynamic.literal(canvas = canvas))
  document.body.appendChild(canvas)

  val scene = new Scene()
  val camera = new PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000)
  val cube = new Mesh(new BoxGeometry(1,1,1), new MeshLambertMaterial(js.Dynamic.literal(color = 0xff0000)))
  scene.add(cube)
  camera.position.z = 5

  val light = new PointLight(0xffffff)
  scene.add(light)

  var cubeRotation = 0.0

  @JSExport
  def main(): Unit = {
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight

    setInterval(1000 / 60) {
      update()
      draw()
    }
  }

  def update(): Unit = {
    cubeRotation += 0.1
    cube.rotation.x = cubeRotation
    cube.rotation.y = cubeRotation
  }

  def draw(): Unit = {
    renderer.render(scene, camera)
  }
}
728x90
LIST

댓글