gpt4 book ai didi

javascript - 无法为盒子设置纹理(html5/js/babylon.js)

转载 作者:行者123 更新时间:2023-12-02 22:20:44 25 4
gpt4 key购买 nike

我正在尝试 Babylon.js。我想设置地板纹理,但不起作用。我收到以下错误:

enter image description here

“未捕获的类型错误:_this.getScene 不是函数”(用于 Google 搜索)

我真的不明白,youtube 教程让它看起来很简单。

源代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
#canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="https://cdn.babylonjs.com/babylon.max.js"></script>
<script>
window.addEventListener('DOMContentLoaded', function () {
var canvas = document.getElementById('canvas');
var engine = new BABYLON.Engine(canvas, true);
var createScene = function () {
var scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3.White();

var floor = BABYLON.MeshBuilder.CreateBox("floor", { height: 25, width: 800, depth: 900 }, scene);
floor.position.y = -162.5;
floor.position.x = 100;
floor.position.z = 400;

var camera = new BABYLON.ArcRotateCamera(
'camera1',
BABYLON.Tools.ToRadians(45),
BABYLON.Tools.ToRadians(45),
1000.0,
new BABYLON.Vector3(0, 50, 400),
scene);
camera.attachControl(canvas, true);

var light = new BABYLON.PointLight("pointlight", new BABYLON.Vector3(800, 700, 1000), scene);
light.diffuse = new BABYLON.Color3(1, 1, 1);


var floormaterial = new BABYLON.StandardMaterial("floormaterial", scene);
floormaterial.diffuseTexture = BABYLON.Texture("floor.png", scene);
floor.material = floormaterial;

return scene;
};

var scene = createScene();
engine.runRenderLoop(function () {
scene.render();
});

});
</script>
</body>
</html>

如果我删除“floormaterial.diffuseTexture = BABYLON.Texture("floor.png", scene);”一切正常。

最佳答案

您缺少纹理之前的new。该行应该是:

floormaterial.diffuseTexture = new BABYLON.Texture("floor.png", scene);

关于javascript - 无法为盒子设置纹理(html5/js/babylon.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59240048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com