babylon 0.0.15

A wrapper for Babylon.js
Documentation
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use alloc::boxed::Box;
use js_ffi::*;

pub struct BabylonApi {
    fn_log: JSInvoker,
    fn_error: JSInvoker,
    fn_debug: JSInvoker,
    fn_random: JSInvoker,
    fn_create_basic_scene: JSInvoker,
    fn_create_scene: JSInvoker,
    fn_create_sphere: JSInvoker,
    fn_create_cube: JSInvoker,
    fn_create_standard_material: JSInvoker,
    fn_dispose_mesh: JSInvoker,
    fn_set_position: JSInvoker,
    fn_set_scaling: JSInvoker,
    fn_set_material: JSInvoker,
    fn_set_emmisive_color: JSInvoker,
    fn_set_diffuse_color: JSInvoker,
    fn_set_specular_color: JSInvoker,
    fn_set_ambient_color: JSInvoker,
    fn_set_clear_color: JSInvoker,
    fn_set_alpha: JSInvoker,
    fn_add_keyboard_observable: JSInvoker,
    fn_add_observable: JSInvoker,
    fn_get_delta_time: JSInvoker,
    fn_create_arc_rotate_camera: JSInvoker,
    fn_create_hemispheric_light: JSInvoker,
    fn_create_point_light: JSInvoker,
    fn_create_gltf: JSInvoker,
}

