Attempting to create a basic example for rendering a cube using the THREEJS library.
package three
import org.scalajs.dom
import scala.scalajs.js
import scala.scalajs.js.Dynamic._
import scala.scalajs.js.annotation.JSName
...
object ThreeExample
{
def render() =
{
val scene = new THREE.Scene()
val camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
val renderer = new THREE.WebGLRenderer(js.Dynamic.literal(
canvas = global.document.getElementById("ThreeCanvas")
));
val geometry = new THREE.BoxGeometry(1,1,1);
val material = new THREE.SimpleMeshBasicMaterial()
camera.position.z = 2;
val cube = new THREE.Mesh(geometry, material);
Now comes the interesting part. When attempting to do this:
scene.add(cube);
renderer.render(scene, camera);
An error is thrown:
ScalaJS.c.jl_ClassCastException {s$1: "[object Object] is not an instance of scala.runtime.Nothing$", e$1: null, stackTrace$1: null, stackdata: TypeError, constructor: function…}
But when trying this instead:
global._scene = scene
global._cube = cube
global._camera = camera
global._renderer = renderer
global._scene.add(global._cube)
global._renderer.render(global._scene, global._camera);
Everything renders properly without any errors. So, what's the difference between `global._scene` and `scene`, or `global._scene.add` and `scene.add`?