Skip to content

Commit

Permalink
vector3 array syncing across the network realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 10, 2023
1 parent 1d9cbf0 commit dc70461
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 139 deletions.
140 changes: 10 additions & 130 deletions examples/pumpkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,6 @@ import (
"github.com/EliCDavis/vector/vector3"
)

// perm = range(256)
// random.shuffle(perm)
// perm += perm
// dirs = [(math.cos(a * 2.0 * math.pi / 256),
// math.sin(a * 2.0 * math.pi / 256))
// for a in range(256)]

type TilingNoise struct {
dirs []vector2.Float64
perm []int
}

func (tn *TilingNoise) init() {
size := 256

tn.perm = make([]int, size)
for i := 0; i < size; i++ {
tn.perm[i] = i
}
rand.Shuffle(len(tn.perm), func(i, j int) { tn.perm[i], tn.perm[j] = tn.perm[j], tn.perm[i] })
tn.perm = append(tn.perm, tn.perm...)

tn.dirs = make([]vector2.Float64, size)
for i := 0; i < size; i++ {
a := float64(i)
tn.dirs[i] = vector2.New(
math.Cos((a*2.*math.Pi)/float64(size)),
math.Sin((a*2.*math.Pi)/float64(size)),
)
}
}

// https://gamedev.stackexchange.com/questions/23625/how-do-you-generate-tileable-perlin-noise
func (tn *TilingNoise) surflet(v vector2.Float64, g vector2.Int, per int) float64 {
dist := v.Sub(g.ToFloat64()).Abs()
polyX := 1 - (6 * math.Pow(dist.X(), 5)) + (15 * math.Pow(dist.X(), 4)) - (10 * math.Pow(dist.X(), 3))
polyY := 1 - (6 * math.Pow(dist.Y(), 5)) + (15 * math.Pow(dist.Y(), 4)) - (10 * math.Pow(dist.Y(), 3))

hashed := tn.perm[tn.perm[g.X()%per]+(g.Y()%per)]

hashedDir := tn.dirs[hashed]
grad := ((v.X() - float64(g.X())) * hashedDir.X()) + ((v.Y() - float64(g.Y())) * hashedDir.Y())
return polyX * polyY * grad
}

func (tn *TilingNoise) Noise(v vector2.Float64, per int) float64 {
i := v.FloorToInt()
return tn.surflet(v, i, per) +
tn.surflet(v, i.Add(vector2.Right[int]()), per) +
tn.surflet(v, i.Add(vector2.Up[int]()), per) +
tn.surflet(v, i.Add(vector2.One[int]()), per)
}

