Files
apt-nl-map/static/Magic4/js/three.js-dev/docs/manual/zh/introduction/Drawing-lines.html
2024-12-04 10:21:04 +08:00

67 lines
2.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<base href="../../../" />
<script src="page.js"></script>
<link type="text/css" rel="stylesheet" href="page.css" />
</head>
<body>
<h1>画线([name]</h1>
<div>
<p>
假设你将要画一个圆或者画一条线,而不是一个线框模型,或者说不是一个[page:Mesh](网格)。
第一步我们要做的,是设置好[page:WebGLRenderer renderer](渲染器)、[page:Scene scene](场景)和[page:Camera camera](相机)-(如果对这里所提到的东西,还不了解,请阅读本手册第一章“创建一个场景 - Creating a scene”
</p>
<p>这是我们将要用到的代码:</p>
<code>
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.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();
</code>
<p>
接下来我们要做的事情是定义一个材质。对于线条来说,我们能使用的材质只有[page:LineBasicMaterial] 或者 [page:LineDashedMaterial]。
</p>
<code>
//create a blue LineBasicMaterial
const material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
</code>
<p>
定义好材质之后我们需要一个带有一些顶点的geometry几何体
</p>
<code>
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 );
</code>
<p>注意,线是画在每一对连续的顶点之间的,而不是在第一个顶点和最后一个顶点之间绘制线条(线条并未闭合)。</p>
<p>既然我们已经有了能够画两条线的点和一个材质,那我们现在就可以将他们组合在一起,形成一条线。</p>
<code>
const line = new THREE.Line( geometry, material );
</code>
<p>剩下的事情就是把它添加到场景中,并调用[page:WebGLRenderer.render render](渲染)函数。</p>
<code>
scene.add( line );
renderer.render( scene, camera );
</code>
<p>你现在应当已经看到了一个由两条蓝线组成的、指向上的箭头。</p>
</div>
</body>
</html>