소스 검색

Merge pull request #20666 from gkjohnson/rounded-box-normals

RoundedBoxBufferGeometry: Fix normals, enforce odd segments
Mr.doob 4 년 전
부모
커밋
8e9010c5b4
1개의 변경된 파일17개의 추가작업 그리고 0개의 파일을 삭제
  1. 17 0
      examples/jsm/geometries/RoundedBoxBufferGeometry.js

+ 17 - 0
examples/jsm/geometries/RoundedBoxBufferGeometry.js

@@ -41,8 +41,18 @@ class RoundedBoxBufferGeometry extends BoxBufferGeometry {
 
 	constructor( width = 1, height = 1, depth = 1, segments = 1, radius = 1 ) {
 
+		// ensure segments is odd so we have a plane connecting the rounded corners
+		segments = segments % 2 === 0 ? segments + 1 : segments;
+
 		super( 1, 1, 1, segments, segments, segments );
 
+		// if we just have one segment we'r the same as a regular box
+		if ( segments === 1 ) {
+
+			return;
+
+		}
+
 		const geometry2 = this.toNonIndexed();
 
 		this.index = null;
@@ -63,6 +73,7 @@ class RoundedBoxBufferGeometry extends BoxBufferGeometry {
 
 		const faceTris = positions.length / 6;
 		const faceDirVector = new Vector3();
+		const halfSegmentSize = 0.5 / segments;
 
 		for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
 
@@ -73,6 +84,12 @@ class RoundedBoxBufferGeometry extends BoxBufferGeometry {
 			positions[ i + 1 ] = box.y * Math.sign( position.y ) + normal.y * radius;
 			positions[ i + 2 ] = box.z * Math.sign( position.z ) + normal.z * radius;
 
+			normal.copy( position );
+			normal.x -= Math.sign( normal.x ) * halfSegmentSize;
+			normal.y -= Math.sign( normal.y ) * halfSegmentSize;
+			normal.z -= Math.sign( normal.z ) * halfSegmentSize;
+			normal.normalize();
+
 			normals[ i + 0 ] = normal.x;
 			normals[ i + 1 ] = normal.y;
 			normals[ i + 2 ] = normal.z;