WebGL BabylonJS 如何创建场景

介绍babylon场景创建的基本流程

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Babylon Template</title>

        <style>
            html, body {
                overflow: hidden;
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
            }
            #renderCanvas {
                width: 100%;
                height: 100%;
                touch-action: none;
            }
        </style>

        <script src="https://cdn.babylonjs.com/babylon.js"></script>

    </head>

   <body>

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

	<script>
	//获取canvas对象
        const canvas = document.getElementById("renderCanvas");
    //创建渲染引擎
        const engine = new BABYLON.Engine(canvas, true); 
        const createScene = function () {
            // 创建场景
            const scene = new BABYLON.Scene(engine);
            // 创建相机
            const camera = new BABYLON.FreeCamera("camera1", 
                new BABYLON.Vector3(0, 5, -10), scene);
            // 设置相机目标
            camera.setTarget(BABYLON.Vector3.Zero());
            // 允许控制相机
            camera.attachControl(canvas, true);
            // 创建平行光,不然模型没有反射颜色
            const light = new BABYLON.HemisphericLight("light", 
                new BABYLON.Vector3(0, 1, 0), scene);
            // 设置灯光亮度
            light.intensity = 0.7;
            // 创建球体
            const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", 
                {diameter: 2, segments: 32}, scene);
            // 设置球体位置
            sphere.position.y = 1;
            // 创建一个平面
            const ground = BABYLON.MeshBuilder.CreateGround("ground", 
                {width: 6, height: 6}, scene);
            return scene;
        };
        const scene = createScene();
        // 循环渲染场景
        engine.runRenderLoop(function () {
                scene.render();
        });
        // 监听窗口尺寸变化
        window.addEventListener("resize", function () {
                engine.resize();
        });
	</script>

   </body>

</html>
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值