当前位置:网站首页>babylon 里面加gltf 模型
babylon 里面加gltf 模型
2022-08-04 06:14:00 【qianbo_insist】
babylonjs
高光
先上图看结果,好看很多
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon.js实例学习:创建基础材料和阴影</title>
<style> html, body {
overflow: hidden; width: 100%; height: 100%; margin: 0; } canvas {
width: 100%; height: 100%; -ms-touch-action: none; touch-action: none; } </style>
</head>
<body>
<canvas></canvas>
<script src="js/babylon.js"></script>
<script> var canvas = document.querySelector("canvas"); var engine = new BABYLON.Engine(canvas, true); var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3(0.2, 0.2, 0.2); var camera = new BABYLON.FreeCamera("mainCamera", new BABYLON.Vector3(0, 5, -10), scene); camera.setTarget(BABYLON.Vector3.Zero()); camera.attachControl(canvas, false); var light = new BABYLON.SpotLight("light", new BABYLON.Vector3(0, 14, -1), new BABYLON.Vector3(0, -1, 0), 1, 16, scene); light.intensity = 0.9; var sphereMaterial = new BABYLON.StandardMaterial("sphereMaterial", scene); sphereMaterial.diffuseColor = new BABYLON.Color3(0.6, 0.2, 0.2); sphereMaterial.specularPower = 128; var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 2, scene); sphere.position.y = 1; sphere.material = sphereMaterial; var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene); groundMaterial.diffuseColor = new BABYLON.Color3(0.1, 0.3, 0.1); var ground = BABYLON.Mesh.CreateGround("ground", 10, 10, 2, scene); ground.material = groundMaterial; ground.receiveShadows = true; var shadowGenerator = new BABYLON.ShadowGenerator(1024, light); shadowGenerator.setDarkness(0.4); shadowGenerator.getShadowMap().renderList.push(sphere); shadowGenerator.useBlurVarianceShadowMap = true; shadowGenerator.blurScale = 2; shadowGenerator.bias = 0.01; engine.runRenderLoop(function() {
scene.render(); }); window.addEventListener("resize", function() {
engine.resize(); }) </script>
</body>
</html>
天空盒
var skybox = BABYLON.Mesh.CreateBox("skyBox", 6000.0, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("TropicalSunnyDay/TropicalSunnyDay", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
加载模型文件
babylonjs里面加模型文件等等比较简单
var delayCreateScene = function () {
// Create a scene.
var scene = new BABYLON.Scene(engine);
// Create a default skybox with an environment.
var hdrTexture = BABYLON.CubeTexture.CreateFromPrefilteredData("textures/environment.dds", scene);
var currentSkybox = scene.createDefaultSkybox(hdrTexture, true);
// Append glTF model to scene.
BABYLON.SceneLoader.Append("scenes/BoomBox/", "BoomBox.gltf", scene, function (scene) {
// Create a default arc rotate camera and light.
scene.createDefaultCameraOrLight(true, true, true);
// The default camera looks at the back of the asset.
// Rotate the camera by 180 degrees to the front of the asset.
scene.activeCamera.alpha += Math.PI;
});
return scene;
};
code
不使用天空盒,使用默认环境,如下图所示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon.js</title>
<style> html, body {
overflow: hidden; width: 100%; height: 100%; margin: 0; } canvas {
width: 100%; height: 100%; -ms-touch-action: none; touch-action: none; } </style>
</head>
<body>
<canvas></canvas>
<!-- <script src="js/babylon.js"></script> -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script> var canvas = document.querySelector("canvas"); var engine = new BABYLON.Engine(canvas, true); var scene = new BABYLON.Scene(engine); scene.clearColor = new BABYLON.Color3(0.2, 0.2, 0.2); var camera = new BABYLON.FreeCamera("mainCamera", new BABYLON.Vector3(0, 5, -10), scene); camera.setTarget(BABYLON.Vector3.Zero()); camera.attachControl(canvas, false); var light = new BABYLON.SpotLight("light", new BABYLON.Vector3(0, 14, -1), new BABYLON.Vector3(0, -1, 0), 1, 16, scene); light.intensity = 0.9; var sphereMaterial = new BABYLON.StandardMaterial("sphereMaterial", scene); sphereMaterial.diffuseColor = new BABYLON.Color3(0.6, 0.2, 0.2); sphereMaterial.specularPower = 128; var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 2, scene); sphere.position.y = 1; sphere.position.x = -10; sphere.material = sphereMaterial; var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene); groundMaterial.diffuseColor = new BABYLON.Color3(0.1, 0.3, 0.1); var ground = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 2, scene); ground.material = groundMaterial; ground.receiveShadows = true; var shadowGenerator = new BABYLON.ShadowGenerator(1024, light); shadowGenerator.setDarkness(0.4); shadowGenerator.getShadowMap().renderList.push(sphere); shadowGenerator.useBlurVarianceShadowMap = true; shadowGenerator.blurScale = 2; shadowGenerator.bias = 0.01; // Append glTF model to scene. BABYLON.SceneLoader.ImportMeshAsync("", "/bed/", "bed.gltf").then((result)=>{
result.meshes[0].position.x = 10; result.meshes[0].position.y = 0; // const myMesh1 = scene.getMeshByName("myMesh_1"); result.meshes[0].scaling = new BABYLON.Vector3(100,100,100); //或者单独赋值 //result.meshes[0].rotation.y = Math.PI / 2; scene.createDefaultCameraOrLight(true, true, true); scene.createDefaultEnvironment(); scene.activeCamera.position.y +=100; // scene.activeCamera.setPosition(new BABYLON.Vector3(10,420,10)); // scene.activeCamera.setTarget(new BABYLON.Vector3(0,0,0)); }); //Name of the // BABYLON.SceneLoader.Append("/bed/", "bed.gltf", scene, function (scene) {
// // Create a default arc rotate camera and light. // scene.createDefaultCameraOrLight(true, true, true); // // The default camera looks at the back of the asset. // // Rotate the camera by 180 degrees to the front of the asset. // //scene.activeCamera.alpha += Math.PI; // }); // var skybox = BABYLON.Mesh.CreateBox("skyBox", 2500.0, scene); // var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene); // skyboxMaterial.backFaceCulling = false; // skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/TropicalSunnyDay", scene); // skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE; // skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0); // skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0); // skyboxMaterial.disableLighting = true; // skybox.material = skyboxMaterial; engine.runRenderLoop(function() {
scene.render(); }); window.addEventListener("resize", function() {
engine.resize(); }) </script>
</body>
</html>
边栏推荐
猜你喜欢
随机推荐
CAN协议详解-01
likeshop外卖点餐系统开源啦100%开源无加密
千古第一文人苏轼的众CP
IoU, GIoU, DIoU and CIoU in target detection
Base64编码原理
MySQL基础(DDL、DML、DQL)
53个全球免费学术资源数据库整理,查资料写论文必备【开学必备】
SQL去重的三种方法汇总
元素的增删克隆以及利用增删来显示数据到页面上
NelSon:一款新的适配matlab编程语法的编程工具
MMDeploy部署实战系列【第二章】:mmdeploy安装及环境搭建
HbuilderX 启动微信小程序 无法打开项目
JVM 快速检测死锁
原型图总结规范
有趣的USB接口和颜色分类
adb无法桥接夜神模拟器
Computer software: recommend a disk space analysis tool - WizTree
七夕情人节:中英文祝福短信送给你
unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
MySQL(4)