func closestTimeOnMultiLineSegment(point vector3.Float64, multiLine []vector3.Float64, totalLength float64) float64 {
if len(multiLine) < 2 {
panic("line segment required 2 or more points")
Expand Down Expand Up @@ -112,25 +59,11 @@ func metalRoughness() image.Image {
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := &TilingNoise{}
n.init()
n := noise.NewTilingNoise(dim, 1/64., 5)

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := 0.
freq := 1. / 64.
for o := 0; o < 5; o++ {
op2 := math.Pow(2, float64(o))
n := n.Noise(
vector2.New(
(float64(x)*freq)*op2,
(float64(y)*freq)*op2,
),
int(float64(dim)*freq)*int(op2),
)
val += math.Pow(0.5, float64(o)) * n
}
// p := n.Noise(vector2.New(xDim*10, yDim*10), 100)
val := n.Noise(x, y)
p := (val * 128) + 128

p = 255 - (p * 0.75)
Expand All @@ -151,8 +84,7 @@ func albedo(positiveColor, negativeColor color.Color) image.Image {
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := &TilingNoise{}
n.init()
n := noise.NewTilingNoise(dim, 1/64., 5)

nR, nG, nB, _ := negativeColor.RGBA()
pR, pG, pB, _ := positiveColor.RGBA()
Expand All @@ -163,20 +95,7 @@ func albedo(positiveColor, negativeColor color.Color) image.Image {

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := 0.
freq := 1. / 64.
for o := 0; o < 5; o++ {
op2 := math.Pow(2, float64(o))
n := n.Noise(
vector2.New(
(float64(x)*freq)*op2,
(float64(y)*freq)*op2,
),
int(float64(dim)*freq)*int(op2),
)
val += math.Pow(0.5, float64(o)) * n
}
// p := n.Noise(vector2.New(xDim*10, yDim*10), 100)
val := n.Noise(x, y)
p := (val * 0.5) + 0.5

r := uint32(float64(nR) + (rRange * p))
Expand All @@ -199,24 +118,11 @@ func stemNormalImage() image.Image {
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := &TilingNoise{}
n.init()
n := noise.NewTilingNoise(dim, 1/64., 5)

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := 0.
freq := 1. / 64.
for o := 0; o < 5; o++ {
op2 := math.Pow(2, float64(o))
n := n.Noise(
vector2.New(
(float64(x)*freq)*op2,
(float64(y)*freq)*op2,
),
int(float64(dim)*freq)*int(op2),
)
val += math.Pow(0.5, float64(o)) * n
}
val := n.Noise(x, y)
// p := n.Noise(vector2.New(xDim*10, yDim*10), 100)
p := (val * 128) + 128

Expand Down Expand Up @@ -281,24 +187,11 @@ func normalImage() image.Image {
img := image.NewRGBA(image.Rect(0, 0, dim, dim))
// normals.Fill(img)

n := &TilingNoise{}
n.init()
n := noise.NewTilingNoise(dim, 1/64., 5)

for x := 0; x < dim; x++ {
for y := 0; y < dim; y++ {
val := 0.
freq := 1. / 64.
for o := 0; o < 5; o++ {
op2 := math.Pow(2, float64(o))
n := n.Noise(
vector2.New(
(float64(x)*freq)*op2,
(float64(y)*freq)*op2,
),
int(float64(dim)*freq)*int(op2),
)
val += math.Pow(0.5, float64(o)) * n
}
val := n.Noise(x, y)
// p := n.Noise(vector2.New(xDim*10, yDim*10), 100)
p := (val * 128) + 128

Expand Down Expand Up @@ -958,8 +851,7 @@ func main() {
dim := 1024
img := image.NewRGBA(image.Rect(0, 0, dim, dim))

n := &TilingNoise{}
n.init()
n := noise.NewTilingNoise(dim, 1/64., 5)

for x := 0; x < dim; x++ {
// xDim := (float64(x) / float64(dim)) * 2
Expand Down Expand Up @@ -1018,19 +910,7 @@ func main() {

// p := noise.Perlin3D(final.Scale(.8)) * 255

val := 0.
freq := 1. / 64.
for o := 0; o < 5; o++ {
op2 := math.Pow(2, float64(o))
n := n.Noise(
vector2.New(
(float64(x)*freq)*op2,
(float64(y)*freq)*op2,
),
int(float64(dim)*freq)*int(op2),
)
val += math.Pow(0.5, float64(o)) * n
}
val := n.Noise(x, y)
// p := n.Noise(vector2.New(xDim*10, yDim*10), 100)
p := (val * 128) + 128

Expand Down
8 changes: 4 additions & 4 deletions examples/structure/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ func main() {
&generator.VectorArrayParameter{
Name: "Positions",
DefaultValue: []vector3.Vector[float64]{
vector3.New(4*0, 0., 0.),
vector3.New(4*1, 0., 0.),
vector3.New(4*2, 0., 4),
vector3.New(4*3, 0., 4),
vector3.New(4*0., 0., 0.),
vector3.New(4*1., 0., 0.),
vector3.New(4*2., 0., 4.),
vector3.New(4*3., 0., 4.),
},
},
},
Expand Down
22 changes: 17 additions & 5 deletions generator/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let clientID = null;


function fetch(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
console.log(xmlHttp.responseText)
Expand All @@ -14,7 +14,7 @@ function fetch(theUrl, callback) {
}

function download(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
const xmlHttp = new XMLHttpRequest();
xmlHttp.responseType = 'blob';
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
Expand All @@ -28,7 +28,7 @@ function download(theUrl, callback) {


function post(theUrl, body, callback) {
var xmlHttp = new XMLHttpRequest();
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
callback(JSON.parse(xmlHttp.responseText));
Expand Down Expand Up @@ -634,7 +634,19 @@ const updateProfileParametersWithNewSchema = (prof, newSchema) => {
updateProfileParametersWithNewSchema(prof[schemaParam.name], schemaParam)
break;

// case ballsCachePositionsOrSomething
case "VectorArray":
while (prof[schemaParam.name].length > 0) {
prof[schemaParam.name].pop();
}
schemaParam.currentValue.forEach((position) => {
prof[schemaParam.name].push(position)
})

prof[schemaParam.name].forEach((position, index) => {
newPositionControl(prof, schemaParam.name, position, index);
})
break;


default:
prof[schemaParam.name] = schemaParam.currentValue;
Expand All @@ -647,14 +659,14 @@ const updateProfileWithNewSchema = (prof, newSchema) => {
for (const [key, gen] of Object.entries(prof.subGenerators)) {
updateProfileWithNewSchema(gen, newSchema.subGenerators[key])
}
console.log(prof)
updateProfileParametersWithNewSchema(prof.Parameters, newSchema.parameters);
}

const featchandApplyLatestSchemaToControls = () => {
fetch("/schema", (generatorSchema) => {
schema = generatorSchema;
RefreshProducerOutput();
clearPositionControls();
updateProfileWithNewSchema(profile, generatorSchema)
allMeshGUISettings.forEach(setting => {
setting.updateDisplay();
Expand Down

0 comments on commit dc70461

Please sign in to comment.