Skip to content

Commit

Permalink
Adjusts terrain elevation so plateau elevation is exactly what's requ…
Browse files Browse the repository at this point in the history
…ested
  • Loading branch information
DougReeder committed Apr 13, 2023
1 parent e8fdcd7 commit f8d9109
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 34 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Usage

Include using
```html
<script src="https://unpkg.com/aframe-atoll-terrain@^0.7.0/dist/aframe-atoll-terrain.js"></script>
<script src="https://unpkg.com/aframe-atoll-terrain@^1.0.0/dist/aframe-atoll-terrain.js"></script>
```


Expand All @@ -44,12 +44,6 @@ Parameters
Typically, you'll leave the entity position and rotation as zero, but you don't have to.


### mean-elevation
* default: 10

The average elevation of the high-resolution area, in meters (before the sea floods everything below 0).


### plateau-radius
* default: 10
* minimum: 0
Expand All @@ -59,10 +53,23 @@ Radius of the central plateau, in meters.
If radius is 0, there is no plateau.


### plateau-elevation-min
### plateau-elevation
* minimum: 0
* default: 1

Minimum elevation of the plateau. Can be 0, in which case the "plateau" might be sea or land.
Elevation of the plateau. If zero, forces there to be water in the center of the island.

If this is zero and `plateau-radius` is a large fraction of `middle-radius`, you'll have a ring-shaped island - an atoll!


### elevation-bias
* default: 0

When there is no plateau, this is the elevation of the high-resolution area before noise is applied, in meters.

When there is a plateau, the elevation of the high-resolution area is automatically adjusted so the plateau blends in.
Leave this at zero, unless you want a volcano or an isolated plateau.


### plateau-yin-color, plateau-yang-color
* default: land-yin-color, land-yang-color
Expand Down Expand Up @@ -111,7 +118,7 @@ The color of the sea smoothly varies between these two extremes.
* default: {x:-1.0, y:1.0, z:-1.0}

The direction from which the sun is shining.
This primitive ignores directional lights, but others may use them.
This primitive ignores directional lights.
If you're using one of the *-sun-sky primitives, set its sun-position to the same value.


Expand All @@ -133,6 +140,17 @@ A number greater than zero and less than one, from which the terrain will be ran
Whether to write diagnostic data to the console.


Extracting Elevation data
-------------------------

The geometry component has a `getElevation` method, which returns the approximate elevation of a point:

```javascript
const terrainGeometry = this.el.querySelector('a-atoll-terrain').getAttribute('geometry');
const spherePosition = document.querySelector('a-sphere').getAttribute('position');
spherePosition.y = terrainGeometry.getElevation(spherePosition.x, spherePosition.z) + 1.6;
```


Development
---
Expand Down
2 changes: 1 addition & 1 deletion dist/aframe-atoll-terrain.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/aframe-atoll-terrain.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@
<a-entity light="type: ambient; color: #888"></a-entity>
<a-entity id="directional" light="type: directional; color: #FFF; intensity: 0.6" position="0.7 0.4 -1"></a-entity>

<a-sphere position="0 11.6 -5" radius="0.6" color="#EF2D5E" shadow></a-sphere>
<a-sphere position="0 11.6 -2" radius="0.6" color="#EF2D5E" shadow></a-sphere>
<!-- terrain which is likely to have a good vista from the plateau, and runs ok on mobile -->
<a-atoll-terrain mean-elevation="3"
plateau-radius="10"
<a-atoll-terrain plateau-radius="15" plateau-elevation="5"
elevation-bias="0"
middle-radius="300" unit-size="2" far="1900"
sun-position="0.8 0.6 0"
log="true" flat-shading="false">
</a-atoll-terrain>

