[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft.
flo-bit.dev/shapecraft/
2.0 kB
70 lines
1import { describe, it, expect } from 'vitest'
2import * as THREE from 'three'
3import { box, sphere, cylinder, plane, cone, torus, icosphere } from '../src/primitives'
4
5describe('primitives', () => {
6 it('box returns a Mesh with correct defaults', () => {
7 const b = box()
8 expect(b.vertexCount).toBe(24)
9 expect(b.faceCount).toBe(12)
10 })
11
12 it('box size shorthand works', () => {
13 const b = box({ size: 2 })
14 const size = b.boundingBox.getSize(new THREE.Vector3())
15 expect(size.x).toBeCloseTo(2)
16 expect(size.y).toBeCloseTo(2)
17 expect(size.z).toBeCloseTo(2)
18 })
19
20 it('box vec3 size shorthand works', () => {
21 const b = box({ size: [2, 3, 4] })
22 const size = b.boundingBox.getSize(new THREE.Vector3())
23 expect(size.x).toBeCloseTo(2)
24 expect(size.y).toBeCloseTo(3)
25 expect(size.z).toBeCloseTo(4)
26 })
27
28 it('sphere returns a Mesh', () => {
29 const s = sphere()
30 expect(s.vertexCount).toBeGreaterThan(0)
31 expect(s.faceCount).toBeGreaterThan(0)
32 })
33
34 it('cylinder returns a Mesh', () => {
35 const c = cylinder()
36 expect(c.vertexCount).toBeGreaterThan(0)
37 })
38
39 it('plane returns a Mesh and is XZ-oriented', () => {
40 const p = plane()
41 const pos = p.positions
42 for (let i = 0; i < pos.length; i += 3) {
43 expect(Math.abs(pos[i + 1])).toBeLessThan(0.001)
44 }
45 })
46
47 it('plane size shorthand works', () => {
48 const p = plane({ size: 5 })
49 const size = p.boundingBox.getSize(new THREE.Vector3())
50 expect(size.x).toBeCloseTo(5)
51 expect(size.z).toBeCloseTo(5)
52 })
53
54 it('cone returns a Mesh', () => {
55 const c = cone()
56 expect(c.vertexCount).toBeGreaterThan(0)
57 })
58
59 it('torus returns a Mesh', () => {
60 const t = torus()
61 expect(t.vertexCount).toBeGreaterThan(0)
62 })
63
64 it('icosphere returns a Mesh with uniform triangles', () => {
65 const i0 = icosphere({ subdivisions: 0 })
66 expect(i0.vertexCount).toBeGreaterThan(0)
67 const i2 = icosphere({ subdivisions: 2 })
68 expect(i2.vertexCount).toBeGreaterThan(i0.vertexCount)
69 })
70})