Skip to content

Commit

Permalink
proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
DougReeder committed Mar 9, 2019
0 parents commit fee8072
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
assets/aframe-master*

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries


.gitignore~
.DS_Store
*.sw[mnop]

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
9 changes: 9 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2019 P. Douglas Reeder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
aframe-terrain-plain
====================

An [A-Frame](https://aframe.io) [WebVR](https://webvr.info/) primitive that has high-resolution terrain near the origin,
surrounded by a low-resolution plain that stretches to the horizon.
Uses equilateral triangles for efficiency.


[live example scene](https://dougreeder.github.io/aframe-terrain-plain/example.html)
8 changes: 8 additions & 0 deletions aframe-terrain-plain.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
87 changes: 87 additions & 0 deletions aframe-terrain-plain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// aframe-terrain-plain.js - An A-Frame WebVR primitive with high-resolution terrain surrounded by a low-res plain
// Copyright © 2019 P. Douglas Reeder under the MIT License


AFRAME.registerGeometry('terrain-plain', {
schema: {
unitSize: {type: 'number', default: 1, min: 0.1, max: 1000},
size: {type: 'number', default: 10, min: 1},
log: {type: 'boolean', default: false}
},
init: function (data) {
const SQRT3HALF = Math.sqrt(3) / 2;
const INNER_RADIUS = data.size * data.unitSize + 0.0001;
const OUTER_RADIUS = (data.size+1) * data.unitSize + 0.0001;
if (data.log) {console.log("init terrain-plain unitSize="+data.unitSize, " size="+data.size)}

let geometry = new THREE.Geometry();

let vertexLookup = {};
vertexLookup[-data.size-2] = {};
let vertexInd = 0;
// console.log("vertexLookup:", vertexLookup);
for (let i= -(data.size+1); i<=data.size+1; ++i) {
vertexLookup[i] = {};
for (let j= -(data.size+1); j<=data.size+1; ++j) {
let x = i * SQRT3HALF * data.unitSize;
let z = (j - i/2) * data.unitSize;
let r = Math.sqrt(x*x + z*z);
if (r <= OUTER_RADIUS) {
let y = r <= INNER_RADIUS ? 1.5 * r / INNER_RADIUS : 0;
if (data.log) {
console.log("i=" + i, "j=" + j, "x=" + x, "z=" + z, "y=" + y)
}

vertexLookup[i][j] = vertexInd++;
geometry.vertices.push(new THREE.Vector3(x, y, z));

let vertexA = vertexInd - 1;
let vertexB = vertexLookup[i][j-1];
let vertexC = vertexLookup[i-1][j-1];
let vertexD = vertexLookup[i-1][j];
if (typeof vertexB !== 'undefined' && typeof vertexC !== 'undefined') {
geometry.faces.push(new THREE.Face3(vertexA, vertexB, vertexC));
}
if (typeof vertexC !== 'undefined' && typeof vertexD !== 'undefined') {
geometry.faces.push(new THREE.Face3(vertexA, vertexC, vertexD));
}
} else {
if (data.log) {
console.log("excluding i=" + i, "j=" + j, "x=" + x, "z=" + z, "r="+r)
}
}
}
}
// console.log("vertexLookup:", vertexLookup);
geometry.computeBoundingBox();
geometry.mergeVertices();
geometry.computeFaceNormals();
geometry.computeVertexNormals();
this.geometry = geometry;
}
});


AFRAME.registerPrimitive('a-terrain-plain', {
defaultComponents: {
geometry: {
primitive: 'terrain-plain',
unitSize: 1,
size: 1,
log: false
},
material: {
shader: 'standard',
side: 'front'
}
},

mappings: {
'unit-size': 'geometry.unitSize',
'size': 'geometry.size',
'log': 'geometry.log',
'color': 'material.color',
'metalness': 'material.metalness',
'roughness': 'material.roughness',
}
});
42 changes: 42 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>aframe-terrain-plain primitive</title>
<meta name="description" content="An example scene with an aframe-terrain-plain and some polyhedrons">
<script src="https://aframe.io/releases/0.9.0/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-simple-sun-sky@^1.2.0/simple-sun-sky.js"></script>
<script src="aframe-terrain-plain.js"></script>
<!--<script src="https://unpkg.com/aframe-terrain-plain@^0.9.0/aframe-terrain-plain.js"></script>-->
<script>
AFRAME.registerComponent('moving-sun', {
init: function () {
this.positionSph = new THREE.Spherical(1, Math.PI/2, 0);
this.position = new THREE.Vector3();
this.sss = document.querySelector('a-simple-sun-sky');
this.directional = document.getElementById('directional');
},

tick: function (time) {
this.positionSph.phi = Math.PI * 0.4 + Math.sin(time / 32000 * 2 * Math.PI) * 0.4;
this.positionSph.theta = Math.PI + Math.sin(time / 48000 * 2 * Math.PI) * 0.6;
this.position.setFromSpherical(this.positionSph);
let positionStr = this.position.x + ' ' + this.position.y + ' ' + this.position.z;
// console.log(positionStr);
this.sss.setAttribute('sun-position', positionStr);
this.directional.setAttribute('position', positionStr);
}
});
</script>
</head>
<body>
<a-scene moving-sun>
<a-simple-sun-sky sun-position="0.7 0.4 -1"></a-simple-sun-sky>
<a-entity light="type: ambient; color: #BBB"></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 1.6 -5" radius="0.6" color="#EF2D5E" shadow></a-sphere>
<a-terrain-plain position="0 0 0" rotation="0 0 0" unit-size="10" log="true" color="#7BC8A4" metalness="0.1" roughness="0.75"></a-terrain-plain>
</a-scene>
</body>
</html>

0 comments on commit fee8072

Please sign in to comment.