前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >babylon.js 学习笔记(2)

babylon.js 学习笔记(2)

作者头像
菩提树下的杨过
发布2023-05-23 17:17:55
5620
发布2023-05-23 17:17:55
举报

如何在网页中嵌入设计好的模型?

上回继续,我们设计好精美的模型后,最终总要展示给客户,比如利用playground画了1个方块:

代码语言:javascript
复制
const createScene =  () => {
    const scene = new BABYLON.Scene(engine);

    const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new BABYLON.Vector3(0, 0, 0));
    camera.attachControl(canvas, true);

    const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0));

    const box = BABYLON.MeshBuilder.CreateBox("box", {});

    return scene;
}

Playground平台可以直接导出为.babylon文件

随便弄个html,只需要下面2行即可: 

代码语言:javascript
复制
<script src="https://cdn.babylonjs.com/viewer/babylon.viewer.js"></script>
...

<babylon model="./scene.babylon"></babylon>
...

效果:

在线地址:https://yjmyzz.github.io/babylon_js_study/day02/01.html

除此之外,还有另1种导入方式,基本用法如下:

代码语言:javascript
复制
BABYLON.SceneLoader.ImportMeshAsync(model_name, folder_path, file_name, scene);

第1个参数model_name有以下3种变化:

代码语言:javascript
复制
BABYLON.SceneLoader.ImportMeshAsync("", "/relative path/", "myFile"); //第1个参数为空,表示导入myFile中的所有模型
BABYLON.SceneLoader.ImportMeshAsync("model1", "/relative path/", "myFile"); //仅导入model1(根据名称)
BABYLON.SceneLoader.ImportMeshAsync(["model1", "model2"], "/relative path/", "myFile"); //第1个参数,可以传入数组,导入指定的多个模型

另外实际开发中,经常会写一些必要的重复代码,可以直接利用下面的html模板:

代码语言:javascript
复制
<!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: 50%;
            height: 50%;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            touch-action: none;
        }

        #canvasZone {
            width: 100%;
            height: 100%;
        }
    </style>

    <!-- <script src="https://cdn.babylonjs.com/babylon.js"></script>
    <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
    <script src="https://code.jquery.com/pep/0.4.3/pep.js"></script> -->

    <!-- 导入babylon核心功能 -->
    <script src="../js/babylon.js"></script>
    <!-- 允许scene中导入model -->
    <script src="../js/babylonjs.loaders.min.js"></script>
    <!-- 允许使用触屏 -->
    <script src="../js/pep.js"></script>
</head>

<body>
    <canvas id="renderCanvas" touch-action="none"></canvas>

    <script>
        const canvas = document.getElementById("renderCanvas");
        const engine = new BABYLON.Engine(canvas, true);

        //在这里添加自己的核心代码
        const createScene = function () {
            const scene = new BABYLON.Scene(engine);

            //导入当前目录下的scene.babylon中的所有模型
            BABYLON.SceneLoader.ImportMeshAsync("", "./", "scene.babylon");
            const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new BABYLON.Vector3(0, 0, 0));
            camera.attachControl(canvas, true);
            const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0));

            return scene;
        };

        const scene = createScene();

        engine.runRenderLoop(function () {
            scene.render();
        });

        window.addEventListener("resize", function () {
            engine.resize();
        });
    </script>
</body>

</html>

只需在createScene()中添加自己的代码实现即可,导入model时,注意下目录的相对位置(参考下图)

在线地址:https://yjmyzz.github.io/babylon_js_study/day02/03.html

参考文档:

https://doc.babylonjs.com/features/introductionToFeatures/chap1/first_import

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档