Display a Potree point cloud.

Parameters
100% © Potree
index.js
import { Vector3 } from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

import Instance from "@giro3d/giro3d/core/Instance.js";
import PotreePointCloud from "@giro3d/giro3d/entities/PotreePointCloud.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import PotreeSource from "@giro3d/giro3d/sources/PotreeSource.js";

function bindToggle(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLButtonElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.checked);
  };

  const callback = (v) => {
    element.checked = v;
    onChange(element.checked);
  };

  return [callback, element.checked, element];
}

function bindSlider(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLInputElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.valueAsNumber);
  };

  const setValue = (v) => {
    element.valueAsNumber = v;
    onChange(element.valueAsNumber);
  };

  const initialValue = element.valueAsNumber;

  return [setValue, initialValue, element];
}

function updateLabel(id, text) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLLabelElement)) {
    throw new Error(
      "invalid binding element: expected HTMLLabelElement, got: " +
        element.constructor.name,
    );
  }

  element.innerText = text;
}

const source = new PotreeSource(
  "https://3d.oslandia.com/potree/pointclouds/lion_takanawa",
);

const instance = new Instance({
  target: "view",
  crs: "EPSG:3857",
  backgroundColor: null,
});

function placeCamera() {
  const camera = instance.view.camera;

  const pos = new Vector3(7.5, -9.14, 9.1);
  const lookAt = new Vector3(0.5, 0, 4.3);

  const controls = new OrbitControls(camera, instance.domElement);
  controls.enableDamping = true;
  controls.dampingFactor = 0.25;

  camera.position.copy(pos);

  camera.lookAt(lookAt);
  controls.target.copy(lookAt);

  instance.view.setControls(controls);

  instance.notifyChange(instance.view.camera);

  Inspector.attach("inspector", instance);
}

const potree = new PotreePointCloud(source);

instance.add(potree).then(placeCamera).catch(console.error);

const renderingOptions = instance.renderingOptions;

renderingOptions.enableEDL = true;
renderingOptions.enableInpainting = true;
renderingOptions.inpaintingSteps = 1;
renderingOptions.enablePointCloudOcclusion = true;

bindToggle("edl", (v) => {
  renderingOptions.enableEDL = v;
  instance.notifyChange();
});
bindToggle("inpainting", (v) => {
  renderingOptions.enableInpainting = v;
  instance.notifyChange();
});
bindSlider("size", (v) => {
  potree.pointSize = v;
  updateLabel("size-label", "Point size: " + (v > 0 ? v.toString() : "auto"));
  instance.notifyChange(potree);
});
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Potree Point Cloud</title>
    <meta charset="UTF-8" />
    <meta name="name" content="potree_pointcloud" />
    <meta name="description" content="Display a Potree point cloud." />
    <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"
    />

    <style>
      #view canvas {
        background: rgb(132, 170, 182);
        background: radial-gradient(
          circle,
          rgba(132, 170, 182, 1) 0%,
          rgba(37, 44, 48, 1) 100%
        );
      }
    </style>
  </head>

  <body>
    <div id="view" class="m-0 p-0 w-100 h-100"></div>
    <div
      id="inspector"
      class="position-absolute top-0 start-0 mh-100 overflow-auto"
    ></div>

    <div class="side-pane-with-status-bar">
      <div class="card">
        <div class="card-header">Parameters</div>
        <div class="card-body">
          <!-- EDL toggle -->
          <div class="form-check form-switch">
            <input
              class="form-check-input"
              type="checkbox"
              checked="true"
              role="switch"
              id="edl"
              autocomplete="off"
            />
            <label class="form-check-label" for="edl">Eye dome lighting</label>
          </div>

          <!-- Inpainting toggle -->
          <div class="form-check form-switch">
            <input
              class="form-check-input"
              type="checkbox"
              checked="true"
              role="switch"
              id="inpainting"
              autocomplete="off"
            />
            <label class="form-check-label" for="inpainting">Inpainting</label>
          </div>

          <!-- Point size slider -->
          <div class="row mt-2">
            <div class="col-auto">
              <label
                for="size"
                style="width: 8rem"
                id="size-label"
                class="form-label"
                >Point size: 4</label
              >
            </div>
            <div class="col">
              <input
                type="range"
                min="0"
                max="20"
                step="1"
                value="4"
                class="form-range"
                id="size"
                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>
package.json
{
    "name": "potree_pointcloud",
    "dependencies": {
        "@giro3d/giro3d": "0.39.0"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}