From fee80726049742c1e21d84154fe59e58d89fec13 Mon Sep 17 00:00:00 2001 From: "P. Douglas Reeder" Date: Fri, 8 Mar 2019 21:49:14 -0500 Subject: [PATCH] proof of concept --- .gitignore | 53 ++++++++++++++++++++++++ .idea/misc.xml | 9 +++++ .idea/modules.xml | 8 ++++ LICENSE.md | 7 ++++ README.md | 9 +++++ aframe-terrain-plain.iml | 8 ++++ aframe-terrain-plain.js | 87 ++++++++++++++++++++++++++++++++++++++++ example.html | 42 +++++++++++++++++++ 8 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 aframe-terrain-plain.iml create mode 100644 aframe-terrain-plain.js create mode 100644 example.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e63556 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5e35a10 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d4d41e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..0f26891 --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..64cf3e0 --- /dev/null +++ b/README.md @@ -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) diff --git a/aframe-terrain-plain.iml b/aframe-terrain-plain.iml new file mode 100644 index 0000000..80cc739 --- /dev/null +++ b/aframe-terrain-plain.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/aframe-terrain-plain.js b/aframe-terrain-plain.js new file mode 100644 index 0000000..731ea75 --- /dev/null +++ b/aframe-terrain-plain.js @@ -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', + } +}); diff --git a/example.html b/example.html new file mode 100644 index 0000000..e537774 --- /dev/null +++ b/example.html @@ -0,0 +1,42 @@ + + + + + aframe-terrain-plain primitive + + + + + + + + + + + + + + + + + +