Skip to content

Commit

Permalink
Screw operation takes UVs, new smooth normals implicit weld. New octr…
Browse files Browse the repository at this point in the history
…ee functions, new potree functions + fixes, stl fixes
  • Loading branch information
EliCDavis committed Aug 15, 2024
1 parent 48cb58d commit d0b673c
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 87 deletions.
51 changes: 51 additions & 0 deletions drawing/texturing/noise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package texturing

import (
"image"
"image/color"

"github.com/EliCDavis/polyform/drawing/coloring"
"github.com/EliCDavis/polyform/math/noise"
"github.com/EliCDavis/polyform/nodes"
)

type SeamlessPerlinNode = nodes.StructNode[image.Image, SeamlessPerlinNodeData]

type SeamlessPerlinNodeData struct {
Positive nodes.NodeOutput[coloring.WebColor]
Negative nodes.NodeOutput[coloring.WebColor]
}

func (an SeamlessPerlinNodeData) Process() (image.Image, error) {
dim := 1024
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := noise.NewTilingNoise(dim, 1/64., 5)

nR, nG, nB, _ := an.Negative.Value().RGBA()
pR, pG, pB, _ := an.Positive.Value().RGBA()

rRange := float64(pR>>8) - float64(nR>>8)
gRange := float64(pG>>8) - float64(nG>>8)
bRange := float64(pB>>8) - float64(nB>>8)

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := n.Noise(x, y)
p := (val * 0.5) + 0.5

r := uint32(float64(nR) + (rRange * p))
g := uint32(float64(nG) + (gRange * p))
b := uint32(float64(nB) + (bRange * p))

img.Set(x, y, color.RGBA{
R: byte(r), // byte(len * 255),
G: byte(g),
B: byte(b),
A: 255,
})
}
}
return img, nil
}
49 changes: 4 additions & 45 deletions examples/pumpkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,6 @@ import (
"github.com/EliCDavis/vector/vector3"
)

type Albedo = nodes.StructNode[image.Image, AlbedoData]

type AlbedoData struct {
Positive nodes.NodeOutput[coloring.WebColor]
Negative nodes.NodeOutput[coloring.WebColor]
}

func (an AlbedoData) Process() (image.Image, error) {
dim := 1024
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := noise.NewTilingNoise(dim, 1/64., 5)

nR, nG, nB, _ := an.Negative.Value().RGBA()
pR, pG, pB, _ := an.Positive.Value().RGBA()

rRange := float64(pR>>8) - float64(nR>>8)
gRange := float64(pG>>8) - float64(nG>>8)
bRange := float64(pB>>8) - float64(nB>>8)

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := n.Noise(x, y)
p := (val * 0.5) + 0.5

r := uint32(float64(nR) + (rRange * p))
g := uint32(float64(nG) + (gRange * p))
b := uint32(float64(nB) + (bRange * p))

img.Set(x, y, color.RGBA{
R: byte(r), // byte(len * 255),
G: byte(g),
B: byte(b),
A: 255,
})
}
}
return img, nil
}

