Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <!-- babylon.min.js v3.3.0 -->
<script src="https://cdn.jsdelivr.net/npm/cannon@0.6.2/build/cannon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babylonjs@3.3.0/babylon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babylonjs-gui@3.3.0/babylon.gui.min.js"></script>

<canvas id="renderCanvas"></canvas>

              
            
!

CSS

              
                html, body {
    overflow: hidden;
    width   : 100%;
    height  : 100%;
    margin  : 0;
    padding : 0;
}

#renderCanvas {
    width   : 100%;
    height  : 100%;
    touch-action: none;
}
              
            
!

JS

              
                //Babylon.js Version 3.3.0 のライブラリを使用してみました
//
//Babylon.jsのサンプルがある「Babylon.js Playground」からのコードを参考にして
//GUI機能のボタンでCubeオブジェクトを移動できるようにしてみました
//
//さらに衝突判定ができるように
//操作対象のCubeオブジェクト、衝突対象のSphereオブジェクト、地面となるPlaneオブジェクト
//にライブラリを使用して物理エンジン(本体はCannon.jsのよう)を設定してみました
//
//
//下記のサイト・コードを参考にさせていただきました
//
//Use the Babylon GUI
//https://doc.babylonjs.com/how_to/gui
//https://www.babylonjs-playground.com/#XCPP9Y#121
//
//Use a Physics Engine
//https://doc.babylonjs.com/how_to/using_the_physics_engine
//https://www.babylonjs-playground.com/#BEFOO
//
//ゲーム開発 - Babylon.js: 初めての Web ゲームを強化する高度な機能
//https://msdn.microsoft.com/ja-jp/magazine/mt614269.aspx
//


var canvas = document.getElementById("renderCanvas");

var box;

var isLeftBtnDwn = false;
var isForwardBtnDwn = false;
var isBackwardBtnDwn = false;
var isRightBtnDwn = false;

