Options
Mode
Color ramp
Illustrates colorization of point clouds with multiple modes.
Point clouds can be colorized from different methods : using the original color in the point cloud, coloring using an elevation gradient, or coloring using an external image source, such as a WMS layer.
import colormap from 'colormap';
import { Color, MathUtils, Vector3 } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import Tiles3D from '@giro3d/giro3d/entities/Tiles3D.js';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.js';
import PointCloudMaterial, { MODE } from '@giro3d/giro3d/renderer/PointCloudMaterial.js';
import Tiles3DSource from '@giro3d/giro3d/sources/Tiles3DSource.js';
import WmsSource from '@giro3d/giro3d/sources/WmsSource.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
function makeColorRamp(preset, nshades) {
const values = colormap({ colormap: preset, nshades });
const colors = values.map(v => new Color(v));
return colors;
}
const colorRamps = {};
function makeColorRamps() {
const nshades = 256;
colorRamps.viridis = makeColorRamp('viridis', nshades);
colorRamps.jet = makeColorRamp('jet', nshades);
colorRamps.blackbody = makeColorRamp('blackbody', nshades);
colorRamps.earth = makeColorRamp('earth', nshades);
colorRamps.bathymetry = makeColorRamp('bathymetry', nshades);
colorRamps.magma = makeColorRamp('magma', nshades);
colorRamps.par = makeColorRamp('par', nshades);
colorRamps.slope = makeColorRamp('RdBu', nshades);
}
makeColorRamps();
const tmpVec3 = new Vector3();
Instance.registerCRS(
'EPSG:3946',
'+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 ' +
'+y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs',
);
const viewerDiv = document.getElementById('viewerDiv');
const instance = new Instance(viewerDiv, {
crs: 'EPSG:3946',
renderer: {
clearColor: 0xcccccc,
},
});
// Create a custom material for our point cloud.
const material = new PointCloudMaterial({
size: 4,
mode: MODE.TEXTURE,
});
material.colorMap.min = 100;
material.colorMap.max = 600;
material.colorMap.colors = colorRamps['viridis'];
// Create the 3D tiles entity
const pointcloud = new Tiles3D(
'pointcloud',
new Tiles3DSource('https://3d.oslandia.com/3dtiles/lyon.3dtiles/tileset.json'),
{
material,
},
);
let colorLayer;
function placeCamera(position, lookAt) {
instance.camera.camera3D.position.set(position.x, position.y, position.z);
instance.camera.camera3D.lookAt(lookAt);
// create controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
controls.target.copy(lookAt);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
instance.useTHREEControls(controls);
instance.notifyChange(instance.camera.camera3D);
}
// add pointcloud to scene
function initializeCamera() {
const bbox = pointcloud.root.bbox
? pointcloud.root.bbox
: pointcloud.root.boundingVolume.box.clone().applyMatrix4(pointcloud.root.matrixWorld);
instance.camera.camera3D.far = 2.0 * bbox.getSize(tmpVec3).length();
const ratio = bbox.getSize(tmpVec3).x / bbox.getSize(tmpVec3).z;
const position = bbox.min
.clone()
.add(bbox.getSize(tmpVec3).multiply({ x: 0, y: 0, z: ratio * 0.5 }));
const lookAt = bbox.getCenter(tmpVec3);
lookAt.z = bbox.min.z;
const extent = Extent.fromBox3('EPSG:3946', bbox);
placeCamera(position, lookAt);
const colorize = new WmsSource({
url: 'https://data.geopf.fr/wms-r',
projection: 'EPSG:3946',
layer: 'HR.ORTHOIMAGERY.ORTHOPHOTOS',
imageFormat: 'image/jpeg',
});
colorLayer = new ColorLayer({
name: 'wms_imagery',
extent,
source: colorize,
});
pointcloud.attach(colorLayer);
instance.renderingOptions.enableEDL = true;
instance.renderingOptions.enableInpainting = true;
instance.renderingOptions.enablePointCloudOcclusion = true;
}
instance.add(pointcloud).then(initializeCamera);
Inspector.attach(document.getElementById('panelDiv'), instance);
instance.domElement.addEventListener('dblclick', e =>
console.log(
instance.pickObjectsAt(e, {
// Specify a radius around where we click so we don't have to precisely be on a point
// to select it
radius: 5,
// Limit the number of results for better performances
limit: 10,
// Some points are incoherent in the pointcloud, don't pick them
filter: p => !Number.isNaN(p.point.z) && p.point.z < 1000,
}),
),
);
instance.notifyChange();
function bindSlider(name, fn) {
const slider = document.getElementById(name);
slider.oninput = function oninput() {
fn(slider.value);
instance.notifyChange();
};
}
function bindToggle(name, action) {
const toggle = document.getElementById(name);
toggle.oninput = () => {
const state = toggle.checked;
action(state);
instance.notifyChange();
};
}
bindToggle('edl-enable', v => {
instance.renderingOptions.enableEDL = v;
});
bindToggle('occlusion-enable', v => {
instance.renderingOptions.enablePointCloudOcclusion = v;
});
bindToggle('inpainting-enable', v => {
instance.renderingOptions.enableInpainting = v;
});
bindSlider('edl-radius', v => {
instance.renderingOptions.EDLRadius = v;
});
bindSlider('edl-intensity', v => {
instance.renderingOptions.EDLStrength = v;
});
bindSlider('inpainting-steps', v => {
instance.renderingOptions.inpaintingSteps = v;
});
bindSlider('opacity', v => {
pointcloud.opacity = v;
document.getElementById('opacityLabel').innerText =
`Point cloud opacity: ${Math.round(v * 100)}%`;
});
function bindColorMapBounds(callback) {
/** @type {HTMLInputElement} */
const lower = document.getElementById('min');
/** @type {HTMLInputElement} */
const upper = document.getElementById('max');
callback(lower.valueAsNumber, upper.valueAsNumber);
function updateLabels() {
document.getElementById('minLabel').innerText =
`Lower bound: ${Math.round(lower.valueAsNumber)}m`;
document.getElementById('maxLabel').innerText =
`Upper bound: ${Math.round(upper.valueAsNumber)}m`;
}
lower.oninput = function oninput() {
const rawValue = lower.valueAsNumber;
const clampedValue = MathUtils.clamp(rawValue, lower.min, upper.valueAsNumber - 1);
lower.valueAsNumber = clampedValue;
callback(lower.valueAsNumber, upper.valueAsNumber);
instance.notifyChange();
updateLabels();
};
upper.oninput = function oninput() {
const rawValue = upper.valueAsNumber;
const clampedValue = MathUtils.clamp(rawValue, lower.valueAsNumber + 1, upper.max);
upper.valueAsNumber = clampedValue;
callback(lower.valueAsNumber, upper.valueAsNumber);
instance.notifyChange();
updateLabels();
};
}
bindColorMapBounds((min, max) => {
material.colorMap.min = min;
material.colorMap.max = max;
});
const colorMapGroup = document.getElementById('colormapGroup');
document.getElementById('pointcloud_mode').addEventListener('change', e => {
const newMode = parseInt(e.target.value, 10);
material.mode = newMode;
if (newMode === MODE.ELEVATION) {
colorMapGroup.classList.remove('d-none');
} else {
colorMapGroup.classList.add('d-none');
}
instance.notifyChange(pointcloud, true);
if (colorLayer) {
colorLayer.visible = newMode === MODE.TEXTURE;
instance.notifyChange(colorLayer, true);
}
});
document.getElementById('colormap').addEventListener('change', e => {
const newRamp = e.target.value;
material.colorMap.colors = colorRamps[newRamp];
instance.notifyChange(pointcloud, true);
});
<!doctype html>
<html lang="en">
<head>
<title>Colorized 3D Tiles Point Cloud</title>
<meta charset="UTF-8" />
<meta name="name" content="colorized_pointcloud" />
<meta name="description" content="Illustrates colorization of point clouds with multiple modes." />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="https://giro3d.org/images/favicon.svg" />
<link href="https://giro3d.org/assets/bootstrap-custom.css" rel="stylesheet" />
<script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://giro3d.org/latest/css/example.css"
/>
</head>
<body>
<div id="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
<div id="panelDiv" class="position-absolute top-0 start-0 mh-100 overflow-auto"></div>
<div class="side-pane-with-status-bar">
<!-- Top color layer -->
<div class="card">
<div class="card-header">Options</div>
<div class="card-body" id="top-options">
<!-- Coloring mode -->
<div class="input-group">
<span class="input-group-text flex-grow-1">Mode</span>
<select
class="btn btn-outline-primary btn-sm"
id="pointcloud_mode"
autocomplete="off"
>
<option value="0">point cloud colors</option>
<option value="5">elevation gradient</option>
<option value="4" selected>color layer</option>
</select>
</div>
<!-- Color ramp -->
<div class="d-none my-2" id="colormapGroup">
<div class="input-group">
<span class="input-group-text flex-grow-1">Color ramp</span>
<select class="btn btn-outline-primary btn-sm" id="colormap" autocomplete="off">
<option value="viridis" selected>Viridis</option>
<option value="jet">Jet</option>
<option value="blackbody">Blackbody</option>
<option value="earth">Earth</option>
<option value="bathymetry">Bathymetry</option>
<option value="magma">Magma</option>
<option value="par">Par</option>
</select>
</div>
<div class="input-group my-2">
<label for="min" class="form-label" id="minLabel">Lower bound: 100m</label>
<div class="input-group">
<input
type="range"
min="0"
max="1000"
value="100"
class="form-range"
id="min"
autocomplete="off"
/>
</div>
</div>
<div class="input-group my-2">
<label for="max" class="form-label" id="maxLabel">Upper bound: 600m</label>
<div class="input-group">
<input
type="range"
min="0"
max="1000"
value="600"
class="form-range"
id="max"
autocomplete="off"
/>
</div>
</div>
</div>
<!-- Opacity -->
<div class="input-group my-2">
<label for="opacity" class="form-label" id="opacityLabel"
>Point cloud opacity: 100%</label
>
<div class="input-group">
<input
type="range"
min="0"
max="1"
step="0.05"
value="1"
class="form-range"
id="opacity"
autocomplete="off"
/>
</div>
</div>
<!-- Activate EDL -->
<div class="input-group my-2">
<div>
<div class="form-check form-switch">
<input
class="form-check-input"
checked
type="checkbox"
role="switch"
id="edl-enable"
autocomplete="off"
/>
<label class="form-check-label" for="edl-enable"
>Eye dome lighting (EDL)</label
>
</div>
</div>
</div>
<!-- EDL intensity -->
<div class="input-group my-2">
<label for="edl-intensity" class="form-label">EDL intensity</label>
<div class="input-group">
<input
type="range"
min="0"
step="0.05"
max="2"
value="0.7"
class="form-range"
id="edl-intensity"
autocomplete="off"
/>
</div>
</div>
<!-- EDL radius -->
<div class="input-group my-2">
<label for="edl-radius" class="form-label">EDL radius</label>
<div class="input-group">
<input
type="range"
min="0.1"
step="0.05"
max="5"
value="1.5"
class="form-range"
id="edl-radius"
autocomplete="off"
/>
</div>
</div>
<!-- Activate occlusion -->
<div class="input-group my-2">
<div>
<div class="form-check form-switch">
<input
class="form-check-input"
checked
type="checkbox"
role="switch"
id="occlusion-enable"
autocomplete="off"
/>
<label class="form-check-label" for="occlusion-enable"
>Point occlusion effect</label
>
</div>
</div>
</div>
<!-- Activate inpainting -->
<div class="input-group my-2">
<div>
<div class="form-check form-switch">
<input
class="form-check-input"
checked
type="checkbox"
role="switch"
id="inpainting-enable"
autocomplete="off"
/>
<label class="form-check-label" for="inpainting-enable">Inpainting</label>
</div>
</div>
</div>
<!-- Inpainting steps -->
<div class="input-group my-2">
<label for="inpainting-steps" class="form-label">Inpainting steps</label>
<div class="input-group">
<input
type="range"
min="1"
step="1"
max="10"
value="2"
class="form-range"
id="inpainting-steps"
autocomplete="off"
/>
</div>
</div>
</div>
</div>
</div>
<script type="module" src="index.js"></script>
<script>
/* activate popovers */
const popoverTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="popover"]'),
);
popoverTriggerList.map(
// bootstrap is used as script in the template, disable warning about undef
// eslint-disable-next-line no-undef
popoverTriggerEl =>
new bootstrap.Popover(popoverTriggerEl, {
trigger: 'hover',
placement: 'left',
content: document.getElementById(
popoverTriggerEl.getAttribute('data-bs-content'),
).innerHTML,
html: true,
}),
);
</script>
</body>
</html>
{
"name": "colorized_pointcloud",
"dependencies": {
"colormap": "^2.3.2",
"@giro3d/giro3d": "0.37.3"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}