NewSegmentedBox creates a segmented box geometry with the specified width, height, length, and number of segments in each dimension.
(width, height, length float32, widthSegments, heightSegments, lengthSegments int)
| 26 | |
| 27 | // NewSegmentedBox creates a segmented box geometry with the specified width, height, length, and number of segments in each dimension. |
| 28 | func NewSegmentedBox(width, height, length float32, widthSegments, heightSegments, lengthSegments int) *Geometry { |
| 29 | |
| 30 | box := NewGeometry() |
| 31 | |
| 32 | // Validate arguments |
| 33 | if widthSegments <= 0 || heightSegments <= 0 || lengthSegments <= 0 { |
| 34 | panic("Invalid argument(s). All segment quantities should be greater than zero.") |
| 35 | } |
| 36 | |
| 37 | // Create buffers |
| 38 | positions := math32.NewArrayF32(0, 16) |
| 39 | normals := math32.NewArrayF32(0, 16) |
| 40 | uvs := math32.NewArrayF32(0, 16) |
| 41 | indices := math32.NewArrayU32(0, 16) |
| 42 | |
| 43 | // Internal function to build each of the six box planes |
| 44 | buildPlane := func(u, v string, udir, vdir int, width, height, length float32, materialIndex uint) { |
| 45 | |
| 46 | offset := positions.Len() / 3 |
| 47 | gridX := widthSegments |
| 48 | gridY := heightSegments |
| 49 | var w string |
| 50 | |
| 51 | if (u == "x" && v == "y") || (u == "y" && v == "x") { |
| 52 | w = "z" |
| 53 | } else if (u == "x" && v == "z") || (u == "z" && v == "x") { |
| 54 | w = "y" |
| 55 | gridY = lengthSegments |
| 56 | } else if (u == "z" && v == "y") || (u == "y" && v == "z") { |
| 57 | w = "x" |
| 58 | gridX = lengthSegments |
| 59 | } |
| 60 | |
| 61 | var normal math32.Vector3 |
| 62 | if length > 0 { |
| 63 | normal.SetByName(w, 1) |
| 64 | } else { |
| 65 | normal.SetByName(w, -1) |
| 66 | } |
| 67 | |
| 68 | wHalf := width / 2 |
| 69 | hHalf := height / 2 |
| 70 | gridX1 := gridX + 1 |
| 71 | gridY1 := gridY + 1 |
| 72 | segmentWidth := width / float32(gridX) |
| 73 | segmentHeight := height / float32(gridY) |
| 74 | |
| 75 | // Generate the plane vertices, normals, and uv coordinates |
| 76 | for iy := 0; iy < gridY1; iy++ { |
| 77 | for ix := 0; ix < gridX1; ix++ { |
| 78 | var vector math32.Vector3 |
| 79 | vector.SetByName(u, (float32(ix)*segmentWidth-wHalf)*float32(udir)) |
| 80 | vector.SetByName(v, (float32(iy)*segmentHeight-hHalf)*float32(vdir)) |
| 81 | vector.SetByName(w, length) |
| 82 | positions.AppendVector3(&vector) |
| 83 | normals.AppendVector3(&normal) |
| 84 | uvs.Append(float32(ix)/float32(gridX), float32(1)-(float32(iy)/float32(gridY))) |
| 85 | } |
no test coverage detected