Skip to content

Commit

Permalink
Heightmaps (#35)
Browse files Browse the repository at this point in the history
* wip with grass

* grass improvements

* wild

* add skirtDepth param to flatWorlds

* changeset new version

* ci: debug publishing
  • Loading branch information
kenjinp authored Dec 21, 2023
1 parent 17d3d09 commit 32acd09
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 31 deletions.
9 changes: 9 additions & 0 deletions .changeset/sixty-ducks-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"docs": patch
"examples": patch
"@hello-worlds/planets": patch
"@hello-worlds/react": patch
"@hello-worlds/vfx": patch
---

Change normal calculations for FlatWorlds (broken)
9 changes: 9 additions & 0 deletions .changeset/soft-shrimps-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"docs": patch
"examples": patch
"@hello-worlds/planets": patch
"@hello-worlds/react": patch
"@hello-worlds/vfx": patch
---

Various improvements to FlatWorlds
5 changes: 3 additions & 2 deletions apps/examples/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "examples",
"private": true,
"version": "0.0.1",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "vite build",
"build:strictly": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/planets/src/flat-world/FlatWorld.chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export function buildFlatWorldChunk<D>(
// Generate indices
const indices = generateIndices(resolution)
// Get Normals
const normals = generateNormals(positions, indices)
let normals = new Array(positions.length).fill(0.0)
normals = generateNormals(normals, positions, indices)

// Pull the skirt vertices down away from the surface
fixEdgeSkirt(
Expand All @@ -58,6 +59,7 @@ export function buildFlatWorldChunk<D>(
skirtDepth,
inverted,
)

const heightmap = generateHeightmapBitmap(heights, heightsMin, heightsMax)

// TODO: allow users to create their own buffers (for terrain splatting or object scattering)
Expand Down
73 changes: 59 additions & 14 deletions packages/planets/src/flat-world/chunk-helpers/fixEdgeSkirts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { MathUtils, Vector3 } from "three"

const _D = new Vector3()
const _P = new Vector3()
const _N = new Vector3()
const _NP1 = new Vector3()
const _NP2 = new Vector3()

export const fixEdgeSkirt = (
resolution: number,
Expand All @@ -14,8 +17,15 @@ export const fixEdgeSkirt = (
inverted: boolean,
) => {
const effectiveResolution = resolution + 2
const copyNormalsFromTopology = true

const _ApplyFix = (x: number, y: number, xp: number, yp: number) => {
const _ApplyFix = (
x: number,
y: number,
xp: number,
yp: number,
normal: Vector3,
) => {
const skirtIndex = x * (effectiveResolution + 1) + y
const proxyIndex = xp * (effectiveResolution + 1) + yp

Expand All @@ -39,21 +49,56 @@ export const fixEdgeSkirt = (
positions[skirtIndex * 3 + 1] = _P.y
positions[skirtIndex * 3 + 2] = _P.z

normals[skirtIndex * 3 + 0] = normals[proxyIndex * 3 + 0]
normals[skirtIndex * 3 + 1] = normals[proxyIndex * 3 + 1]
normals[skirtIndex * 3 + 2] = normals[proxyIndex * 3 + 2]
}
// this copies the normals from the other one, but what if we don't want that?
if (copyNormalsFromTopology) {
normals[skirtIndex * 3 + 0] = normals[proxyIndex * 3 + 0]
normals[skirtIndex * 3 + 1] = normals[proxyIndex * 3 + 1]
normals[skirtIndex * 3 + 2] = normals[proxyIndex * 3 + 2]
} else {
// calculate REAL normals

for (let y = 0; y <= effectiveResolution; ++y) {
_ApplyFix(0, y, 1, y)
// get skirt edge normal
normals[skirtIndex * 3 + 0] = normal.x
normals[skirtIndex * 3 + 1] = normal.y
normals[skirtIndex * 3 + 2] = normal.z

normals[proxyIndex * 3 + 0] = normal.x
normals[proxyIndex * 3 + 1] = normal.y
normals[proxyIndex * 3 + 2] = normal.z
}
}

for (let y = 0; y <= effectiveResolution; ++y) {
_ApplyFix(effectiveResolution, y, effectiveResolution - 1, y)
}
for (let x = 0; x <= effectiveResolution; ++x) {
_ApplyFix(x, 0, x, 1)
}
for (let x = 0; x <= effectiveResolution; ++x) {
_ApplyFix(x, effectiveResolution, x, effectiveResolution - 1)
_ApplyFix(0, y, 1, y, new Vector3(1, 0, 0).random().normalize())
_ApplyFix(
effectiveResolution,
y,
effectiveResolution - 1,
y,
new Vector3(-1, 0, 0).random().normalize(),
)

// because this is a square, we can do this loop at the same time
const x = y

_ApplyFix(x, 0, x, 1, new Vector3(0, 0, 1).random().normalize())
_ApplyFix(
x,
effectiveResolution,
x,
effectiveResolution - 1,
new Vector3(0, 0, -1).random().normalize(),
)
}

// for (let x = 0; x <= effectiveResolution; ++x) {
// _ApplyFix(x, 0, x, 1, new Vector3(0, 0, 1).random().normalize())
// _ApplyFix(
// x,
// effectiveResolution,
// x,
// effectiveResolution - 1,
// new Vector3(0, 0, -1).random().normalize(),
// )
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import {
import { tempColor } from "../../utils"

const _D = new Vector3()

const _P = new Vector3()

const _H = new Vector3()
const _W = new Vector3()
// const _S = new Vector3()
const _C = new Vector3()

const colorInputVector = new Vector3()

Expand All @@ -22,14 +18,6 @@ export interface GenerateInitialHeightsProps<D> extends ChunkGeneratorProps<D> {
colorGenerator?: ColorGenerator<D>
}

// function calculateUVCoordinates(x: number, y: numbe, width: number, height: number): [number, number] {
// // Normalize the coordinates to range [0, 1]
// const u = x / (this.width - 1);
// const v = y / (this.height - 1);

// return { u, v };
// }

export const generateInitialHeights = <D>(
params: GenerateInitialHeightsProps<D>,
) => {
Expand Down Expand Up @@ -99,7 +87,7 @@ export const generateInitialHeights = <D>(
heightsMin = Math.min(heightsMin, height)
}

const color = colorGenerator
let color = colorGenerator
? colorGenerator({
input: colorInputVector.set(_W.x, _W.y, height).clone(),
worldPosition: _W.clone(),
Expand All @@ -115,6 +103,10 @@ export const generateInitialHeights = <D>(
})
: tempColor.set(0xffffff).clone()

// if (!notInSkirt) {
// color = skirtDebugColor
// }

// Purturb height along z-vector
_P.z += height * (params.inverted ? -1 : 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const _D1 = new Vector3()
const _D2 = new Vector3()

export const generateNormals = (
normals: number[],
positions: number[],
indices: number[],
): number[] => {
const normals = new Array(positions.length).fill(0.0)
for (let i = 0, n = indices.length; i < n; i += 3) {
const i1 = indices[i] * 3
const i2 = indices[i + 1] * 3
Expand Down

0 comments on commit 32acd09

Please sign in to comment.