WebGL - 黑色 Canvas 和控制台错误 Three.JS 和 Blazor
WebGL - Black Canvas and console errors with Three.JS and Blazor
我已经创建了一个 blazor web assembly 项目,并且正在尝试使用 JSInterop 来使用 Three.JS 画一条线,按照他们位于 Here 的教程。我在我的 csproj 文件中安装了 Three.JS 使用 npm 和 webpack 以及预构建事件。
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="npm install" WorkingDirectory="NpmJS" />
<Exec Command="npm run build" WorkingDirectory="NpmJS" />
</Target>
问题是 canvas 呈现为黑色,没有其他任何东西,而控制台显示
几个错误。任何调试帮助将不胜感激。我知道我处于未知领域,因为使用 Three.js 和来自 blazor 的 webgl 功能不全。这是第一个错误:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: (intermediate value).setFromPoints is not a function
TypeError: (intermediate value).setFromPoints is not a function
at Object.drawLine (https://localhost:44370/javascript/threeTutorial.js:21:53)
at https://localhost:44370/_framework/blazor.webassembly.js:1:3942
at new Promise (<anonymous>)
at Object.beginInvokeJSFromDotNet (https://localhost:44370/_framework/blazor.webassembly.js:1:3908)
at Object.w [as invokeJSFromDotNet] (https://localhost:44370/_framework/blazor.webassembly.js:1:64232)
at _mono_wasm_invoke_js_blazor (https://localhost:44370/_framework/dotnet.5.0.10.js:1:190800)
at do_icall (<anonymous>:wasm-function[10596]:0x194e4e)
at do_icall_wrapper (<anonymous>:wasm-function[3305]:0x79df9)
at interp_exec_method (<anonymous>:wasm-function[2155]:0x44ad3)
at interp_runtime_invoke (<anonymous>:wasm-function[7862]:0x12efff)
开发人员控制台中的调试显示 THREE.BufferGeometry() 未定义,错误似乎是因为当时试图在未定义的对象上调用该方法。
我的 Razor 页面代码如下所示:
namespace MyProject.Shared.Components
{
/// <summary>
/// The canvas for browser rendering using Webgl.
/// </summary>
public partial class GameCanvas : MyLayoutComponentBase
{
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("threeTutorial.drawLine");
}
}
}
}
我的Javascript文件:
window.threeTutorial = {
drawLine: function () {
const renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("gameCanvas").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
camera.position.set(0, 0, 100);
camera.lookAt(0, 0, 0);
const scene = new THREE.Scene();
//create a blue LineBasicMaterial
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
const points = [];
points.push(new THREE.Vector3(- 10, 0, 0));
points.push(new THREE.Vector3(0, 10, 0));
points.push(new THREE.Vector3(10, 0, 0));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, material);
scene.add(line);
renderer.render(scene, camera);
}
}
我还在我的 wwwroot.index.html 页面中添加了以下脚本:
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
<script src="javascript/threeTutorial.js"></script>
并且正在我的 NpmJS.src.index.js 文件中导入三个:
import * as THREE from 'three';
犯人849的评论正确:
“尝试 three.js 的更新(最新)版本。IIRC,r79 没有实现 BufferGeometry 的 .setFromPoints()”
我已经创建了一个 blazor web assembly 项目,并且正在尝试使用 JSInterop 来使用 Three.JS 画一条线,按照他们位于 Here 的教程。我在我的 csproj 文件中安装了 Three.JS 使用 npm 和 webpack 以及预构建事件。
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="npm install" WorkingDirectory="NpmJS" />
<Exec Command="npm run build" WorkingDirectory="NpmJS" />
</Target>
问题是 canvas 呈现为黑色,没有其他任何东西,而控制台显示 几个错误。任何调试帮助将不胜感激。我知道我处于未知领域,因为使用 Three.js 和来自 blazor 的 webgl 功能不全。这是第一个错误:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: (intermediate value).setFromPoints is not a function
TypeError: (intermediate value).setFromPoints is not a function
at Object.drawLine (https://localhost:44370/javascript/threeTutorial.js:21:53)
at https://localhost:44370/_framework/blazor.webassembly.js:1:3942
at new Promise (<anonymous>)
at Object.beginInvokeJSFromDotNet (https://localhost:44370/_framework/blazor.webassembly.js:1:3908)
at Object.w [as invokeJSFromDotNet] (https://localhost:44370/_framework/blazor.webassembly.js:1:64232)
at _mono_wasm_invoke_js_blazor (https://localhost:44370/_framework/dotnet.5.0.10.js:1:190800)
at do_icall (<anonymous>:wasm-function[10596]:0x194e4e)
at do_icall_wrapper (<anonymous>:wasm-function[3305]:0x79df9)
at interp_exec_method (<anonymous>:wasm-function[2155]:0x44ad3)
at interp_runtime_invoke (<anonymous>:wasm-function[7862]:0x12efff)
开发人员控制台中的调试显示 THREE.BufferGeometry() 未定义,错误似乎是因为当时试图在未定义的对象上调用该方法。
我的 Razor 页面代码如下所示:
namespace MyProject.Shared.Components
{
/// <summary>
/// The canvas for browser rendering using Webgl.
/// </summary>
public partial class GameCanvas : MyLayoutComponentBase
{
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("threeTutorial.drawLine");
}
}
}
}
我的Javascript文件:
window.threeTutorial = {
drawLine: function () {
const renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("gameCanvas").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
camera.position.set(0, 0, 100);
camera.lookAt(0, 0, 0);
const scene = new THREE.Scene();
//create a blue LineBasicMaterial
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
const points = [];
points.push(new THREE.Vector3(- 10, 0, 0));
points.push(new THREE.Vector3(0, 10, 0));
points.push(new THREE.Vector3(10, 0, 0));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, material);
scene.add(line);
renderer.render(scene, camera);
}
}
我还在我的 wwwroot.index.html 页面中添加了以下脚本:
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
<script src="javascript/threeTutorial.js"></script>
并且正在我的 NpmJS.src.index.js 文件中导入三个:
import * as THREE from 'three';
犯人849的评论正确:
“尝试 three.js 的更新(最新)版本。IIRC,r79 没有实现 BufferGeometry 的 .setFromPoints()”