简体   繁体   English

Three.js - 自定义网格上的奇怪线条

[英]Three.js - Strange lines on custom mesh

I'm trying to make a custom mesh in order to have a lot of customizable planes in the scene.我正在尝试制作自定义网格,以便在场景中拥有许多可自定义的平面。

Here is some code:这是一些代码:

geometry = new THREE.BufferGeometry();

geometry.addAttribute( 'position',  new THREE.BufferAttribute( vertexPositions, 3 ).setDynamic( true ) );
geometry.addAttribute( 'color',     new THREE.BufferAttribute( colors, 3 ).setDynamic( true ) );
geometry.addAttribute( 'opacity',   new THREE.BufferAttribute( opacity, 1 ).setDynamic( true ) );

// Initialize material
var material = new THREE.ShaderMaterial( { vertexColors: THREE.VertexColors, transparent: true, vertexShader: vertexShader, fragmentShader: fragmentShader } );

// Create something like a Wireframe of the planes
var Pgeometry = new THREE.BufferGeometry();
Pgeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( ApointsPositions ), 3 ).setDynamic( true ) );
var points = new THREE.LineSegments( Pgeometry, new THREE.PointsMaterial( { color: 0x353535 } ) );
scene.add( points );

// Create planes and add to scene
planeMeshes = new THREE.Mesh( geometry, material );
planeMeshes.setDrawMode( THREE.TriangleStripDrawMode );
scene.add( planeMeshes );

My custom material is working fine and let me to use opacity on every vertex.我的自定义材质运行良好,让我可以在每个顶点上使用不透明度。 Every plane is created and everything works fine.每架飞机都已创建,一切正常。

The problem is that, when I render the scene, there are some strange lines on every planes row, here's a full working Codepen: Codepen link问题是,当我渲染场景时,每个平面行上都有一些奇怪的线条,这是一个完整的 Codepen: Codepen 链接

Do you see those diagonal lines there?你看到那里的那些对角线了吗? Are you able to tell me what's happening?你能告诉我发生了什么吗? I didn't find their origin.我没有找到它们的来源。

Thank you in advance!先感谢您!

The reason you're seeing diagonals is because you're using THREE.TriangleStripDrawMode , so the rightmost triangles share vertices with the leftmost triangles of the next row.您看到对角线的原因是因为您正在使用THREE.TriangleStripDrawMode ,因此最右侧的三角形与下一行最左侧的三角形共享顶点。 You're seeing a really stretched out triangle cut across.你会看到一个真正拉长的三角形被切开。

To solve this, get rid of the line where you're assigning TriangleStripDrawMode , which will go back to the default of three vertices per triangle (no sharing of vertices).为了解决这个问题,去掉你分配TriangleStripDrawMode的那一行,它会恢复到每个三角形三个顶点的默认值(不共享顶点)。

Problem 2:问题2:

The first triangle on each iteration is being drawn in a clockwise order, but the second triangle on each iteration is being drawn in a counter-clockwise order, so you will not see the second one unless you change the vertex order to be clockwise.每次迭代的第一个三角形是按顺时针顺序绘制的,但每次迭代的第二个三角形是按逆时针顺序绘制的,因此除非将顶点顺序更改为顺时针,否则您将看不到第二个三角形。

Here's how you have it in your Codepen:以下是您在 Codepen 中使用它的方式:

// Second tri is drawn counter-clockwise
AvertexPositions.push( x,        y + 60, 0 ); //
AvertexPositions.push( x + 80,   y + 60, 0 ); // Second triangle of a plane
AvertexPositions.push( x + 80,   y,      0 );

This is how it should be:应该是这样:

// Now, it is clockwise, matching first tri
AvertexPositions.push( x,        y + 60, 0 ); //
AvertexPositions.push( x + 80,   y,      0 ); //
AvertexPositions.push( x + 80,   y + 60, 0 ); // Second triangle of a plane

It is important to draw all your triangles in the same winding order.以相同的缠绕顺序绘制所有三角形很重要。 Otherwise, some will be facing forward, and some will be facing backward.否则,有些人会面朝前,有些人会朝后。 You can choose which side gets rendered, or render both sides with Materials.Side您可以选择渲染哪一侧,或使用Materials.Side渲染两侧

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

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