BABYLON.SceneLoader.ImportMesh('', 'path of model (This path is sent back from the backEnd)', 'name of model (No name)', scene)
But there is an error. The model cannot be displayed.
Unable to load from https://xxx.com/u/xx/xxxxx/0: importMesh of undefined from undefined version: undefined, exporter version: undefinedimportMesh has failed JSON parse
Because it’s the first time I’ve met Babylon, I may not understand or know how to make it clear, because I really don’t understand.
The code like that:
<template>
<canvas></canvas>
</template>
<script setup lang="ts">
import * as BABYLON from 'babylonjs'
import 'babylonjs-loaders'
onMounted(() => {
const canvas = document.querySelector('canvas') as HTMLCanvasElement
canvas.style.width = '100%'
canvas.style.height = '350px'
// 生成 BABYLON 3D 引擎
var engine = new BABYLON.Engine(canvas, true)
const createScene = function () {
engine.enableOfflineSupport = false
BABYLON.Animation.AllowMatricesInterpolation = true
var scene = new BABYLON.Scene(engine)
scene.clearColor = new BABYLON.Color4(0, 0, 0, 0) //设置画布背景透明
var camera = new BABYLON.ArcRotateCamera(
'camera1',
Math.PI / 2,
Math.PI / 2,
4,
new BABYLON.Vector3(0, 0, 0),
scene
)
camera.attachControl(canvas, true)
camera.lowerRadiusLimit = 2
camera.upperRadiusLimit = 10
camera.wheelDeltaPercentage = 0.01
var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), scene)
light.intensity = 0.6
light.specular = BABYLON.Color3.White()
var light2 = new BABYLON.DirectionalLight(
'dir01',
new BABYLON.Vector3(0, -0.5, -1.0),
scene
)
light2.position = new BABYLON.Vector3(0, 5, 5)
// 阴影
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light2)
shadowGenerator.useBlurExponentialShadowMap = true
shadowGenerator.blurKernel = 32
// The model(That is, the following: props.image) is passed from the backEnd to the frontEnd
watch(
() => props.image,
(newVal) => {},
{ immediate: true }
)
BABYLON.SceneLoader.ImportMesh('', '', props.image, scene, function (meshes) {
console.log(meshes)
})
return scene
}
// 调用 createScene 函数
const scene = createScene()
// 注册渲染循环以重复渲染场景
engine.runRenderLoop(function () {
scene.render()
})
// 监视浏览器/画布调整尺寸事件
window.addEventListener('resize', function () {
engine.resize()
})
})
</script>