在three.js中绘制3D网络。

huangapple go评论68阅读模式
英文:

Plotting 3d network in three.js

问题

我正在尝试使用three.js生成一个3D图形。

以下是代码部分:

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/three@0.119.0/build/three.min.js"></script>
    <style>
      body { margin: 0; }
      canvas { display: block; }
    </style>
  </head>
  <body>
    <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 node1_geometry = new THREE.BoxGeometry(1, 1, 1);
      var node1_material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
      var node1 = new THREE.Mesh(node1_geometry, node1_material);
      node1.position.set(-2, 4, 1);

      var node2_geometry = new THREE.BoxGeometry(1, 1, 1);
      var node2_material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
      var node2 = new THREE.Mesh(node2_geometry, node2_material);
      node2.position set(2, 4, 1);

      var edge_geometry = new THREE.Geometry();
      edge_geometry.vertices.push(node1.position, node2.position);
      var edge_material = new THREE.LineBasicMaterial({ color: 0xffffff, linewidth: 2 });
      var edge = new THREE.Line(edge_geometry, edge_material);

      scene.add(node1);
      scene.add(node2);
      scene.add(edge);

      camera.position.z = 5;

      var animate = function () {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
      };

      animate();
    </script>
  </body>
</html>

但是这只会显示一个黑色窗口,图形没有显示出来。如何修复这个问题将非常有帮助。

英文:

I am trying to generate a 3d graph using three.js

Code:

&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/three@0.119.0/build/three.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
body { margin: 0; }
canvas { display: block; }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script&gt;
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 node1_geometry = new THREE.BoxGeometry( 1, 1, 1 );
var node1_material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var node1 = new THREE.Mesh( node1_geometry, node1_material );
node1.position.set(-2, 4, 1);
var node2_geometry = new THREE.BoxGeometry( 1, 1, 1 );
var node2_material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
var node2 = new THREE.Mesh( node2_geometry, node2_material );
node2.position.set(2, 4, 1);
var edge_geometry = new THREE.Geometry();
edge_geometry.vertices.push( node1.position, node2.position );
var edge_material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 2 } );
var edge = new THREE.Line( edge_geometry, edge_material );
scene.add( node1 );
scene.add( node2 );
scene.add( edge );
camera.position.z = 5;
var animate = function () {
requestAnimationFrame( animate );
renderer.render( scene, camera );
};
animate();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

But this only gives a black window and the graph is not displayed.
Suggestions on how to fix this will be really helpful.

答案1

得分: 2

要使代码片段运行,你的代码中有一个小问题:

var edge_material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 2 } );

你可以将其修改为:

var edge_material = new THREE.LineDashedMaterial({
  color: 0xffffff,
  dashSize: 2,
  gapSize: 2
});

现在应该能够渲染了,但是仍然会出现黑色背景。问题是你的相机没有对准你创建的内容,所以添加一行代码设置相机位置:

camera.position.y = 5;

现在你应该能够看到你的两个方块和线了。

英文:

To get the snippet to run you have a small problem with you line

var edge_material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 2 } );

You can change this to:

var edge_material = new THREE.LineDashedMaterial({
color: 0xffffff,
dashSize: 2,
gapSize: 2
});

It should now render, but a black background still shows up. The problem now is that your camera is not angled at the content you have created, so add a line with a camera position below.

camera.position.y = 5;

Now you should be able to see your two boxes, and the line.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
scene.background = new THREE.Color(0x333333);
var node1_geometry = new THREE.BoxGeometry(1, 1, 1);
var node1_material = new THREE.MeshBasicMaterial({
color: 0x00ff00
});
var node1 = new THREE.Mesh(node1_geometry, node1_material);
node1.position.set(-2, 4, 1);
var node2_geometry = new THREE.BoxGeometry(1, 1, 1);
var node2_material = new THREE.MeshBasicMaterial({
color: 0xff00ff
});
var node2 = new THREE.Mesh(node2_geometry, node2_material);
node2.position.set(2, 4, 1);
var edge_geometry = new THREE.Geometry();
edge_geometry.vertices.push(node1.position, node2.position);
var edge_material = new THREE.LineDashedMaterial({
color: 0xffffff,
dashSize: 2,
gapSize: 2
});
var edge = new THREE.Line(edge_geometry, edge_material);
scene.add(node1);
scene.add(node2);
scene.add(edge);
camera.position.z = 5;
camera.position.y = 5;
var animate = function() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
animate();

<!-- language: lang-html -->

&lt;html&gt;
&lt;head&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/three@0.119.0/build/three.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
body {
margin: 0;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 15:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定