Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Molstar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Malý
Molstar
Commits
d3b5a208
Commit
d3b5a208
authored
6 years ago
by
Alexander Rose
Browse files
Options
Downloads
Patches
Plain Diff
simplified box geometry
parent
9bcc5ab2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mol-geo/primitive/box.ts
+43
-76
43 additions, 76 deletions
src/mol-geo/primitive/box.ts
with
43 additions
and
76 deletions
src/mol-geo/primitive/box.ts
+
43
−
76
View file @
d3b5a208
...
...
@@ -11,97 +11,64 @@ import { Vec3 } from 'mol-math/linear-algebra'
export
const
DefaultBoxProps
=
{
width
:
1
,
height
:
1
,
depth
:
1
,
widthSegments
:
1
,
heightSegments
:
1
,
depthSegments
:
1
depth
:
1
}
export
type
BoxProps
=
Partial
<
typeof
DefaultBoxProps
>
const
tmpVector
=
Vec3
.
zero
();
export
default
function
Box
(
props
?:
BoxProps
)
{
const
{
width
,
height
,
depth
,
widthSegments
,
heightSegments
,
depthSegments
}
=
{
...
DefaultBoxProps
,
...
props
}
const
{
width
,
height
,
depth
}
=
{
...
DefaultBoxProps
,
...
props
}
// buffers
const
indices
:
number
[]
=
[]
;
const
vertices
:
number
[]
=
[]
;
const
normals
:
number
[]
=
[]
;
const
vertices
=
new
Float32Array
(
72
)
;
const
normals
=
new
Float32Array
(
72
)
;
const
indices
=
new
Uint32Array
(
36
)
;
// helper variables
let
numberOfVertices
=
0
;
let
vertexCount
=
0
;
// build each side of the box geometry
buildPlane
(
2
,
1
,
0
,
-
1
,
-
1
,
depth
,
height
,
width
,
depthSegments
,
heightSegments
);
// px
buildPlane
(
2
,
1
,
0
,
1
,
-
1
,
depth
,
height
,
-
width
,
depthSegments
,
heightSegments
);
// nx
buildPlane
(
0
,
2
,
1
,
1
,
1
,
width
,
depth
,
height
,
widthSegments
,
depthSegments
);
// py
buildPlane
(
0
,
2
,
1
,
1
,
-
1
,
width
,
depth
,
-
height
,
widthSegments
,
depthSegments
);
// ny
buildPlane
(
0
,
1
,
2
,
1
,
-
1
,
width
,
height
,
depth
,
widthSegments
,
heightSegments
);
// pz
buildPlane
(
0
,
1
,
2
,
-
1
,
-
1
,
width
,
height
,
-
depth
,
widthSegments
,
heightSegments
);
// nz
return
{
vertices
:
new
Float32Array
(
vertices
),
normals
:
new
Float32Array
(
normals
),
indices
:
new
Uint32Array
(
indices
)
}
function
buildPlane
(
u
:
number
,
v
:
number
,
w
:
number
,
udir
:
number
,
vdir
:
number
,
width
:
number
,
height
:
number
,
depth
:
number
,
gridX
:
number
,
gridY
:
number
)
{
const
segmentWidth
=
width
/
gridX
;
const
segmentHeight
=
height
/
gridY
;
buildPlane
(
2
,
1
,
0
,
-
1
,
-
1
,
depth
,
height
,
width
);
// px
buildPlane
(
2
,
1
,
0
,
1
,
-
1
,
depth
,
height
,
-
width
);
// nx
buildPlane
(
0
,
2
,
1
,
1
,
1
,
width
,
depth
,
height
);
// py
buildPlane
(
0
,
2
,
1
,
1
,
-
1
,
width
,
depth
,
-
height
);
// ny
buildPlane
(
0
,
1
,
2
,
1
,
-
1
,
width
,
height
,
depth
);
// pz
buildPlane
(
0
,
1
,
2
,
-
1
,
-
1
,
width
,
height
,
-
depth
);
// nz
const
widthHalf
=
width
/
2
;
const
heightHalf
=
height
/
2
;
const
depthHalf
=
depth
/
2
;
const
gridX1
=
gridX
+
1
;
const
gridY1
=
gridY
+
1
;
let
vertexCounter
=
0
;
const
vector
=
Vec3
.
zero
();
return
{
vertices
,
normals
,
indices
}
function
buildPlane
(
u
:
number
,
v
:
number
,
w
:
number
,
udir
:
number
,
vdir
:
number
,
width
:
number
,
height
:
number
,
depth
:
number
)
{
// generate vertices and normals
for
(
let
iy
=
0
;
iy
<
gridY1
;
++
iy
)
{
const
y
=
iy
*
segmentHeight
-
heightHalf
;
for
(
let
ix
=
0
;
ix
<
gridX1
;
++
ix
)
{
const
x
=
ix
*
segmentWidth
-
widthHalf
;
// set values to correct vector component
vector
[
u
]
=
x
*
udir
;
vector
[
v
]
=
y
*
vdir
;
vector
[
w
]
=
depthHalf
;
// now apply vector to vertex buffer
vertices
.
push
(...
vector
);
// set values to correct vector component
vector
[
u
]
=
0
;
vector
[
v
]
=
0
;
vector
[
w
]
=
depth
>
0
?
1
:
-
1
;
// now apply vector to normal buffer
normals
.
push
(...
vector
);
vertexCounter
+=
1
;
}
}
// indices
// 1. you need three indices to draw a single face
// 2. a single segment consists of two faces
// 3. so we need to generate six (2*3) indices per segment
for
(
let
iy
=
0
;
iy
<
gridY
;
++
iy
)
{
for
(
let
ix
=
0
;
ix
<
gridX
;
++
ix
)
{
const
a
=
numberOfVertices
+
ix
+
gridX1
*
iy
;
const
b
=
numberOfVertices
+
ix
+
gridX1
*
(
iy
+
1
);
const
c
=
numberOfVertices
+
(
ix
+
1
)
+
gridX1
*
(
iy
+
1
);
const
d
=
numberOfVertices
+
(
ix
+
1
)
+
gridX1
*
iy
;
// faces
indices
.
push
(
a
,
b
,
d
);
indices
.
push
(
b
,
c
,
d
);
for
(
let
iy
=
0
;
iy
<
2
;
++
iy
)
{
const
y
=
iy
*
height
-
height
/
2
;
for
(
let
ix
=
0
;
ix
<
2
;
++
ix
)
{
const
x
=
ix
*
width
-
width
/
2
;
// set values to correct vector component and add to vertex buffer
tmpVector
[
u
]
=
x
*
udir
;
tmpVector
[
v
]
=
y
*
vdir
;
tmpVector
[
w
]
=
depth
/
2
;
Vec3
.
toArray
(
tmpVector
,
vertices
,
vertexCount
*
3
);
// set values to correct vector component and add to normal buffer
tmpVector
[
u
]
=
0
;
tmpVector
[
v
]
=
0
;
tmpVector
[
w
]
=
depth
>
0
?
1
:
-
1
;
Vec3
.
toArray
(
tmpVector
,
normals
,
vertexCount
*
3
);
++
vertexCount
;
}
}
numberOfVertices
+=
vertexCounter
;
// faces
const
vc
=
vertexCount
-
4
const
iidx
=
(
vc
/
2
)
*
3
indices
[
iidx
]
=
vc
indices
[
iidx
+
1
]
=
vc
+
2
indices
[
iidx
+
2
]
=
vc
+
1
indices
[
iidx
+
3
]
=
vc
+
2
indices
[
iidx
+
4
]
=
vc
+
3
indices
[
iidx
+
5
]
=
vc
+
1
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment