forked from jeremy-coleman/babylon-next
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBabylonScene.js
59 lines (52 loc) · 1.42 KB
/
BabylonScene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { PureComponent } from "react"
import {
ArcRotateCamera,
MeshBuilder,
Scene,
Engine,
HemisphericLight,
StandardMaterial,
Texture,
Vector3
} from "@babylonjs/core"
/**
* Babylon 3D Scene.
*/
export default class BabylonScene extends PureComponent {
componentDidMount() {
this.setup(this.canvas)
}
setup = (canvas) => {
const engine = this.createEngine(canvas)
const scene = new Scene(engine)
const camera = new ArcRotateCamera("Camera", -Math.PI / 3, Math.PI / 3, 10, Vector3.Zero(), scene)
camera.attachControl()
camera.radius = 3
const light = new HemisphericLight("Light", new Vector3(0.33, 1, -0.67), scene)
light.intensity = 0.9
const texture = new Texture(`/images/texture.png`, scene)
const mat = new StandardMaterial("Material", scene)
mat.diffuseTexture = texture
const box = new MeshBuilder.CreateBox("box", { size: 1 }, scene)
box.material = mat
engine.runRenderLoop(engine.renderLoop)
}
createEngine = (canvas) => {
const engine = new Engine(canvas)
engine.renderLoop = () =>
engine.scenes.forEach((scene) => {
if (scene.activeCamera) scene.render()
})
return engine
}
id = "Babylon"
onMount = (canvas) => (this.canvas = canvas)
render() {
return (
<>
<canvas id={this.id} ref={this.onMount} style={style} />
</>
)
}
}
const style = { width: "100%", height: "100%" }