在babylon中
ImportMesh 和Append 方法的区别
-
Append 方法
用途:BABYLON.SceneLoader.Append 方法用于将模型添加到已经存在的场景中。它不会创建新的场景,而是将模型直接添加到传入的场景对象中。
返回值:该方法不返回加载的模型,而是将其直接附加到指定的场景。
适用情况:当你希望将多个模型合并到同一个场景中时,可以使用该方法 -
ImportMesh 方法
用途:BABYLON.SceneLoader.ImportMesh 方法用于导入指定的网格(mesh)到场景中。它允许你选择性地导入模型的一部分,而不仅仅是将整个场景加载进来。
返回值:该方法返回加载的网格对象,可以对它们进行进一步的操作。
适用情况:当你只需要从文件中加载特定的网格时,可以使用该方法。
<!--
* @Description: 优化后的代码
* @Author: wz
* @Date: 2024-11-12 10:25:03
* @LastEditors: wz
* @LastEditTime: 2024-11-14 10:32:11
* @FilePath: /babylon/ziji/dianji.html
*
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://cdn.babylonjs.com/gui/babylon.gui.min.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
<style>
* {
padding: 0;
margin: 0;
}
canvas {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<script>
try {
const canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
// 创建引擎和场景
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
console.log(engine, '引擎创建成功');
console.log(scene, '场景创建成功');
// 创建相机
const camera = new BABYLON.ArcRotateCamera("Camera", 30, Math.PI / 2.06, 120, BABYLON.Vector3.Zero(), scene);
camera.wheelPrecision = 1.8;
camera.upperBetaLimit = (Math.PI / 2) * 0.9;
camera.attachControl(canvas, true);
console.log(camera, '相机创建成功');
// 创建灯光
const light = new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 1, 0), scene);
console.log(light, '灯光创建成功');
// 创建全屏 UI
var advancedTexture;
// const advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI", scene);
var icos;
// 创建天空盒
const skybox = BABYLON.Mesh.CreateBox("skyBox", 9000.0, scene);
const skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("skybox", 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;
// // 加载模型
// BABYLON.SceneLoader.ImportMesh("","./", "dapeng2.gltf", scene, function (meshes) {
// // BABYLON.SceneLoader.Append("./", "dapeng2.gltf", scene, function (newScene) {
// const model = meshes[0]; // 获取加载的模型
// // 设置模型可点击
// model.isPickable = true;
// // 设置模型的位置
// // model.position = new BABYLON.Vector3(0, 0, 0);
// console.log(meshes, 'zijimeshesdianji.html-第84行')
// console.log(model, 'zijimodeldianji.html-第84行')
// // 创建图标
// const icon = BABYLON.MeshBuilder.CreateSphere("icon", { diameter: 0.2 }, scene);
// icon.material = new BABYLON.StandardMaterial("iconMaterial", scene);
// icon.material.emissiveColor = new BABYLON.Color3(1, 0, 0); // 设置图标颜色为红色
// icon.isVisible = false; // 默认隐藏图标
// // 检测点击事件
// scene.onPointerObservable.add((ptrEvt) => {
// if (ptrEvt.type === BABYLON.PointerEventTypes.POINTERUP) {
// const pickResult = scene.pick(ptrEvt.event.clientX, ptrEvt.event.clientY);
// console.log(pickResult, '-------------------------zijiptrEvtdianji.html-第99行')
// if (pickResult.hit && pickResult.pickedMesh === model) {
// // 显示图标并设置位置
// icon.position = pickResult.pickedPoint.add(new BABYLON.Vector3(0, 1, 0)); // 图标位置在模型上方
// icon.isVisible = true;
// }
// }
// });
// }, null, function (error) {
// console.error("加载错误:", error);
// });
// // 为画布添加点击事件监听器
// canvas.addEventListener("click", function (evt) {
// // 将点击事件的屏幕坐标转换为 Babylon.js 场景中的三维射线
// var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh.isPickable === true; });
// // 如果pickInfo.hit为true,则表示点击到了模型
// if (pickInfo.hit) {
// var clickedMesh = pickInfo.pickedMesh; // 获取被点击的模型
// console.log("点击了模型:", clickedMesh);
// console.log("点击了pickInfo:", pickInfo);
// // 在这里执行点击模型后的操作,例如改变颜色、播放动画等
// var clickedPosition = pickInfo.pickedPoint;
// // 设置图标的位置为点击的世界坐标位置
// if (clickedPosition) {
// let div = document.createElement('div');
// document.createElement('div');
// let img = document.createElement('img');
// img.src = './icon.png';
// img.style = "width:100%;height:100%;"
// div.style = "width:20px;height:20px;position:absolute;top:0;left:0;background-color:rgba(255,255,255,0.5);"
// console.log(img, 'zijiunit1.html-第120行')
// document.body.appendChild(div);
// div.appendChild(img);
// // 将 HTML 元素定位在点击位置上方
// var canvasRect = canvas.getBoundingClientRect();
// console.log(canvasRect, 'zijiunit1.html-第125行')
// console.log(evt.clientX, 'zijiunit1.html-evt.clientX')
// console.log(evt.clientY, 'zijiunit1.html-evt.clientX')
// // var x = canvasRect.left + (canvasRect.width / 2) + (evt.clientX - canvasRect.left);
// // var y = canvasRect.top + (canvasRect.height / 2) - (evt.clientY - canvasRect.top);
// // console.log(x,y, 'zijiunit1.html-第128行')
// updateElementPosition()
// function updateElementPosition() {
// if (clickedPosition) {
// // 将 3D 坐标转换为 2D 屏幕坐标
// var projectedPosition = scene.pointerX + "px " + (canvas.clientHeight - scene.pointerY) + "px";
// console.log(projectedPosition, 'zijiprojectedPositionunit1.html-第138行')
// div.style.left = projectedPosition;
// div.style.top = projectedPosition;
// }
// }
// }
// // icon.centerX = icon.width / 2;
// // icon.centerY = icon.height / 2;
// // icon.linkWithTransformNode = null; // 确保图标不随任何3D节点移动
// // icon.position = clickedPosition; // 设置图标的位置
// // // 将图标添加到 UI 中
// // advancedTexture.addControl(icon);
// // 打印点击位置的坐标
// console.log("点击位置坐标:", clickedPosition);
// }
// });
// 加载模型
BABYLON.SceneLoader.ImportMesh("", "./", "dapeng2.gltf", scene, function(meshes) {
const model = meshes[0]; // 获取加载的第一个模型
// 设置模型可点击
model.isPickable = true;
// 创建图标
const icon = BABYLON.MeshBuilder.CreateSphere("icon", { diameter: 2 }, scene);
icon.material = new BABYLON.StandardMaterial("iconMaterial", scene);
icon.material.emissiveColor = new BABYLON.Color3(1, 0, 0); // 设置图标颜色为红色
icon.isVisible = false; // 默认隐藏图标
// 检测点击事件
scene.onPointerObservable.add((ptrEvt) => {
if (ptrEvt.type === BABYLON.PointerEventTypes.POINTERUP) {
const pickResult = scene.pick(ptrEvt.event.clientX, ptrEvt.event.clientY);
console.log(pickResult, 'ziji*-*-*-*-*-*-*-*-*-*-*-*-*-dianji.html-第200行')
console.log(pickResult.pickedMesh, 'pickResult.pickedMesh*-*-*-*-*-*-*-*-*-*-*-*-*-dianji.html-第200行')
// if (pickResult.hit && pickResult.pickedMesh === model) {
if (pickResult.hit ) {
console.log(pickResult.pickedPoint, '*-*-*-*-*-*-*-*-*-*-*-*-*-dianji.html-第200行')
// 显示图标并设置位置
icon.position = pickResult.pickedPoint.add(new BABYLON.Vector3(0, 0, 0)); // 图标位置在模型上方
icon.isVisible = true;
}
}
});
});
// 运行渲染循环
engine.runRenderLoop(() => {
scene.render();
});
// 处理窗口重置
window.addEventListener('resize', () => {
engine.resize();
});
} catch (error) {
console.error('引擎初始化失败:', error);
}
</script>
</body>
</html>
点击后