How can I create a 3D cubic-bezier curved triangle from 3D points in Three.js?

后端 未结 3 1276
Happy的楠姐
Happy的楠姐 2021-01-07 01:28

Following this topic, I am trying to generate a 3D curved triangle as a NURBS surface, but I don\'t understand how to set up my 3D points to do that.

Here is the cur

相关标签:
3条回答
  • 2021-01-07 01:44

    Here is the way how you can draw Bezier Triangle (snippet below) - algorithm is in Geometry class. Number of triangles in one side of the triangle you set in constructor. In code I made hard separation between algorithm/calculations (Geometry class) and drawing code (Draw class).

    For bezier triangle we need to use 10 control points (9 for edges and one for "plane") like in below picture (src here ):

    In this code, we don't use normals, and b points names are changed to p (eg. b003 to p003). We use following formula (for cubic Bezier triangles n=3)

    Where p_ijk is control point (for n=3 above sum has 10 elements so we have 10 control points), and where B^n_ijk(r,s,t) are Bernstein polynomials defined for i,j,k>=0 and i+j+k=n

    or 0 in other case. The domain of r,s,t in barycentric coordinates (where r,s,t are real numbers from [0, 1] and r+s+t=1) and where r=(r=1, s=t=0), s=(s=1, r=t=0), t=(t=1, r=s=0) looks as follows (the black points - we divide each triangle side to 5 parts - but we can change it to any number)

    We calculate this reqular positions for black domain dots in method barycentricCoords(n) and we define which point create which triangles in method genTrianglesIndexes(n) in Geometry class. However you can change this points positions and density to any (inside triangle) to get different surface-triangle division. Below is snippet which shows domain in 2D

    let pp= ((s='.myCanvas',c=document.querySelector(s),ctx=c.getContext('2d'),id=ctx.createImageData(1,1)) => (x,y,r=0,g=0,b=0,a=255)=>(id.data.set([r,g,b,a]),ctx.putImageData(id, x, y),c))()
    
    
    cr=[255,0,0,255];
    cg=[0,255,0,255];
    cb=[0,0,255,255];
    
    w=400;
    h=400;
    
    const p1=[0,h-1];
    const p2=[w-1,h-1];
    const p3=[w/2,0];
    
    mainTriangle=[p1,p2,p3];
    //mainTriangle.map(p => pp(...p,...cr));
    
    let n=5;
    let points=[];
    
    function calcPoint(p1,p2,p3,r,s,t) {
      const px=p1[0]*r + p2[0]*s + p3[0]*t;
      const py=p1[1]*r + p2[1]*s + p3[1]*t;
      return [px,py];
    }
    
    // barycentric coordinates r,s,t of point in triangle
    // the points given from triangle bottom to top line by line
    // first line has n+1 pojnts, second has n, third n-1
    // coordinates has property r+s+t=1
    function barycentricCoords(n) {
      let rst=[];
      for(let i=0; i<=n; i++) for(let j=0; j<=n-i; j++) {
        s=(j/n);
        t=(i/n);    
        r=1-s-t;
        rst.push([r,s,t]);    
      }
      return rst;
    }
    
    // Procedure calc indexes for each triangle from 
    // points list (in format returned by barycentricCoords(n) )
    function genTrianglesIndexes(n) {
      let st=0; 
      let m=n;  
      let triangles=[];
    
      for(let j=n; j>0; j--) {    
        for(let i=0; i<m; i++) {    
          triangles.push([st+i, st+i+1, st+m+i+1]);
          if(i<m-1) triangles.push([st+i+1, st+m+i+2, st+m+i+1 ]);
        }
        m--;
        st+=j+1;  
      }
      
      return triangles;
    }
    
    function drawLine(p1,p2,c) {
      let n=Math.max(Math.abs(p1[0]-p2[0]),Math.abs(p1[1]-p2[1]))/2;
    	for(let i=0; i<=n; i++) {
      	let s=i/n;
        let x=p1[0]*s + p2[0]*(1-s);
        let y=p1[1]*s + p2[1]*(1-s);
        pp(x,y,...c);
      }
    }
    
    function drawTriangle(p1,p2,p3,c) {
    	drawLine(p1,p2,c);
      drawLine(p2,p3,c);
      drawLine(p3,p1,c);
    }
    
    // Bernstein Polynomial, i+j+k=n
    function bp(n,i,j,k, r,s,t) {
      const f=x=>x?f(x-1)*x:1 // number fractional f(4)=1*2*3*4=24
      
      return r**i * s**j * t**k * f(n) / (f(i)*f(j)*f(k));  
    }
    
    //drawTriangle(...mainTriangle,cr); // draw main triangle
    
    let bar=barycentricCoords(n);  // each domain point barycentric coordinates
    
    let ti=genTrianglesIndexes(n); // indexes in bar for each triangle
    
    // triangles calculated to cartesian coordinate system
    let triangles = ti.map(tr=> tr.map(x=>calcPoint(...mainTriangle,...bar[x]) ) ); 
    
    triangles.map(t => drawTriangle(...t, cg));
    
    // domain points calculated to cartesian coordinate system (for draw)
    let dp = bar.map(x=> calcPoint(...mainTriangle,...x) );
    
    // draw black dots (4 pixels for each dot)
    dp.map(x=> pp(x[0],x[1]) )
    dp.map(x=> pp(x[0],x[1]-1) )
    dp.map(x=> pp(x[0]-1,x[1]) )
    dp.map(x=> pp(x[0]-1,x[1]-1) )
    <canvas class="myCanvas" width=400 height=400 style="background: white"></canvas>

    Below is final snippet with 3D bezier cubic triangle ( algorithm starts in method genTrianglesForCubicBezierTriangle(n, controlPoints) in Geometry class) - (caution: It is strange, but in SO snippets after first run you will NOT see lines, and you need reload page and run it again to see triangles-lines)

    ///////////////////////////////////////////////////////
    // THIS PART/CLASS IS FOR ALGORITHMS AND CALCULATIONS
    ///////////////////////////////////////////////////////
    class Geometry {
    
      constructor() { this.init(); } 
    
      init(n) {
        this.pts = [
          { x:-16, y: -8, z:0,  color:0xcc0000 }, // p003 RED
          { x:8,   y:-12, z:0,  color:0x888888 }, // p201
          { x:-8,  y:-12, z:0,  color:0x999999 }, // p102    
          { x:16,  y:-8,  z:0,  color:0x00cc00 }, // p300 GREEN
          { x:12,  y:-6,  z:-8, color:0x777777 }, // p210
          { x:8,   y:6,   z:-8, color:0x666666 }, // p120
          { x:0,   y:12,  z:0,  color:0x0000cc }, // p030 BLUE
          { x:-8,  y:6,   z:-8, color:0x555555 }, // p021
          { x:-12, y:-6,  z:-8, color:0x444444 }, // p012
          { x:0,   y:0,   z:8,  color:0xffff00 }, // p111 YELLOW (plane control point)
        ];
        
        this.mainTriangle = [this.pts[0],this.pts[3],this.pts[6]];
        
        this.bezierCurvesPoints = [
        	[ this.pts[0], this.pts[2], this.pts[1], this.pts[3] ],
            [ this.pts[3], this.pts[4], this.pts[5], this.pts[6] ],
            [ this.pts[6], this.pts[7], this.pts[8], this.pts[0] ]
        ];
        
        //this.triangles = [
        // { points: [this.pts[0], this.pts[1], this.pts[2]], color: null }, // wireframe
        // { points: [this.pts[1], this.pts[2], this.pts[3]], color: 0xffff00 } // yellow
        //]
        
        this.triangles = this.genTrianglesForCubicBezierTriangle(25, this.pts);
      }
      
      // n = number of triangles per triangle side
      genTrianglesForCubicBezierTriangle(n, controlPoints) {
        let bar= this.barycentricCoords(n);     // domain in barycentric coordinats   
        let ti = this.genTrianglesIndexes(n);   // indexes of triangles (in bar array)
            
        let val= bar.map(x => this.calcCubicBezierTriangleValue(controlPoints,...x));  // Calc Bezier triangle vertex for each domain (bar) point    
        let tv= ti.map(tr=> tr.map(x=>val[x]) );         // generate triangles using their indexes (ti) and val    
        return tv.map(t=> ({ points: t, color: null}) ); // map triangles to proper format (color=null gives wireframe)
        
        
        // Generate domain triangles
        //let td= ti.map(tr=> tr.map(x=>this.calcPointFromBar(...this.mainTriangle,...bar[x]) ) );     
        //this.trianglesDomain = td.map(t=> ({ points: t, color: null}) );
      }
      
      // more: https://www.mdpi.com/2073-8994/8/3/13/pdf
      // Bézier Triangles with G2 Continuity across Boundaries
      // Chang-Ki Lee, Hae-Do Hwang and Seung-Hyun Yoon
      calcCubicBezierTriangleValue(controlPoints, r,s,t ) {
        let p = controlPoints, b=[];  
        b[0]= this.bp(0,0,3,r,s,t); // p[0]=p003
        b[1]= this.bp(2,0,1,r,s,t); // p[1]=p201 
        b[2]= this.bp(1,0,2,r,s,t); // p[2]=p102
        b[3]= this.bp(3,0,0,r,s,t); // p[3]=p300
        b[4]= this.bp(2,1,0,r,s,t); // p[4]=p210
        b[5]= this.bp(1,2,0,r,s,t); // p[5]=p120
        b[6]= this.bp(0,3,0,r,s,t); // p[6]=p030
        b[7]= this.bp(0,2,1,r,s,t); // p[7]=p021
        b[8]= this.bp(0,1,2,r,s,t); // p[8]=p012
        b[9]= this.bp(1,1,1,r,s,t); // p[9]=p111
        
        let x=0, y=0, z=0;
        for(let i=0; i<=9; i++) {
          x+=p[i].x*b[i];
          y+=p[i].y*b[i];
          z+=p[i].z*b[i];
        }
        return { x:x, y:y, z:z };
      }
      
      // Bernstein Polynomial degree n, i+j+k=n
      bp(i,j,k, r,s,t, n=3) {
        const f=x=>x?f(x-1)*x:1 // number fractional f(4)=1*2*3*4=24    
        return r**i * s**j * t**k * f(n) / (f(i)*f(j)*f(k));  
      }
      
      coordArrToObj(p) { return { x:p[0], y:p[1], z:p[2] } } 
      
      // Calc cartesian point from barycentric coords system
      calcPointFromBar(p1,p2,p3,r,s,t) {  
        const px=p1.x*r + p2.x*s + p3.x*t;
        const py=p1.y*r + p2.y*s + p3.y*t;
        const pz=p1.z*r + p2.z*s + p3.z*t;       
        return { x:px, y:py,  z:pz};
      }
    
      // barycentric coordinates r,s,t of point in triangle
      // the points given from triangle bottom to top line by line
      // first line has n+1 pojnts, second has n, third n-1
      // coordinates has property r+s+t=1
      barycentricCoords(n) {
        let rst=[];
        for(let i=0; i<=n; i++) for(let j=0; j<=n-i; j++) {
          let s=(j/n);
          let t=(i/n);    
          let r=1-s-t;
          rst.push([r,s,t]);    
        }
        return rst;
      }
    
      // Procedure calc indexes for each triangle from 
      // points list (in format returned by barycentricCoords(n) )
      genTrianglesIndexes(n) {
        let st=0; 
        let m=n;  
        let triangles=[];
    
        for(let j=n; j>0; j--) {    
          for(let i=0; i<m; i++) {    
            triangles.push([st+i, st+i+1, st+m+i+1]);
            if(i<m-1) triangles.push([st+i+1, st+m+i+2, st+m+i+1 ]);
          }
          m--;
          st+=j+1;  
        }
    
        return triangles;
      }
      
      // This procedures are interface for Draw class 
      getPoints() { return this.pts }
      getTriangles() { return this.triangles }
      getBezierCurves() { return this.bezierCurvesPoints; }
    }
    
    
    ///////////////////////////////////////////////
    // THIS PART IS FOR DRAWING
    ///////////////////////////////////////////////
    
    // init tree js and draw geometry objects
    class Draw {
    
      constructor(geometry) { this.init(geometry); }
      
      initGeom() {
      	this.geometry.getPoints().forEach(p=> this.createPoint(p));
        this.geometry.getTriangles().forEach(t=> this.createTriangle(t));
        
        this.geometry.getBezierCurves().forEach(c=> this.createEdge(...c));
      }
    
      init(geometry) {
        this.geometry = geometry;
        this.W = 480,
        this.H = 400,
        this.DISTANCE = 100 ;
        this.PI = Math.PI,
        
      
        this.renderer = new THREE.WebGLRenderer({
          canvas : document.querySelector('canvas'),
          antialias : true,
          alpha : true
        }),
        this.camera = new THREE.PerspectiveCamera(25, this.W/this.H),
        this.scene = new THREE.Scene(),
        this.center = new THREE.Vector3(0, 0, 0),
    
    		this.pts = [] ;
        
        this.renderer.setClearColor(0x000000, 0) ;
    
        this.renderer.setSize(this.W, this.H) ;
        // camera.position.set(-48, 32, 80) ;
        this.camera.position.set(0, 0, this.DISTANCE) ;
        this.camera.lookAt(this.center) ;
        
        this.initGeom();
        
        this.azimut = 0;
        this.pitch = 90;
        this.isDown = false;
        this.prevEv = null;
    
        
    
        this.renderer.domElement.onmousedown = e => this.down(e) ;
        window.onmousemove = e => this.move(e) ;
        window.onmouseup = e => this.up(e) ;
    
        this.renderer.render(this.scene, this.camera) ;
        
      }
        
      createPoint(p) {
        let {x, y, z, color} = p;
    		let pt = new THREE.Mesh(
          new THREE.SphereGeometry(1, 10, 10),
          new THREE.MeshBasicMaterial({ color })
        ) ;
        pt.position.set(x, y, z) ;
        pt.x = x ;
        pt.y = y ;
        pt.z = z ;
        this.pts.push(pt) ;
        
        this.scene.add(pt) ;
    	}
      
      createTriangle(t) {    
        var geom = new THREE.Geometry();
        var v1 = new THREE.Vector3(t.points[0].x, t.points[0].y, t.points[0].z);
        var v2 = new THREE.Vector3(t.points[1].x, t.points[1].y, t.points[1].z);
        var v3 = new THREE.Vector3(t.points[2].x, t.points[2].y, t.points[2].z);
    
        geom.vertices.push(v1);
        geom.vertices.push(v2);
        geom.vertices.push(v3);
            
        let material = new THREE.MeshNormalMaterial({wireframe: true,}) 
        if(t.color != null) material = new THREE.MeshBasicMaterial( { 
        	color: t.color,
          side: THREE.DoubleSide,
          } );
        
    
        geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
        geom.computeFaceNormals();
    
        var mesh= new THREE.Mesh( geom, material);
        this.scene.add(mesh) ;
      }
      
      createEdge(pt1, pt2, pt3, pt4) {
     
    		let curve = new THREE.CubicBezierCurve3(
              new THREE.Vector3(pt1.x, pt1.y, pt1.z),
              new THREE.Vector3(pt2.x, pt2.y, pt2.z),
              new THREE.Vector3(pt3.x, pt3.y, pt3.z),
              new THREE.Vector3(pt4.x, pt4.y, pt4.z),
            ),
        		mesh = new THREE.Mesh(
              new THREE.TubeGeometry(curve, 8, 0.5, 8, false),
              new THREE.MeshBasicMaterial({
                color : 0x203040
              })
            ) ;
            
        this.scene.add(mesh) ;
    }
      
      down(de) {
            this.prevEv = de ;
            this.isDown = true ;
        }
    
      move(me) {
        if (!this.isDown) return ;
    
        this.azimut -= (me.clientX - this.prevEv.clientX) * 0.5 ;
        this.azimut %= 360 ;
        if (this.azimut < 0) this.azimut = 360 - this.azimut ;
    
        this.pitch -= (me.clientY - this.prevEv.clientY) * 0.5 ;
        if (this.pitch < 1) this.pitch = 1 ;
        if (this.pitch > 180) this.pitch = 180 ;
    
        this.prevEv = me ;
    
        let theta = this.pitch / 180 * this.PI,
            phi = this.azimut / 180 * this.PI,
            radius = this.DISTANCE ;
    
        this.camera.position.set(
          radius * Math.sin(theta) * Math.sin(phi),
          radius * Math.cos(theta),
          radius * Math.sin(theta) * Math.cos(phi),
        ) ;
        this.camera.lookAt(this.center) ;
    
        this.renderer.render(this.scene, this.camera) ;
      }
    
      up(ue) {
        this.isDown = false ;
      }
    }
    
    // SYSTEM SET UP
    let geom= new Geometry();
    let draw = new Draw(geom);
    body {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background: #1c2228;
      overflow: hidden;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/101/three.min.js"></script>
    
    <canvas></canvas>

    Fiddle version is here . I put info in comments but algorithm is complicated and if you have questions - ask them as comments - I will answer.

    0 讨论(0)
  • 2021-01-07 01:45

    In your code you use NURBSSurface function from NURBSSurface.js file, that function uses NURBSUtils.calcSurfacePoint function from NURBSUtils.js file. But the calcSurfacePoint calculate point for standard NUBRB surface where where parameter are from rectangle (u,v) wiki.

    You will not generate "3D cubic bezier triangle" in this way - to do this you need write your own code which will use bezier-triangle formulas (where the input parameters are triangle points in Barycentric_coordinate_system).

    0 讨论(0)
  • 2021-01-07 01:53

    I modified Kamil Kiełczewski's code and separated it into 2 classes:

    1. BarycentricBufferGeometry based on ParametricBufferGeometry
    2. BezierTriangle based on NURBSSurface

    Now it functions similar to NURBSSurface.js and is more efficient.

    BarycentricBufferGeometry.js

    import { BufferGeometry, Float32BufferAttribute, Vector3 } from './three.module.js';
    
    class BarycentricBufferGeometry extends BufferGeometry {
    
        constructor(func, slices) {
    
            super();
    
            this.type = 'BezierTriangleGeometry';
    
            this.parameters = {
                func: func,
                slices: slices
            };
    
            // buffers
            const indices = [];
            const vertices = [];
            const normals = [];
            const uvs = [];
    
            const EPS = 0.00001;
    
            const normal = new Vector3();
    
            const p0 = new Vector3(), p1 = new Vector3();
            const pu = new Vector3(), pv = new Vector3();
    
            if (func.length < 3) {
    
                console.error('THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter.');
    
            }
    
            // generate vertices, normals and uvs
            for (let i = 0; i <= slices; i++) {
    
                for (let j = 0; j <= slices - i; j++) {
    
                    const u = j / slices;
                    const v = i / slices;
    
                    // vertex
                    func(u, v, p0);
                    vertices.push(p0.x, p0.y, p0.z);
    
                    // normal
                    // approximate tangent vectors via finite differences
                    if (u - EPS >= 0) {
    
                        func(u - EPS, v, p1);
                        pu.subVectors(p0, p1);
    
                    } else {
    
                        func(u + EPS, v, p1);
                        pu.subVectors(p1, p0);
    
                    }
    
                    if (v - EPS >= 0) {
    
                        func(u, v - EPS, p1);
                        pv.subVectors(p0, p1);
    
                    } else {
    
                        func(u, v + EPS, p1);
                        pv.subVectors(p1, p0);
    
                    }
    
                    // cross product of tangent vectors returns surface normal
                    normal.crossVectors(pu, pv).normalize();
                    normals.push(normal.x, normal.y, normal.z);
    
                    // uv
                    uvs.push(u, v);
    
                }
    
            }
    
            // generate indices
            let st = 0;
            let m = slices;
    
            for (let j = slices; j > 0; j--) {
    
                for (let i = 0; i < m; i++) {
    
                    const a = st + i;
                    const b = st + i + 1;
                    const c = st + i + 1 + m;
    
                    indices.push(a, b, c);
    
                    if (i < m - 1)
                        indices.push(st + i + 1, st + m + i + 2, st + m + i + 1);
                }
    
                m = m - 1;
                st += j + 1;
            }
    
            // build geometry
            this.setIndex(indices);
            this.setAttribute('position', new Float32BufferAttribute(vertices, 3));
            this.setAttribute('normal', new Float32BufferAttribute(normals, 3));
            this.setAttribute('uv', new Float32BufferAttribute(uvs, 2));
    
        }
    }
    
    // BarycentricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
    ;
    
    
    export { BarycentricBufferGeometry };
    

    BezierTriangle.js

    class BezierTriangle {
    
        constructor(controlPoints) {
    
            this.controlPoints = controlPoints;
        }
    
        static bp(i, j, k, r, s, t, n = 3) {
            const f = x => x ? f(x - 1) * x : 1;
            return r ** i * s ** j * t ** k * f(n) / (f(i) * f(j) * f(k));
        }
    
        static calcSurfacePoint(p, u, v, target) {
    
            const t = 1 - u - v;
            let b = [];
    
            b[0] = BezierTriangle.bp(0, 0, 3, u, v, t);
            b[1] = BezierTriangle.bp(1, 0, 2, u, v, t);
            b[2] = BezierTriangle.bp(2, 0, 1, u, v, t);
            b[3] = BezierTriangle.bp(3, 0, 0, u, v, t);
            b[4] = BezierTriangle.bp(2, 1, 0, u, v, t);
            b[5] = BezierTriangle.bp(1, 2, 0, u, v, t);
            b[6] = BezierTriangle.bp(0, 3, 0, u, v, t);
            b[7] = BezierTriangle.bp(0, 2, 1, u, v, t);
            b[8] = BezierTriangle.bp(0, 1, 2, u, v, t);
            b[9] = BezierTriangle.bp(1, 1, 1, u, v, t);
    
            let x = 0,
                y = 0,
                z = 0;
    
            for (let i = 0; i < 10; i++) {
                x += p[i].x * b[i];
                y += p[i].y * b[i];
                z += p[i].z * b[i];
            }
    
            target.set(x, y, z);
        }
    
        getPoint(u, v, target) {
    
            BezierTriangle.calcSurfacePoint(this.controlPoints, u, v, target);
        }
    }
    
    
    export { BezierTriangle };
    

    Example:

    import * as THREE from './three.module.js';
    
    import { BarycentricBufferGeometry } from './BarycentricBufferGeometry.js';
    import { BezierTriangle } from './BezierTriangle.js';
    
    
    //setup
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, .01, 10000);
    camera.position.set(2, 2, 6)
    
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
    
    
    // bezier triangle points
    const points = [
        { x: 0, y: 0, z: 0, c: 'red' },
    
        { x: 0, y: 1, z: 0, c: 'grey' },
        { x: 0, y: 2, z: 0, c: 'grey' },
        { x: 0, y: 3, z: 1, c: 'green' },
    
        { x: 1, y: 3, z: 1, c: 'grey' },
        { x: 2, y: 3, z: 1, c: 'grey' },
        { x: 3, y: 3, z: 2, c: 'blue' },
    
        { x: 2, y: 2, z: 0, c: 'grey' },
        { x: 1, y: 1, z: 0, c: 'grey' },
    
        { x: 1, y: 2, z: 0, c: 'yellow' },
    ];
    
    // add some colored spheres to help identify points
    points.forEach(p => {
        const sphere = new THREE.Mesh(
            new THREE.SphereBufferGeometry(.1, 32, 32),
            new THREE.MeshBasicMaterial({ color: p.c ? p.c : 'white' })
        );
        sphere.position.set(p.x, p.y, p.z);
        scene.add(sphere);
    });
    
    // draw bezier triangle
    const triangle = new BezierTriangle(points);
    
    function getSurfacePoint(u, v, target) {
        return triangle.getPoint(u, v, target);
    }
    
    const geometry = new BarycentricBufferGeometry(getSurfacePoint, 3);
    const material = new THREE.MeshBasicMaterial({ color: 'gold', wireframe: true });
    const mesh = new THREE.Mesh(geometry, material);
    
    scene.add(mesh);
    
    renderer.render(scene, camera);
    
    0 讨论(0)
提交回复
热议问题