メインコンテンツまでスキップ

Cylinder

ライブエディター

function Canvas() {
      const geo = cylinder({ radiusTop: 0.5, radiusBottom: 0.5, height: 1 })
      const mat = rotate3dX(iDrag.y).mul(rotate3dY(iDrag.x))
      const gl = useGL({
              isWebGL: true,
              isDepth: true,
              count: geo.count,
              vertex: vec4(mat.mul(geo.vertex), 1),
              fragment: vec4(varying(geo.normal), 1),
      })
      return <canvas ref={gl.ref} />
}
結果
Loading...

Cylinder Props

radiusTopRadius of the cylinder at the top.
Default is 1.
radiusBottomRadius of the cylinder at the bottom.
Default is 1.
heightHeight of the cylinder.
Default is 1.
radialSegmentsNumber of segmented faces around the circumference.
Default is 32.
heightSegmentsNumber of rows of faces along the height.
Default is 1.
openEndedWhether the ends of the cylinder are open or capped.
Default is false.
thetaStartStart angle for first segment in radians.
Default is 0.
thetaLengthCentral angle of the circular sector in radians.
Default is Math.PI * 2.

Parametric Surface Definition

The cylinder surface is defined as a parametric equation where the radius varies linearly along the height:

r(v)=rtop+v(rbottomrtop)r(v) = r_{\text{top}} + v \cdot (r_{\text{bottom}} - r_{\text{top}})

For parameters (u,v)(u, v) where u[0,1]u \in [0, 1] represents angular position and v[0,1]v \in [0, 1] represents height position:

x=r(v)sin(θstart+uθlength)y=vh+h2z=r(v)cos(θstart+uθlength)\begin{align} x &= r(v) \cdot \sin(\theta_{\text{start}} + u \cdot \theta_{\text{length}}) \\ y &= -v \cdot h + \frac{h}{2} \\ z &= r(v) \cdot \cos(\theta_{\text{start}} + u \cdot \theta_{\text{length}}) \end{align}

Normal Vector Calculation

Surface normals are computed considering the slope between top and bottom radii:

slope=rbottomrtoph\text{slope} = \frac{r_{\text{bottom}} - r_{\text{top}}}{h}

The unnormalized normal vector at any point is:

n=(sinθ,slope,cosθ)\vec{n} = (\sin\theta, \text{slope}, \cos\theta)