MCPcopy Index your code
hub / github.com/g3n/engine / NewSegmentedBox

Function NewSegmentedBox

geometry/box.go:28–137  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

26
27// NewSegmentedBox creates a segmented box geometry with the specified width, height, length, and number of segments in each dimension.
28func 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 }

Callers 2

NewSegmentedCubeFunction · 0.85
NewBoxFunction · 0.85

Calls 15

LenMethod · 0.95
SetByNameMethod · 0.95
AppendVector3Method · 0.95
AppendMethod · 0.95
SizeMethod · 0.95
AppendMethod · 0.95
AddGroupMethod · 0.95
SetIndicesMethod · 0.95
AddVBOMethod · 0.95
NewArrayF32Function · 0.92
NewArrayU32Function · 0.92
NewVBOFunction · 0.92

Tested by

no test coverage detected