var createScene = function () {
    
        var scene = new BABYLON.Scene(engine);

        // create a FreeCamera, and set its position to (x:0, y:5, z:-10)
        var camera = new BABYLON.FreeCamera('camera1', new BABYLON.Vector3(0, 5,-10), scene);
        
        // target the camera to scene origin
        camera.setTarget(BABYLON.Vector3.Zero());
        
        // attach the camera to the canvas
        camera.attachControl(canvas, false);
        
        // create a basic light, aiming 0,1,0 - meaning, to the sky
        var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0,1,0), scene);
        
        // create a built-in "sphere" shape; its constructor takes 6 params: name, segment, diameter, scene, updatable, sideOrientation 
        var sphere = BABYLON.Mesh.CreateSphere('sphere1', 16, 2, scene);
        
        // move the sphere 
        sphere.position.y = 1;
        sphere.position.z = 5;

        box = BABYLON.Mesh.CreateBox("box", 2.0, scene);
        var material = new BABYLON.StandardMaterial("material01", scene);
        box.material = material;
        
        material.diffuseColor = new BABYLON.Color3(1, 0, 0); 

            // move the sphere 
        box.position.y = 1;

    
        // create a built-in "ground" shape;
        var ground = BABYLON.Mesh.CreateGround('ground1', 16, 16, 2, scene);

	scene.enablePhysics();
    
	box.physicsImpostor = new BABYLON.PhysicsImpostor(box, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 1, restitution: 0.9 }, scene);
	
	sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1, restitution: 0.9 }, scene);
	ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, scene);
    
        // UI
        var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
    
        //下段 表示用ラベル ラベル部分設定 直接advancedTextureに追加
        var label = new BABYLON.GUI.Rectangle("label");
        label.background = "#00ffff";
        label.height = "60px";
        label.alpha = 0.5;
        label.width = "300px";
        label.cornerRadius = 25;
        label.thickness = 1;
        //label2.linkOffsetY = 30;
        label.top = "5%";
        label.paddingLeft = "10px";
            label.paddingRight = "10px";
        advancedTexture.addControl(label); 
        //下段 表示用ラベル テキスト部分設定 ラベルに追加
        var text = new BABYLON.GUI.TextBlock();
        text.text = "てすと";
        text.color = "black";
        text.fontSize = "20px";
        label.addControl(text); 

    
    
        //ボタンまとめ用パネル ここにボタンをまとめてからadvancedTextureに追加    
        var UiPanel = new BABYLON.GUI.StackPanel("horizontal");
        UiPanel.isVertical = false;
        UiPanel.top = "30%";
        UiPanel.width = "360px";
        UiPanel.height = "160px";
        UiPanel.fontSize = "14px";
        UiPanel.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
        UiPanel.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
        advancedTexture.addControl(UiPanel);

        
        //左ボタン
        var leftButton = BABYLON.GUI.Button.CreateSimpleButton("leftBut", "left");
        leftButton.paddingLeft = "10px";
        leftButton.width = "100px";
        leftButton.height = "50px";
        leftButton.cornerRadius = 5;
        leftButton.color = "white";
        leftButton.background = "black";

        leftButton.onPointerDownObservable.add(()=> {
            text.text = "左ぼたん \nが押されました";
            isLeftBtnDwn = true;
        });
    
        leftButton.onPointerUpObservable.add(()=> {
            text.text = "左ぼたん \nが開放されました";
            isLeftBtnDwn = false;
        });
    
        leftButton.onPointerOutObservable.add(()=> {
            text.text = "左ぼたん \nが開放されました";
            isLeftBtnDwn = false;
        });
        
        UiPanel.addControl(leftButton);
    
        //前進ボタン
        var forwardButton = BABYLON.GUI.Button.CreateSimpleButton("forwardBut", "forward");
        forwardButton.paddingLeft = "30px";
        forwardButton.paddingTop = "-50px";
        forwardButton.paddingBottom = "50px";
        forwardButton.paddingRight = "-20px";
        forwardButton.width = "100px";
        forwardButton.height = "50px";
        forwardButton.cornerRadius = 5;
        forwardButton.color = "white";
        forwardButton.background = "black";

        forwardButton.onPointerDownObservable.add(()=> {
            text.text = "前進ぼたん \nが押されました";
            isForwardBtnDwn = true;
        });
    
        forwardButton.onPointerUpObservable.add(()=> {
            text.text = "前進ぼたん \nが開放されました";
            isForwardBtnDwn = false;
        });
    
        forwardButton.onPointerOutObservable.add(()=> {
            text.text = "前進ぼたん \nが開放されました";
            isForwardBtnDwn = false;
        });
    
        UiPanel.addControl(forwardButton);

    
        //後退ボタン
        var backwardButton = BABYLON.GUI.Button.CreateSimpleButton("backwardBut", "backward");
        backwardButton.paddingLeft = "-70px";
        backwardButton.paddingTop = "50px";
        backwardButton.paddingBottom = "-50px";
        backwardButton.paddingRight = "30px";
        backwardButton.width = "50px";
        backwardButton.height = "50px";
        backwardButton.cornerRadius = 5;
        backwardButton.color = "white";
        backwardButton.background = "black";

        backwardButton.onPointerDownObservable.add(()=> {
            text.text = "後退ぼたん \nが押されました";
            isBackwardBtnDwn = true;
        });
    
        backwardButton.onPointerUpObservable.add(()=> {
            text.text = "後退ぼたん \nが開放されました";
            isBackwardBtnDwn = false;
        });
    
        backwardButton.onPointerOutObservable.add(()=> {
            text.text = "後退ぼたん \nが開放されました";
            isBackwardBtnDwn = false;
        });
    
        UiPanel.addControl(backwardButton);
    
        //右ボタン
        var rightButton = BABYLON.GUI.Button.CreateSimpleButton("rightButt", "right");
        rightButton.paddingRight = "10px";
        rightButton.width = "100px";
        rightButton.height = "50px";
        rightButton.cornerRadius = 5;
        rightButton.color = "white";
        rightButton.background = "black";

        rightButton.onPointerDownObservable.add(()=> {
            text.text = "右ぼたん \nが押されました";
            isRightBtnDwn = true;
        });
    
        rightButton.onPointerUpObservable.add(()=> {
            text.text = "右ぼたん \nが開放されました";
            isRightBtnDwn = false;
        });
    
        rightButton.onPointerOutObservable.add(()=> {
            text.text = "右ぼたん \nが開放されました";
            isRightBtnDwn = false;
        });
        
        UiPanel.addControl(rightButton);

    
    return scene;
};

var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
var scene = createScene();

engine.runRenderLoop(function () {
    if (scene) {
        
        //Leftボタン押下時 Boxを左に移動
        if(isLeftBtnDwn) box.position.x = box.position.x - 0.1;
        
        //Forwardボタン押下時 Boxを前進させる
        if(isForwardBtnDwn)  box.position.z = box.position.z + 0.1;
        
        //Backwardボタン押下時 Boxを後退させる
        if(isBackwardBtnDwn) box.position.z = box.position.z - 0.1;
        
        //Rightボタン押下時 Boxを右に移動
        if(isRightBtnDwn) box.position.x = box.position.x + 0.1;
        
        scene.render();
    }
});

// Resize
window.addEventListener("resize", function () {
    engine.resize();
});
              
            
!
999px

Console