impl Default for BabylonApi {
    fn default() -> Self {
        BabylonApi {
            fn_create_basic_scene: register_function(
                r#"
                function(selector){
                    var canvas = document.querySelector(selector);
                    var engine = new BABYLON.Engine(canvas, true); 
                    var createScene = function () {
                        var scene = new BABYLON.Scene(engine);
                        scene.createDefaultEnvironment({
                            createGround: false,
                            createSkybox: false,
                        });
    
                        // Add a camera to the scene and attach it to the canvas
                        var camera = new BABYLON.ArcRotateCamera(
                            "Camera",
                            Math.PI / 2,
                            Math.PI / 2,
                            2,
                            BABYLON.Vector3.Zero(),
                            scene
                        );
                        camera.attachControl(canvas, true);
    
                        // Add lights to the scene
                        var light1 = new BABYLON.HemisphericLight(
                            "light1",
                            new BABYLON.Vector3(1, 1, 0),
                            scene
                        );
                        var light2 = new BABYLON.PointLight(
                            "light2",
                            new BABYLON.Vector3(0, 1, -1),
                            scene
                        );
    
                        return scene;
                    };
                    var scene = createScene();
                    engine.runRenderLoop(function () {
                            scene.render();
                    });
                    window.addEventListener("resize", function () {
                            engine.resize();
                    });
                    return scene;
                }
            "#,
            ),
            fn_create_scene: register_function(
                r#"
                function(selector){
                    var canvas = document.querySelector(selector);
                    var engine = new BABYLON.Engine(canvas, true); 
                    var createScene = function () {
                        var scene = new BABYLON.Scene(engine);
                        return scene;
                    };
                    var scene = createScene();
                    engine.runRenderLoop(function () {
                            scene.render();
                    });
                    window.addEventListener("resize", function () {
                            engine.resize();
                    });
                    return scene;
                }
            "#,
            ),
            fn_create_sphere: register_function(
                r#"
                function(scene,size){
                    return BABYLON.MeshBuilder.CreateSphere(
                        null,
                        { diameter: size },
                        scene);
                }
            "#,
            ),
            fn_create_cube: register_function(
                r#"
                function(scene,w,h,d){
                    return BABYLON.MeshBuilder.CreateBox(
                        null,
                        { height: h, width: w, depth: d },
                        scene);
                }
            "#,
            ),
            fn_create_standard_material: register_function(
                r#"
                function(scene){
                    return new BABYLON.StandardMaterial(null, scene);
                }
            "#,
            ),
            fn_dispose_mesh: register_function(
                r#"
                function(mesh){
                    mesh.dispose()
                }
            "#,
            ),
            fn_log: register_function(
                r#"
                function(msg){
                    console.log(msg);
                }
            "#,
            ),
            fn_error: register_function(
                r#"
                function(msg){
                    console.error(msg);
                }
            "#,
            ),
            fn_debug: register_function(
                r#"
                function(){
                    debugger;
                }
            "#,
            ),
            fn_random: register_function(
                r#"
                function(){
                    return Math.random();
                }
            "#,
            ),
            fn_set_position: register_function(
                r#"
                function(mesh,x,y,z){
                    mesh.position = new BABYLON.Vector3(x,y,z);
                }
            "#,
            ),
            fn_set_scaling: register_function(
                r#"
                function(mesh,x,y,z){
                    mesh.scaling = new BABYLON.Vector3(x,y,z);
                }
            "#,
            ),
            fn_set_material: register_function(
                r#"
                function(mesh,mat){
                    mesh.material = mat;
                }
            "#,
            ),
            fn_set_emmisive_color: register_function(
                r#"
                function(mat,r,g,b){
                    mat.emissiveColor = new BABYLON.Color3(r, g, b);
                }
            "#,
            ),
            fn_set_diffuse_color: register_function(
                r#"
                function(mat,r,g,b){
                    mat.diffuseColor = new BABYLON.Color3(r, g, b);
                }
            "#,
            ),
            fn_set_specular_color: register_function(
                r#"
                function(mat,r,g,b){
                    mat.specularColor = new BABYLON.Color3(r, g, b);
                }
            "#,
            ),
            fn_set_ambient_color: register_function(
                r#"
                function(mat,r,g,b){
                    mat.ambientColor = new BABYLON.Color3(r, g, b);
                }
            "#,
            ),
            fn_set_clear_color: register_function(
                r#"
                function(scene,r,g,b){
                    scene.clearColor = new BABYLON.Color3(r, g, b);
                }
            "#,
            ),
            fn_set_alpha: register_function(
                r#"
                function(mat,a){
                    mat.alpha = a;
                }
            "#,
            ),
            fn_add_keyboard_observable: register_function(
                r#"
                function(scene,cb){
                    scene.onKeyboardObservable.add((kbInfo) => {
                        cb(kbInfo.type,kbInfo.event.keyCode)
                    });
                }
            "#,
            ),
            fn_add_observable: register_function(
                r#"
                function(scene,name,cb){
                    scene[name].add(() => {
                        cb()
                    });
                }
            "#,
            ),
            fn_get_delta_time: register_function(
                r#"
                function(scene){
                    return scene.getEngine().getDeltaTime();
                }
            "#,
            ),
            fn_create_arc_rotate_camera: register_function(
                r#"
                function(scene){
                    var camera = new BABYLON.ArcRotateCamera(
                        "Camera",
                        Math.PI / 2,
                        Math.PI / 2,
                        2,
                        BABYLON.Vector3.Zero(),
                        scene
                    );
                    return camera;
                }
            "#,
            ),
            fn_create_hemispheric_light: register_function(
                r#"
                function(scene){
                    var light = new BABYLON.HemisphericLight(
                        null,
                        new BABYLON.Vector3(1, 1, 0),
                        scene
                    );
                    return light;
                }
            "#,
            ),
            fn_create_point_light: register_function(
                r#"
                function(scene){
                    var light = new BABYLON.PointLight(
                        null,
                        new BABYLON.Vector3(0, 1, -1),
                        scene
                    );
                    return light;
                }
            "#,
            ),
            fn_create_gltf: register_function(
                r#"
                function(scene,file){
                    var dummy = new BABYLON.Mesh(null, scene);
                    BABYLON.SceneLoader.ImportMesh(null, "", file, scene, function (newMeshes, particleSystems, skeletons) {
                        for(v in newMeshes){
                            var dude = newMeshes[v];
                            dude.parent = dummy;
                        }
                    });
                    return dummy;
                }
                "#,
            ),
        }
    }
}

