zhaoxing
2022-08-22 74bb50ce93d0c9cd20a0b84f63db46f433a39e91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * @author Mugen87 / https://github.com/Mugen87
 */
 
( function() {
 
    // ConvexGeometry
 
    function ConvexGeometry( points ) {
 
        THREE.Geometry.call( this );
 
        this.type = 'ConvexGeometry';
 
        this.fromBufferGeometry( new ConvexBufferGeometry( points ) );
        this.mergeVertices();
 
    }
 
    ConvexGeometry.prototype = Object.create( THREE.Geometry.prototype );
    ConvexGeometry.prototype.constructor = ConvexGeometry;
 
    // ConvexBufferGeometry
 
    function ConvexBufferGeometry( points ) {
 
      THREE.BufferGeometry.call( this );
 
        this.type = 'ConvexBufferGeometry';
 
      // buffers
 
      var vertices = [];
      var normals = [];
 
      // execute QuickHull
 
        if ( THREE.QuickHull === undefined ) {
 
            console.error( 'THREE.ConvexBufferGeometry: ConvexBufferGeometry relies on THREE.QuickHull' );
 
        }
 
      var quickHull = new THREE.QuickHull().setFromPoints( points );
 
      // generate vertices and normals
 
      var faces = quickHull.faces;
 
      for ( var i = 0; i < faces.length; i ++ ) {
 
        var face = faces[ i ];
        var edge = face.edge;
 
        // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
 
        do {
 
          var point = edge.head().point;
 
          vertices.push( point.x, point.y, point.z );
          normals.push( face.normal.x, face.normal.y, face.normal.z );
 
          edge = edge.next;
 
        } while ( edge !== face.edge );
 
      }
 
      // build geometry
      if (THREE.Float32BufferAttribute) {
          this.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
          this.addAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
 
      } else {
          this.addAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3));
          this.addAttribute('normal', new THREE.BufferAttribute(new Float32Array(normals), 3));
      }
    
    }
 
    ConvexBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
    ConvexBufferGeometry.prototype.constructor = ConvexBufferGeometry;
 
    // export
 
    THREE.ConvexGeometry = ConvexGeometry;
    THREE.ConvexBufferGeometry = ConvexBufferGeometry;
 
} ) ();