<a-entity id="armature" position="0 10 0" movement-controls="fly:true; speed:0.5">
<a-entity camera="far:2500; near:0.05" position="0 1.6 0" look-controls="pointerLockEnabled: true"></a-entity>
<a-entity id="armature" position="0 10 2" movement-controls="fly:true; speed:0.5">
<a-entity camera="far:2500; near:0.05" position="0 1.6 0" look-controls="pointerLockEnabled: false"></a-entity>
<a-entity hand-controls="hand:left"></a-entity>
<a-entity hand-controls="hand:right"></a-entity>
</a-entity>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aframe-atoll-terrain",
"version": "0.7.3",
"version": "1.0.0",
"description": "An A-Frame WebVR primitive that has a circle of high-resolution terrain near the origin, surrounded by a low-resolution plain that stretches to the horizon",
"scripts": {
"build": "webpack",
Expand Down
30 changes: 17 additions & 13 deletions src/geometry-atoll-terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import ImprovedNoise from './ImprovedNoise';

AFRAME.registerGeometry('atoll-terrain', {
schema: {
buffer: {type: 'boolean', default: false},
meanElevation: {type: 'number', default: 10},
elevationBias: {type: 'number', default: 0},
plateauRadius: {type: 'number', default: 10, min: 0},
plateauElevationMin: {type: 'number', default: 1, min: 0},
plateauElevation: {type: 'number', default: 1, min: 0},
plateauYinColor: {type: 'color', default: undefined},
plateauYangColor: {type: 'color', default: undefined},
middleRadius: {type: 'number', default: 100, min: 10},
Expand Down Expand Up @@ -49,16 +48,21 @@ AFRAME.registerGeometry('atoll-terrain', {
const BEACH_COLOR = new THREE.Color(0x71615b); // brownish-gray beach
const ROCK_COLOR = new THREE.Color(0x837E7C); // granite

let plateauElevation = data.meanElevation;
// plateau has elevation of center
for (let quality = 25; quality <= 1500; quality *= 5) {
// The largest scale (625) is constrained to avoid all mountain or all sea.
plateauElevation += perlin.noise((0+data.middleRadius) / quality, (0+data.middleRadius) / quality, SEED) * Math.min(quality / 2, 62.5);
let elevationOffset;
if (data.plateauRadius > 0) {
let smoothNoise = 0;
// plateau has elevation of center
for (let quality = 25; quality <= 1500; quality *= 5) {
// The largest scale (625) is constrained to avoid all mountain or all sea.
smoothNoise += perlin.noise((0+data.middleRadius) / quality, (0+data.middleRadius) / quality, SEED) * Math.min(quality / 2, 62.5);
}
elevationOffset = data.plateauElevation - smoothNoise + data.elevationBias;
} else {
elevationOffset = data.elevationBias;
}
plateauElevation = Math.max(plateauElevation, data.plateauElevationMin);

if (data.log) {
console.log("atoll-terrain", "SEED="+SEED, "PLATEAU_EDGE="+PLATEAU_EDGE, "plateauElevation="+plateauElevation,
console.log("atoll-terrain", "SEED="+SEED, "PLATEAU_EDGE="+PLATEAU_EDGE, "elevationOffset="+elevationOffset,
"SIZE="+SIZE, "SCAN_SIZE="+SCAN_SIZE, "UNIT_SIZE="+UNIT_SIZE,
"middleRadius="+data.middleRadius, "FAR="+FAR);
}
Expand All @@ -79,17 +83,17 @@ AFRAME.registerGeometry('atoll-terrain', {
if (r <= OUTER_RADIUS) {
let y;
if (data.plateauRadius > 0 && r <= PLATEAU_EDGE) {
y = plateauElevation;
y = data.plateauElevation;
} else if (r <= INNER_RADIUS) {
y = data.meanElevation;
y = elevationOffset;
// generates smooth noisy terrain
for (let quality = 25; quality <= 1500; quality *= 5) {
// The largest scale (625) is constrained to avoid all mountain or all sea.
y += perlin.noise((x+data.middleRadius) / quality, (z+data.middleRadius) / quality, SEED) * Math.min(quality / 2, 62.5);
}

if (data.plateauRadius > 0) {
y = plateauElevation + (y - plateauElevation) * Math.min(r - PLATEAU_EDGE, MASK_EDGE) / MASK_EDGE;
y = data.plateauElevation + (y - data.plateauElevation) * Math.min(r - PLATEAU_EDGE, MASK_EDGE) / MASK_EDGE;
}
y *= Math.min(INNER_RADIUS - r, MASK_EDGE) / MASK_EDGE;

Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ AFRAME.registerPrimitive('a-atoll-terrain', {
},

mappings: {
'mean-elevation': 'geometry.meanElevation',
'elevation-bias': 'geometry.elevationBias',
'plateau-radius': 'geometry.plateauRadius',
'plateau-elevation-min': 'geometry.plateauElevationMin',
'plateau-elevation': 'geometry.plateauElevation',
'plateau-yin-color': 'geometry.plateauYinColor',
'plateau-yang-color': 'geometry.plateauYangColor',
'middle-radius': 'geometry.middleRadius',
Expand Down

0 comments on commit f8d9109

Please sign in to comment.