impl BabylonApi {
    pub fn create_basic_scene(selector: &str) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_basic_scene.invoke_1(selector).to_js_object()
    }
    pub fn create_scene(selector: &str) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_scene.invoke_1(selector).to_js_object()
    }
    pub fn create_sphere(scene_ref: &JSObject, size: f64) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_sphere
            .invoke_2(scene_ref, size)
            .to_js_object()
    }

    pub fn create_cube(scene_ref: &JSObject, width: f64, height: f64, depth: f64) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_cube
            .invoke_4(scene_ref, width, height, depth)
            .to_js_object()
    }

    pub fn create_standard_material(scene_ref: &JSObject) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_standard_material
            .invoke_1(scene_ref)
            .to_js_object()
    }

    pub fn dispose_mesh(mesh: &JSObject) {
        let api = globals::get::<BabylonApi>();
        api.fn_dispose_mesh.invoke_1(mesh);
    }

    pub fn log(msg: &str) {
        let api = globals::get::<BabylonApi>();
        api.fn_log.invoke_1(msg);
    }

    pub fn error(msg: &str) {
        let api = globals::get::<BabylonApi>();
        api.fn_error.invoke_1(msg);
    }

    pub fn debugger() {
        let api = globals::get::<BabylonApi>();
        api.fn_debug.invoke_0();
    }

    pub fn random() -> f64 {
        let api = globals::get::<BabylonApi>();
        api.fn_random.invoke_0()
    }

    pub fn set_position(mesh: &JSObject, x: f64, y: f64, z: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_position.invoke_4(mesh, x, y, z);
    }

    pub fn set_scaling(mesh: &JSObject, x: f64, y: f64, z: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_scaling.invoke_4(mesh, x, y, z);
    }

    pub fn set_material(mesh: &JSObject, mat: &JSObject) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_material.invoke_2(mesh, mat);
    }

    pub fn set_emmisive_color(mat: &JSObject, r: f64, g: f64, b: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_emmisive_color.invoke_4(mat, r, g, b);
    }

    pub fn set_diffuse_color(mat: &JSObject, r: f64, g: f64, b: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_diffuse_color.invoke_4(mat, r, g, b);
    }

    pub fn set_specular_color(mat: &JSObject, r: f64, g: f64, b: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_specular_color.invoke_4(mat, r, g, b);
    }

    pub fn set_ambient_color(mat: &JSObject, r: f64, g: f64, b: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_ambient_color.invoke_4(mat, r, g, b);
    }

    pub fn set_clear_color(mat: &JSObject, r: f64, g: f64, b: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_clear_color.invoke_4(mat, r, g, b);
    }

    pub fn set_alpha(mat: &JSObject, a: f64) {
        let api = globals::get::<BabylonApi>();
        api.fn_set_alpha.invoke_2(mat, a);
    }

    pub fn add_keyboard_observable(
        scene: &JSObject,
        callback: Box<dyn FnMut(JSValue, JSValue) -> () + Send>,
    ) {
        let cb = create_callback_2(callback);
        let api = globals::get::<BabylonApi>();
        api.fn_add_keyboard_observable.invoke_2(scene, cb);
    }

    pub fn add_observable(
        scene: &JSObject,
        observable_name: &str,
        callback: Box<dyn FnMut() -> () + Send>,
    ) {
        let cb = create_callback_0(callback);
        let api = globals::get::<BabylonApi>();
        api.fn_add_observable.invoke_3(scene, observable_name, cb);
    }

    pub fn get_delta_time(scene: &JSObject) -> f64 {
        let api = globals::get::<BabylonApi>();
        api.fn_get_delta_time.invoke_1(scene)
    }

    pub fn create_arc_rotate_camera(scene: &JSObject) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_arc_rotate_camera
            .invoke_1(scene)
            .to_js_object()
    }

    pub fn create_hemispheric_light(scene: &JSObject) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_hemispheric_light
            .invoke_1(scene)
            .to_js_object()
    }

    pub fn create_point_light(scene: &JSObject) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_point_light.invoke_1(scene).to_js_object()
    }

    pub fn create_gltf(scene: &JSObject, file: &str) -> JSObject {
        let api = globals::get::<BabylonApi>();
        api.fn_create_gltf
            .invoke_2(scene, file)
            .to_js_object()
    }
}