Colorize a point cloud with an image layer.

Options
Mode
100% © IGN

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.

index.js
import { Vector3 } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import TileWMS from 'ol/source/TileWMS.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';



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,
});

// 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;
});

document.getElementById('pointcloud_mode').addEventListener('change', e => {
    const newMode = parseInt(e.target.value, 10);
    material.mode = newMode;
    instance.notifyChange(pointcloud, true);
    if (colorLayer) {
        colorLayer.visible = newMode === MODE.TEXTURE;
        instance.notifyChange(colorLayer, true);
    }
});
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Colorized 3D Tiles Point Cloud</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <style>
    body {
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
    }

    #viewerDiv {
      width: 100%;
      height: 100%;
    }

    #panelDiv {
      position: absolute;
      top: 0;
      left: 0;
    }
    
  </style>
</head>

<body>
  <div id="viewerDiv"></div>
  <div id="panelDiv"></div>
  <div class="m-2 position-absolute top-0 end-0">
    <!-- Top color layer -->
    <div class="card m-1">
        <div class="card-header">Options</div>

        <fieldset class="container m-1" id="top-options">
            <!-- Coloring mode -->
            <div class="input-group p-2">
                <span class="input-group-text flex-grow-1">Mode</span>
                <select class="btn btn-outline-primary btn-sm" id="pointcloud_mode">
                    <option value="0">point cloud colors</option>
                    <option value="5">elevation gradient</option>
                    <option value="4" selected>color layer</option>
                </select>
            </div>

            <!-- Activate EDL -->
            <div class="input-group p-2">
                <div>
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            checked
                            type="checkbox"
                            role="switch"
                            id="edl-enable"
                        />
                        <label class="form-check-label" for="edl-enable"
                            >Eye dome lighting (EDL)</label
                        >
                    </div>
                </div>
            </div>

            <!-- EDL intensity -->
            <div class="input-group p-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="slider"
                        id="edl-intensity"
                    />
                </div>
            </div>

            <!-- EDL radius -->
            <div class="input-group p-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="slider"
                        id="edl-radius"
                    />
                </div>
            </div>

            <!-- Activate occlusion -->
            <div class="input-group p-2">
                <div>
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            checked
                            type="checkbox"
                            role="switch"
                            id="occlusion-enable"
                        />
                        <label class="form-check-label" for="occlusion-enable"
                            >Point occlusion effect</label
                        >
                    </div>
                </div>
            </div>

            <!-- Activate inpainting -->
            <div class="input-group p-2">
                <div>
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            checked
                            type="checkbox"
                            role="switch"
                            id="inpainting-enable"
                        />
                        <label class="form-check-label" for="inpainting-enable">Inpainting</label>
                    </div>
                </div>
            </div>

            <!-- Inpainting steps -->
            <div class="input-group p-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="slider"
                        id="inpainting-steps"
                    />
                </div>
            </div>
        </fieldset>
    </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>
package.json
{
  "name": "colorized_pointcloud",
  "dependencies": {
    "@giro3d/giro3d": "0.35.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}