Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add subtractBox option #47

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions examples/subtract-box/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A-Frame 3D Tiles Component - Basic example</title>
<meta name="description" content="Basic example for 3D Tiles component showing google 3d Tiles."></meta>
<script src="https://aframe.io/releases/1.5.0/aframe.min.js"></script>
<script src="../../dist/aframe-loader-3dtiles-component.js"></script>
<!-- <script src="https://unpkg.com/[email protected]/dist/aframe-street-component.js"></script> -->

<!-- vr teleport controls -->
<!-- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe-blink-controls.min.js"></script> -->
<style>
#guide {
position: fixed;
top: 0;
right: 0;
width: 300px;
padding: 1rem 2rem;
font-family:'Courier New', Courier, monospace;
line-height: 1.2;
background-color: white;
color: black;
}

#guide p {
margin-top: 10px;
}
</style>
</head>
<body>
<a-scene vr-mode-ui="enabled: false" renderer="colorManagement: true;">
<a-entity camera="fov:45; near:1; far: 1000" look-controls="reverseMouseDrag: true" wasd-controls="enabled: true" id="camera" cursor-teleport="cameraRig: #camera; cameraHead: #camera;"></a-entity>
<a-entity
id="tileset"
loader-3dtiles="
url: https://tile.googleapis.com/v1/3dtiles/root.json;
lat: 37.77522354250163;
long: -122.41931773049723;
height: 50;
googleApiKey: AIzaSyAQshwLVKTpwTfPJxFEkEzOdP_cgmixTCQ;
geoTransform: WGS84Cartesian;
maximumSSE: 48;
maximumMem: 400;
cameraEl: #camera;
subtractBox: #subtract"
>
</a-entity>

<a-box id="subtract" position="0 -10 0" geometry="depth:60; height:20; width:32" material="transparent: true; opacity:0.5"></a-box>

</a-scene>

<!-- GitHub Corner. -->
<a href="https://github.com/nytimes/aframe-loader-3dtiles-component" class="github-corner">
<svg width="80" height="80" viewBox="0 0 250 250" style="fill:#222; color:#fff; position: absolute; top: 0; border: 0; right: 0;">
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path>
</svg>
</a>
<div id="guide">
<span id="example-desc">
<b>Google 3D Tiles example.</b>
</span>
<p>Use arrow/WASD keys to move around and click and drag to turn/rotate the camera.</p>
<p>
<u>Available component parameters:</u>
<ul>
<li><b>lat, long</b>: coordinates of Google Map.</li>
<li><b>height</b>: camera height.</li>
<li><b>googleApiKey</b>: Google Api Key.</li>
</ul>
</p>
</div>
<script>
const queryParams = new URLSearchParams(document.location.search);

if (queryParams.get('tilesetUrl')) {
document.querySelector('#tileset').addEventListener('object3dset', (e) => {
e.target.setAttribute('loader-3dtiles', {
url: queryParams.get('tilesetUrl')
})
})
}
</script>
<style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<!-- End GitHub Corner. -->
</body>
</html>
64 changes: 62 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ AFRAME.registerComponent('loader-3dtiles', {
lat: { type: 'number' },
long: { type: 'number' },
height: { type: 'number' },
geoTransform: { type: 'string', default: 'Reset' }
geoTransform: { type: 'string', default: 'Reset' },
subtractBox: {type: 'selector'}
},
init: async function () {
this.camera = this.data.cameraEl?.object3D.children[0] ?? document.querySelector('a-scene').camera;
Expand Down Expand Up @@ -86,6 +87,41 @@ AFRAME.registerComponent('loader-3dtiles', {
await this._nextFrame();
this.runtime = runtime;
this.runtime.setElevationRange(this.data.pointcloudElevationRange.map(n => Number(n)));

},
setSubtractShaders: function () {
this.fragmentShader = `
varying vec3 vNormal;
varying vec3 vPosition;
varying vec2 vUv;

uniform sampler2D map;
uniform vec3 boxMin;
uniform vec3 boxMax;

void main() {
// Check if the fragment position is inside the bounding box
if (vPosition.x > boxMin.x && vPosition.y > boxMin.y && vPosition.z > boxMin.z &&
vPosition.x < boxMax.x && vPosition.y < boxMax.y && vPosition.z < boxMax.z) {
discard; // Discard the fragment if inside the bounding box
}

// Otherwise, keep the fragment color
gl_FragColor = texture2D(map, vUv);
}`
this.vertexShader = `
varying vec3 vNormal;
varying vec3 vPosition;
varying vec2 vUv;

void main() {
vUv = uv;
vNormal = normalize(normalMatrix * normal);
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;

},
update: async function (oldData) {
if (oldData.url !== this.data.url) {
Expand Down Expand Up @@ -116,6 +152,9 @@ AFRAME.registerComponent('loader-3dtiles', {

// set parameters for google 3dtiles API
if (this.data.lat && this.data.long && this.data.height) {
// create shanders for subtract box
this.setSubtractShaders();

// eslint-disable-next-line no-unused-vars
const { model, runtime } = await this._initTileset();

Expand Down Expand Up @@ -147,6 +186,23 @@ AFRAME.registerComponent('loader-3dtiles', {
}
}
},
shaderCallback: function (renderer, material) {
if (this.data.subtractBox) {
const threeBox = new THREE.Box3().setFromObject(this.data.subtractBox.object3D);

const uniforms = {
boxMin: {value: threeBox.min || new THREE.Vector3()},
boxMax: {value: threeBox.max || new THREE.Vector3()}
};
const fragmentShader = this.fragmentShader;
const vertexShader = this.vertexShader;
material.onBeforeCompile = function(shader) {
shader.uniforms = THREE.UniformsUtils.merge([shader.uniforms, uniforms]);
shader.fragmentShader = fragmentShader;
shader.vertexShader = vertexShader;
}
}
},
remove: function () {
if (this.runtime) {
this.runtime.dispose();
Expand All @@ -162,6 +218,7 @@ AFRAME.registerComponent('loader-3dtiles', {
}
},
_initTileset: async function () {
this.shaderCallback = this.shaderCallback.bind(this);
const pointCloudColoring = this._resolvePointcloudColoring(this.data.pointcloudColoring);
return Loader3DTiles.load({
url: this.data.url,
Expand All @@ -178,7 +235,10 @@ AFRAME.registerComponent('loader-3dtiles', {
viewDistanceScale: this.data.distanceScale,
wireframe: this.data.wireframe,
updateTransforms: true,
geoTransform: GeoTransform[this.data.geoTransform]
geoTransform: GeoTransform[this.data.geoTransform],
shaderCallback: this.shaderCallback,
shading: 2,
transparent: true
}
});
},
Expand Down