func jitterPositions(pos []vector3.Float64, amplitude, frequency float64) []vector3.Float64 {
return vector3.Array[float64](pos).
Modify(func(v vector3.Float64) vector3.Float64 {
Expand Down Expand Up @@ -364,8 +323,8 @@ func main() {
},
},
},
"pumpkin.png": artifact.NewImageNode(&Albedo{
Data: AlbedoData{
"pumpkin.png": artifact.NewImageNode(&texturing.SeamlessPerlinNode{
Data: texturing.SeamlessPerlinNodeData{
Positive: &parameter.Color{
Name: "Base Color",
DefaultValue: coloring.WebColor{R: 0xf9, G: 0x81, B: 0x1f, A: 255},
Expand All @@ -376,8 +335,8 @@ func main() {
},
},
}),
"stem.png": artifact.NewImageNode(&Albedo{
Data: AlbedoData{
"stem.png": artifact.NewImageNode(&texturing.SeamlessPerlinNode{
Data: texturing.SeamlessPerlinNodeData{
Positive: &parameter.Color{
Name: "Stem Base Color",
DefaultValue: coloring.WebColor{R: 0xce, G: 0xa2, B: 0x7e, A: 255},
Expand Down
28 changes: 24 additions & 4 deletions examples/ufo/gltf.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ func (gad GltfModelData) Process() (gltf.PolyformModel, error) {
type GltfMaterialNode = nodes.StructNode[gltf.PolyformMaterial, GltfMaterialNodeData]

type GltfMaterialNodeData struct {
Color nodes.NodeOutput[coloring.WebColor]
MetallicFactor nodes.NodeOutput[float64]
RoughnessFactor nodes.NodeOutput[float64]
EmissiveFactor nodes.NodeOutput[coloring.WebColor]
Color nodes.NodeOutput[coloring.WebColor]
ColorTexture nodes.NodeOutput[string]
MetallicFactor nodes.NodeOutput[float64]
RoughnessFactor nodes.NodeOutput[float64]
MetallicRoughnessTexture nodes.NodeOutput[string]
EmissiveFactor nodes.NodeOutput[coloring.WebColor]

// Extensions
IndexOfRefraction nodes.NodeOutput[float64]
Expand All @@ -77,6 +79,24 @@ func (gmnd GltfMaterialNodeData) Process() (gltf.PolyformMaterial, error) {
pbr.BaseColorFactor = gmnd.Color.Value()
}

if gmnd.ColorTexture != nil {
if pbr == nil {
pbr = &gltf.PolyformPbrMetallicRoughness{}
}
pbr.BaseColorTexture = &gltf.PolyformTexture{
URI: gmnd.ColorTexture.Value(),
}
}

if gmnd.MetallicRoughnessTexture != nil {
if pbr == nil {
pbr = &gltf.PolyformPbrMetallicRoughness{}
}
pbr.MetallicRoughnessTexture = &gltf.PolyformTexture{
URI: gmnd.MetallicRoughnessTexture.Value(),
}
}

if gmnd.MetallicFactor != nil {
if pbr == nil {
pbr = &gltf.PolyformPbrMetallicRoughness{}
Expand Down
87 changes: 82 additions & 5 deletions examples/ufo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"os"

"github.com/EliCDavis/polyform/drawing/coloring"
"github.com/EliCDavis/polyform/drawing/texturing"
"github.com/EliCDavis/polyform/formats/gltf"
"github.com/EliCDavis/polyform/formats/obj"
"github.com/EliCDavis/polyform/generator"
"github.com/EliCDavis/polyform/generator/artifact"
"github.com/EliCDavis/polyform/generator/parameter"
"github.com/EliCDavis/polyform/generator/room"
"github.com/EliCDavis/polyform/math/quaternion"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/EliCDavis/polyform/modeling/primitives"
"github.com/EliCDavis/polyform/modeling/repeat"
"github.com/EliCDavis/polyform/nodes"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)

Expand Down Expand Up @@ -368,28 +371,52 @@ func main() {
Name: "Revolutions",
DefaultValue: 1,
},
UVs: &primitives.StripUVsNode{
Data: primitives.StripUVsNodeData{
Start: &parameter.Vector2{
Name: "UV Start",
DefaultValue: vector2.New(0., 0.5),
},
End: &parameter.Vector2{
Name: "UV End",
DefaultValue: vector2.New(20, 0.5),
},
},
},
},
}

smoothedBody := &meshops.SmoothNormalsNode{
Data: meshops.SmoothNormalsNodeData{
smoothedBody := &meshops.SmoothNormalsImplicitWeldNode{
Data: meshops.SmoothNormalsImplicitWeldNodeData{
Mesh: body,
Distance: &parameter.Float64{
Name: "Weld Dist",
DefaultValue: 0.0001,
},
},
}

ufoBodyMaterial := &GltfMaterialNode{
Data: GltfMaterialNodeData{
Color: &parameter.Color{
Name: "UFO Color",
DefaultValue: coloring.WebColor{R: 225, G: 225, B: 225, A: 255},
DefaultValue: coloring.White(),
},
MetallicFactor: &parameter.Float64{
Name: "UFO Metallic",
DefaultValue: 1,
},
RoughnessFactor: &parameter.Float64{
Name: "UFO Roughness",
DefaultValue: .4,
DefaultValue: .3,
},
ColorTexture: &parameter.String{
Name: "Color Tex URI",
DefaultValue: "brushed.png",
},
MetallicRoughnessTexture: &parameter.String{
Name: "Metalic Tex URI",
DefaultValue: "rough.png",
},
Clearcoat: &GltfMaterialClearcoatExtensionNode{
Data: GltfMaterialClearcoatExtensionNodeData{
Expand Down Expand Up @@ -423,7 +450,7 @@ func main() {
Near: 10,
Far: 150,
},
Ground: coloring.WebColor{R: 0x2e, G: 0x47, B: 0x2e, A: 255},
Ground: coloring.WebColor{R: 0x51, G: 0x6e, B: 0x51, A: 255},
Lighting: coloring.White(),
},
Producers: map[string]nodes.NodeOutput[generator.Artifact]{
Expand Down Expand Up @@ -527,6 +554,56 @@ func main() {
},
},
},
// "brushed.png": artifact.NewImageNode(&BrushedMetalNode{
// Data: BrushedMetalNodeNodeData{
// Dimensions: &parameter.Int{
// Name: "Tex Dimensions",
// DefaultValue: 512,
// },
// Count: &parameter.Int{
// Name: "Brush Count",
// DefaultValue: 50,
// },
// BrushSize: &parameter.Float64{
// Name: "Brush Size",
// DefaultValue: 10,
// },
// },
// }),
"brushed.png": artifact.NewImageNode(&texturing.SeamlessPerlinNode{
Data: texturing.SeamlessPerlinNodeData{
Positive: &parameter.Color{
Name: "Positive",
DefaultValue: coloring.Grey(222),
},
Negative: &parameter.Color{
Name: "Negative",
DefaultValue: coloring.Grey(202),
},
},
}),
"rough.png": artifact.NewImageNode(&texturing.SeamlessPerlinNode{
Data: texturing.SeamlessPerlinNodeData{
Positive: &parameter.Color{
Name: "Positive",
DefaultValue: coloring.WebColor{
R: 0,
G: 70, // Perfectly reflective
B: 255, // Always metalic
A: 255,
},
},
Negative: &parameter.Color{
Name: "Negative",
DefaultValue: coloring.WebColor{
R: 0,
G: 200, // Somewhat reflective
B: 255, // Always metalic
A: 255,
},
},
},
}),
},
}

Expand Down
63 changes: 63 additions & 0 deletions examples/ufo/tex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"image"
"image/draw"

"github.com/EliCDavis/polyform/drawing/coloring"
"github.com/EliCDavis/polyform/nodes"
"github.com/fogleman/gg"
)

type BrushedMetalNode = nodes.StructNode[image.Image, BrushedMetalNodeNodeData]

type BrushedMetalNodeNodeData struct {
Dimensions nodes.NodeOutput[int]
BaseColor nodes.NodeOutput[coloring.WebColor]
BrushColor nodes.NodeOutput[coloring.WebColor]
BrushSize nodes.NodeOutput[float64]
Count nodes.NodeOutput[int]
}

func (gnd BrushedMetalNodeNodeData) Process() (image.Image, error) {
dimensions := 512
if gnd.Dimensions != nil {
dimensions = gnd.Dimensions.Value()
}
img := image.NewRGBA(image.Rect(0, 0, dimensions, dimensions))

baseColor := coloring.Grey(200)
if gnd.BaseColor != nil {
baseColor = gnd.BaseColor.Value()
}
draw.Draw(img, img.Bounds(), &image.Uniform{baseColor}, image.Point{}, draw.Src)

ctx := gg.NewContextForImage(img)

lineWidth := 1.
if gnd.BrushSize != nil {
lineWidth = gnd.BrushSize.Value()
}
ctx.SetLineWidth(lineWidth)

bruchColor := coloring.Grey(150)
if gnd.BrushColor != nil {
bruchColor = gnd.BrushColor.Value()
}
ctx.SetColor(bruchColor)

horizontalLines := 10
if gnd.Count != nil {
horizontalLines = gnd.Count.Value()
}

horizontalSpacing := float64(dimensions) / float64(horizontalLines)
for i := 0; i < horizontalLines; i++ {
y := (horizontalSpacing * float64(i)) + (horizontalSpacing / 2)
ctx.DrawLine(0, y, float64(dimensions), y)
}

ctx.Stroke()

return ctx.Image(), nil
}
Loading

0 comments on commit d0b673c

Please sign in to comment.