简体   繁体   English

THREE.js 线对象未在浏览器中运行

[英]THREE.js Line Object not running in browser

I am new to THREE.js.我是 THREE.js 的新手。 When I open this code in the browser, it only shows a black screen instead of a triangle.当我在浏览器中打开这段代码时,它只显示黑屏而不是三角形。 Please help me figuring out the problem in the code.请帮我找出代码中的问题。

 <!DOCTYPE html> <html> <head> <title>First Line Object</title> <style> body{ margin: 0; } canvas { width: 100%; height: 100%; } </style> </head> <body> <script src="js/three.js"></script> <script> var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var lineGeom = new Geometry(); lineGeom.vertices = [ new THREE.Vector3(-2,-2, 0), new THREE.Vector3(2, -2, 0), new THREE.Vector3(0, 2, 0), new THREE.Vector3(-2,-2, 0) ]; var lineMat = new THREE.LineBasicMaterial({ color: 0xA000A0, linewidth: 2 }); var line = new THREE.Line(lineGeom, lineMat, THREE.LineStrip); scene.add(line); camera.position.z = 5; var render = function() { requestAnimationFrame(render); renderer.render(scene, camera); } render(); </script> </body> </html>

The above code should display a triangle based on the given vertices in the array.上面的代码应该根据数组中给定的顶点显示一个三角形。 But it only shows a black screen and nothing else.但它只显示黑屏,没有别的。

There is a problem with the initialization of the Geometry. Geometry 的初始化有问题。 You have to use the constructor like this你必须像这样使用构造函数

new THREE.Geometry()

This will solve your problem这将解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM