mesh

package module
v0.0.0-...-bdc3219 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 7 Imported by: 0

README

Mesh

Library for editing and generating 3D geometry.

❌ Doing one thing really well.

✔️ Doing everything terribly.

go get github.com/EliCDavis/mesh

Processing Example

Reads in a obj file and welds vertices, applies laplacian smoothing, and calculates smoothed normals.

package main

import (
	"os"

	"github.com/EliCDavis/mesh"
	"github.com/EliCDavis/mesh/formats/obj"
)

func main() {
	inFile, err := os.Open("dirty.obj")
	if err != nil {
		panic(err)
	}
	defer inFile.Close()

	loadedMesh, _, err := obj.ReadMesh(inFile)
	if err != nil {
		panic(err)
	}

	outFile, err := os.Create("smooth.obj")
	if err != nil {
		panic(err)
	}
	defer outFile.Close()

	smoothedMesh := loadedMesh.
		WeldByVertices(4).
		SmoothLaplacian(5, 0.5).
		CalculateSmoothNormals()

	obj.WriteMesh(smoothedMesh, "", outFile)
}

Helpful Procedural Generation Sub Packages

  • extrude - Functionality for generating geometry from 2D shapes.
  • repeat - Functionality for copying geometry in common patterns.
  • primitives - Functionality pertaining to generating common geometry.
  • noise - Utilities around noise functions for common usecases like stacking multiple samples of perlin noise from different frequencies.
  • coloring - Color utilities for blending multiple colors together using weights.
  • texturing - Image processing utilities like generating Normal maps or blurring images.

Procedural Generation Examples

You can at the different projects under the examples folder for different examples on how to procedurally generate meshes.

Evergreen Trees

This was my submission for ProcJam 2022. Pretty much uses every bit of functionality available in this repository.

[Source Here]

Evergreen Tree Demo

Evergreen Terrain Demo

Terrain

This shows off how to use Delaunay triangulation, perlin noise, and the coloring utilities in this repository.

[Source Here]

terrain

UFO

Shows off how to use the repeat, primitives, and extrude utilities in this repository.

[Source Here]

ufo

Candle

Shows off how to use the primitives and extrude utilities in this repository.

[Source Here]

candle

Todo List

Things I want to implement eventually...

  • Cube Marching
  • Bezier Curves
  • Constrained Delaunay Tesselation
  • 3D Tesselation
  • Slice By Plane
  • Slice By Octree
  • Poisson Reconstruction

Resources

Resources either directly contributing to the code here or are just interesting finds while researching.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoIntersection = errors.New("no intersection")

ErrNoIntersection is thrown when Intersection() contains no intersection

Functions

func CenterOfBoundingBoxOfShapes

func CenterOfBoundingBoxOfShapes(shapes []Shape) vector.Vector2

CenterOfBoundingBoxOfShapes finds the center point of the smallest box that could be drawn to encompas all shapes passed in.

func Clamp

func Clamp(v, min, max float64) float64

func Multiply3x3by3x1

func Multiply3x3by3x1(m Matrix, v vector.Vector3) vector.Vector3

func ToNormal

func ToNormal(inEulerAngle vector.Vector3) vector.Vector3

Types

type AABB

type AABB struct {
	// contains filtered or unexported fields
}

func NewAABB

func NewAABB(center, size vector.Vector3) AABB

func (AABB) Contains

func (aabb AABB) Contains(p vector.Vector3) bool

func (*AABB) EncapsulateBounds

func (aabb *AABB) EncapsulateBounds(b AABB)

func (*AABB) EncapsulatePoint

func (aabb *AABB) EncapsulatePoint(p vector.Vector3)

func (*AABB) Expand

func (aabb *AABB) Expand(amount float64)

func (AABB) Max

func (aabb AABB) Max() vector.Vector3

func (AABB) Min

func (aabb AABB) Min() vector.Vector3

func (*AABB) SetMinMax

func (aabb *AABB) SetMinMax(min, max vector.Vector3)

func (AABB) Size

func (aabb AABB) Size() vector.Vector3

type CollapsableMesh

type CollapsableMesh struct {
	// contains filtered or unexported fields
}

func NewCollapsableMesh

func NewCollapsableMesh(m Mesh) CollapsableMesh

func (*CollapsableMesh) CollapseTri

func (cm *CollapsableMesh) CollapseTri(tri int)

func (CollapsableMesh) ToMesh

func (cm CollapsableMesh) ToMesh() Mesh

type Line2D

type Line2D struct {
	// contains filtered or unexported fields
}

Line2D represents a line segment

func NewLine2D

func NewLine2D(p1, p2 vector.Vector2) Line2D

NewLine2D create a new line

func (Line2D) ClosestPointOnLine

func (l Line2D) ClosestPointOnLine(p vector.Vector2) vector.Vector2

func (Line2D) Dir

func (l Line2D) Dir() vector.Vector2

Dir is end point - starting point

func (Line2D) GetEndPoint

func (l Line2D) GetEndPoint() vector.Vector2

GetEndPoint returns the end point of the line segment

func (Line2D) GetStartPoint

func (l Line2D) GetStartPoint() vector.Vector2

GetStartPoint returns the starting point of the line segment

func (Line2D) Intersection

func (l Line2D) Intersection(other Line2D) (vector.Vector2, error)

Intersection finds where two lines intersect https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

func (Line2D) Intersects

func (l Line2D) Intersects(other Line2D) bool

Intersects determines whether two lines intersect eachother

func (Line2D) ScaleOutwards

func (l Line2D) ScaleOutwards(amount float64) Line2D

ScaleOutwards multiplies the current length of the line by extending it out further in the two different directions it's heading

type Line3D

type Line3D struct {
	// contains filtered or unexported fields
}

Line3D is a series of ordered points that make up a line segment through 3D space.

func NewLine3D

func NewLine3D(p1, p2 vector.Vector3) Line3D

NewLine3D create a new line

func (Line3D) GetEndPoint

func (l Line3D) GetEndPoint() vector.Vector3

GetEndPoint returns the end point of the line segment

func (Line3D) GetStartPoint

func (l Line3D) GetStartPoint() vector.Vector3

GetStartPoint returns the starting point of the line segment

func (Line3D) ScaleOutwards

func (l Line3D) ScaleOutwards(amount float64) Line3D

ScaleOutwards assumes line segment 3d is only two points multiplies the current length of the line by extending it out further in the two different directions it's heading

func (Line3D) SetEndPoint

func (l Line3D) SetEndPoint(newEnd vector.Vector3) Line3D

func (Line3D) SetStartPoint

func (l Line3D) SetStartPoint(newStart vector.Vector3) Line3D

func (Line3D) Translate

func (l Line3D) Translate(amt vector.Vector3) Line3D

func (Line3D) YIntersection

func (l Line3D) YIntersection(x float64, z float64) float64

YIntersection Uses the paremetric equations of the line

type Material

type Material struct {
	Name string

	// Account for light that is scattered about the entire scene
	AmbientColor color.Color

	// The main color
	DiffuseColor color.Color

	// Color seen where the surface is shiny and mirror like
	SpecularColor color.Color

	// Typically between 0 - 1000, with a high value resulting in a tight,
	// concentrated highlight
	//
	// Defines the focus of the specular highlight
	SpecularHighlight float64

	// Index of refraction, between 0.001 to 10, 1.0 means light does not bend
	// as it passes through the object
	OpticalDensity float64

	// Specifies how much this material dissolves into the background. A factor
	// of 0.0 is fully opaque. A factor of 1.0 is completely transparent.
	Transparency float64

	ColorTextureURI *string

	NormalTextureURI *string

	SpecularTextureURI *string
}

Material is just a clone of obj's MTL format at the moment cause man this problem scares me.

func DefaultColorMaterial

func DefaultColorMaterial(c color.Color) Material

func DefaultMaterial

func DefaultMaterial() Material

type Matrix

type Matrix [][]float64

Matrix is row major

func Multiply3x3

func Multiply3x3(m1, m2 Matrix) Matrix

func Rx

func Rx(theta float64) Matrix

func RxT

func RxT(theta float64) Matrix

func Ry

func Ry(theta float64) Matrix

func RyT

func RyT(theta float64) Matrix

func Rz

func Rz(theta float64) Matrix

func RzT

func RzT(theta float64) Matrix

func Transpose

func Transpose(in Matrix) Matrix

type Mesh

type Mesh struct {
	// contains filtered or unexported fields
}

func EmptyMesh

func EmptyMesh() Mesh

func MeshFromView

func MeshFromView(view MeshView) Mesh

func NewMesh

func NewMesh(
	triangles []int,
	vertices []vector.Vector3,
	normals []vector.Vector3,
	uvs [][]vector.Vector2,
) Mesh

func NewMeshWithMaterials

func NewMeshWithMaterials(
	triangles []int,
	vertices []vector.Vector3,
	normals []vector.Vector3,
	uvs [][]vector.Vector2,
	materials []MeshMaterial,
) Mesh

func (Mesh) Append

func (m Mesh) Append(other Mesh) Mesh

func (Mesh) CalculateFlatNormals

func (m Mesh) CalculateFlatNormals() Mesh

func (Mesh) CalculateSmoothNormals

func (m Mesh) CalculateSmoothNormals() Mesh

func (Mesh) Materials

func (m Mesh) Materials() []MeshMaterial

func (Mesh) ModifyUVs

func (m Mesh) ModifyUVs(f func(v vector.Vector3, uv vector.Vector2) vector.Vector2) Mesh

func (Mesh) ModifyVertices

func (m Mesh) ModifyVertices(f func(v vector.Vector3) vector.Vector3) Mesh

func (Mesh) RemoveDegenerateTriangles

func (m Mesh) RemoveDegenerateTriangles(sideLength float64) Mesh

func (Mesh) RemoveUnusedIndices

func (m Mesh) RemoveUnusedIndices() Mesh

func (Mesh) Rotate

func (m Mesh) Rotate(q Quaternion) Mesh

func (Mesh) Scale

func (m Mesh) Scale(origin, amount vector.Vector3) Mesh

func (Mesh) SetMaterial

func (m Mesh) SetMaterial(mat Material) Mesh

func (Mesh) SmoothLaplacian

func (m Mesh) SmoothLaplacian(iterations int, smoothingFactor float64) Mesh

func (Mesh) SplitOnUniqueMaterials

func (m Mesh) SplitOnUniqueMaterials() []Mesh

SplitOnUniqueMaterials generates a mesh per material,

func (Mesh) Translate

func (m Mesh) Translate(v vector.Vector3) Mesh

func (Mesh) Tri

func (m Mesh) Tri(i int) Tri

func (Mesh) TriCount

func (m Mesh) TriCount() int

func (Mesh) VertexNeighborTable

func (m Mesh) VertexNeighborTable() VertexLUT

func (Mesh) View

func (m Mesh) View() MeshView

View exposes the underlying data to be modified. Using this breaks the immutable design of the system, but required for some mesh processing.

Modifying the data stored in the mesh found here will directly update the mesh, and side-steps any type of validation we could have done previously.

If you make changes to this view, assume the mesh and all ancestors of said mesh have just become garbage.

func (Mesh) WeldByVertices

func (m Mesh) WeldByVertices(decimalPlace int) Mesh

type MeshMaterial

type MeshMaterial struct {
	NumOfTris int
	Material  *Material
}

type MeshView

type MeshView struct {
	Vertices  []vector.Vector3
	Triangles []int
	Normals   []vector.Vector3
	UVs       [][]vector.Vector2
}

type Orientation

type Orientation int
const (
	Colinear Orientation = iota
	Clockwise
	Counterclockwise
)

type Quaternion

type Quaternion struct {
	// contains filtered or unexported fields
}

Quaternion is a 4 component imaginary number thingy for rotating 3D meshes.

func NewQuaternion

func NewQuaternion(v vector.Vector3, w float64) Quaternion

NewQuaternion creates a quaternion

func QuaternionZero

func QuaternionZero() Quaternion

QuaternionZero returns a quaternion with 0 for all it's components

func UnitQuaternionFromTheta

func UnitQuaternionFromTheta(theta float64, v vector.Vector3) Quaternion

UnitQuaternionFromTheta takes a vector and angle and builds a unit quaternion in the form (cos(theta/2.0), sin(theta/2.0))

Resources Used:

https://www.youtube.com/watch?v=mHVwd8gYLnI
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

func (Quaternion) Rotate

func (q Quaternion) Rotate(v vector.Vector3) vector.Vector3

Rotate takes a given vector and rotates it with by this quaternion.

Resources Used:

https://gamedev.stackexchange.com/questions/28395

type Shape

type Shape []vector.Vector2

Shape is a flat (2D) arrangement of points.

func (Shape) GetBoundingBoxDimensions

func (s Shape) GetBoundingBoxDimensions() (width, height float64)

func (Shape) GetBounds

func (s Shape) GetBounds() (vector.Vector2, vector.Vector2)

func (Shape) IsInside

func (s Shape) IsInside(p vector.Vector2) bool

IsInside returns true if the point p lies inside the polygon[] with n vertices

func (Shape) Len

func (s Shape) Len() int

Len returns the number of points in the polygon

func (Shape) Less

func (s Shape) Less(i, j int) bool

Less determines which point is more oriented more clockwise from the center than the other

func (Shape) RandomPointInShape

func (s Shape) RandomPointInShape() vector.Vector2

RandomPointInShape returns a random point inside of the shape

func (Shape) Rotate

func (s Shape) Rotate(amount float64, pivot vector.Vector2) Shape

Rotate will rotate all points in the shape around the pivot by the passed in amount

func (Shape) Scale

func (s Shape) Scale(amount float64, origin vector.Vector2) Shape

Scale shifts all points towards or away from the origin

func (Shape) Split

func (s Shape) Split(vericalLine float64) ([]Shape, []Shape)

Split figures out which points land on which side of the vertical line and builds new shapes from that

func (Shape) Swap

func (s Shape) Swap(i, j int)

Swap switches two points indeces so the polygon is ordered a different way

func (Shape) Translate

func (s Shape) Translate(amount vector.Vector2) Shape

Translate Moves all points over by the specified amount

type Tri

type Tri struct {
	// contains filtered or unexported fields
}

Tri provides utility functions to a specific underlying mesh

func (Tri) P1

func (t Tri) P1() int

P1 is the first point on our triangle, which is an index to the vertices array of a mesh

func (Tri) P2

func (t Tri) P2() int

P2 is the second point on our triangle, which is an index to the vertices array of a mesh

func (Tri) P3

func (t Tri) P3() int

P3 is the third point on our triangle, which is an index to the vertices array of a mesh

func (Tri) UniqueVertices

func (t Tri) UniqueVertices() bool

Valid determines whether or not the contains 3 unique vertices.

type VectorInt

type VectorInt struct {
	X int
	Y int
	Z int
}

func Vector3ToInt

func Vector3ToInt(v vector.Vector3, power int) VectorInt

func (VectorInt) ToRegularVector

func (v VectorInt) ToRegularVector() vector.Vector3

type VertexLUT

type VertexLUT map[int]map[int]struct{}

func (VertexLUT) AddLookup

func (vLUT VertexLUT) AddLookup(from, to int)

func (VertexLUT) Count

func (vLUT VertexLUT) Count(v int) int
func (vLUT VertexLUT) Link(v1, v2 int)

func (VertexLUT) Lookup

func (vLUT VertexLUT) Lookup(v int) map[int]struct{}

func (VertexLUT) Remove

func (vLUT VertexLUT) Remove(v int) map[int]struct{}
func (vLUT VertexLUT) RemoveLink(v1, v2 int)

func (VertexLUT) RemoveLookup

func (vLUT VertexLUT) RemoveLookup(from, to int)

func (VertexLUT) RemoveVertex

func (vLUT VertexLUT) RemoveVertex(v int)

Directories

Path Synopsis
examples
ufo
formats
obj
ply

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL