Commit 55a72d30 authored by Piotr Bialecki's avatar Piotr Bialecki Committed by Commit Bot

WebXR depth: add samples consuming new API

Add 2 samples leveraging prototype implementation of depth API.

Change-Id: I94514a905d694f168983f1afa9a9d4424a385188
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2417064
Commit-Queue: Piotr Bialecki <bialpio@chromium.org>
Reviewed-by: default avatarBrandon Jones <bajones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808485}
parent 1144484f
<!doctype html>
<!--
Copyright 2018 The Immersive Web Community Group
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.
-->
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
<meta name='mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<title>AR Depth API</title>
<link href='../css/common.css' rel='stylesheet'></link>
<!--The polyfill is not needed for browser that have native API support,
but is linked by these samples for wider compatibility.-->
<!--script src='https://cdn.jsdelivr.net/npm/webxr-polyfill@latest/build/webxr-polyfill.js'></script-->
<script src='../js/xrray-polyfill.js' type='module'></script>
<script src='../js/webxr-polyfill.js'></script>
<script src='../js/webxr-button.js'></script>
</head>
<body>
<header>
<details open>
<summary>AR Depth API - GPU access</summary>
<p>
This sample demonstrates use of a depth API in immersive-ar session.
The data will be uploaded to the GPU & accessed from a shader.
<a class="back" href="./index.html">Back</a>
</p>
</details>
</header>
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 aVertexPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
void main(void) {
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
vTexCoord = aTexCoord;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment" src="../shaders/depth-api-gpu.frag"></script>
<script id="turboFragment" type="x-shader/x-fragment" src="../shaders/turbo.glsl"></script>
<script type="module">
import {mat4, vec3, mat3, vec2} from '../js/cottontail/src/math/gl-matrix.js';
// XR globals.
let xrButton = null;
let xrRefSpace = null;
// WebGL scene globals.
let gl = null;
let shaderProgram = null;
let programInfo = null;
let vertexBuffer = null;
let depthTexture = null;
// shader code
let vertexShaderSource = null;
let fragmentShaderSource = null;
function initXR() {
xrButton = new XRDeviceButton({
onRequestSession: onRequestSession,
onEndSession: onEndSession,
textEnterXRTitle: "START AR",
textXRNotFoundTitle: "AR NOT FOUND",
textExitXRTitle: "EXIT AR",
supportedSessionTypes: ['immersive-ar']
});
document.querySelector('header').appendChild(xrButton.domElement);
}
function onRequestSession() {
// Requests an immersive session with environment integration.
let options = {
requiredFeatures: ['depth'],
};
navigator.xr.requestSession('immersive-ar', options).then((session) => {
session.mode = 'immersive-ar';
xrButton.setSession(session);
fetchShaders().then(() => {
onSessionStarted(session);
});
});
}
function onSessionStarted(session) {
session.addEventListener('end', onSessionEnded);
let canvas = document.createElement('canvas');
gl = canvas.getContext('webgl', {
xrCompatible: true
});
initializeGL();
session.updateRenderState({ baseLayer: new XRWebGLLayer(session, gl) });
session.requestReferenceSpace('local').then((refSpace) => {
xrRefSpace = refSpace;
session.requestAnimationFrame(onXRFrame);
});
}
function onEndSession(session) {
session.end();
}
function onSessionEnded(event) {
xrButton.setSession(null);
}
// Helper, fetches shader source code based on the passed in ID of the <script> element.
// Will inspect src attribute value and issue fetch API call to obtain the script body.
async function fetchShader(id) {
const element = document.getElementById(id);
const url = element.src;
const response = await fetch(url);
const text = await response.text();
return text;
}
async function fetchShaders() {
vertexShaderSource = document.getElementById('vertexShader').textContent;
fragmentShaderSource = await fetchShader("fragmentShader") + "\n"
+ await fetchShader("turboFragment");
}
function initializeGL() {
shaderProgram = initShaderProgram(vertexShaderSource, fragmentShaderSource);
programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
texCoord: gl.getAttribLocation(shaderProgram, 'aTexCoord'),
},
uniformLocations: {
depthTexture: gl.getUniformLocation(shaderProgram, 'uDepthTexture'),
uvTransform: gl.getUniformLocation(shaderProgram, 'uUvTransform'),
},
};
vertexBuffer = gl.createBuffer();
// clip space coordinates + texture space coordinates
const vertices_data = [
-1, -1, 0, 0, // bottom left
1, -1, 1, 0, // bottom right
-1, 1, 0, 1, // top left
1, 1, 1, 1, // top right
];
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices_data), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
depthTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
// depth texture will likely not be power-of-2-sized, set parameters
// that would still make it work, see
// https://www.khronos.org/webgl/wiki/WebGL_and_OpenGL_Differences#Non-Power_of_Two_Texture_Support
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
}
function initShaderProgram(vsSource, fsSource) {
const vertexShader = loadShader(gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(gl.FRAGMENT_SHADER, fsSource);
// Create the shader program
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program: " +
gl.getProgramInfoLog(shaderProgram)
);
return null;
}
return shaderProgram;
}
function loadShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(
"An error occurred compiling the shaders: " +
gl.getShaderInfoLog(shader)
);
gl.deleteShader(shader);
return null;
}
return shader;
}
// Called every time a XRSession requests that a new frame be drawn.
function onXRFrame(t, frame) {
const session = frame.session;
session.requestAnimationFrame(onXRFrame);
const baseLayer = session.renderState.baseLayer;
const pose = frame.getViewerPose(xrRefSpace);
if(pose) {
gl.bindFramebuffer(gl.FRAMEBUFFER, session.renderState.baseLayer.framebuffer);
// Clear the framebuffer
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for(const view of pose.views) {
const viewport = baseLayer.getViewport(view);
gl.viewport(viewport.x, viewport.y,
viewport.width, viewport.height);
const depthData = frame.getDepthInformation(view);
if(depthData){
renderDepthInformationGPU(depthData, view, viewport);
}
}
}
}
function renderDepthInformationGPU(depthData, view, viewport) {
const depth_width = depthData.width;
const depth_height = depthData.height;
gl.useProgram(programInfo.program);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
2, // 2 components
gl.FLOAT,
false, // don't normalize
16, // stride = 4 floats * 4 bytes
0 // start at offset 0 of the buffer
);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexPosition
);
gl.vertexAttribPointer(
programInfo.attribLocations.texCoord,
2, // 2 components
gl.FLOAT,
false, // don't normalize
16, // stride = 4 floats * 4 bytes
8 // start at offset of 2 floats * 4 bytes of the buffer
);
gl.enableVertexAttribArray(
programInfo.attribLocations.texCoord
);
const dataBuffer = depthData.data;
gl.bindTexture(gl.TEXTURE_2D, depthTexture);
// Supply the data buffer after converting it to Uint8Array - the
// dataBuffer.buffer is an Uint16Array, but the gl.texImage2D expects Uint8Array
// when using gl.UNSIGNED_BYTE type.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, depthData.width,
depthData.height, 0, gl.LUMINANCE_ALPHA, gl.UNSIGNED_BYTE,
new Uint8Array(dataBuffer.buffer, dataBuffer.byteOffset, dataBuffer.byteLength));
gl.activeTexture(gl.TEXTURE0);
gl.uniform1i(programInfo.uniformLocations.depthTexture, 0);
gl.uniformMatrix4fv(programInfo.uniformLocations.uvTransform, false,
depthData.normTextureFromNormView.matrix);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
// Start the XR application.
initXR();
</script>
</body>
</html>
<!doctype html>
<!--
Copyright 2018 The Immersive Web Community Group
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.
-->
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
<meta name='mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<title>AR Depth API</title>
<link href='../css/common.css' rel='stylesheet'></link>
<!--The polyfill is not needed for browser that have native API support,
but is linked by these samples for wider compatibility.-->
<!--script src='https://cdn.jsdelivr.net/npm/webxr-polyfill@latest/build/webxr-polyfill.js'></script-->
<script src='../js/xrray-polyfill.js' type='module'></script>
<script src='../js/webxr-polyfill.js'></script>
<script src='../js/webxr-button.js'></script>
</head>
<body>
<header>
<details open>
<summary>AR Depth API - CPU access</summary>
<p>
This sample demonstrates use of a depth API in immersive-ar session.
The data will be accessed from a CPU.
<a class="back" href="./index.html">Back</a>
</p>
</details>
</header>
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 aVertexPosition;
attribute float aDepthDistance;
varying float vDepthDistance;
void main(void) {
gl_PointSize = 15.0;
gl_Position = vec4(aVertexPosition, -1.0, 1.0);
vDepthDistance = aDepthDistance;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment" src="../shaders/depth-api-cpu.frag"></script>
<script id="turboFragment" type="x-shader/x-fragment" src="../shaders/turbo.glsl"></script>
<script type="module" async>
import {mat4, vec3, mat3, vec2} from '../js/cottontail/src/math/gl-matrix.js';
// XR globals.
let xrButton = null;
let xrRefSpace = null;
// WebGL scene globals.
let gl = null;
let shaderProgram = null;
let programInfo = null;
let vertexBuffer = null;
// shader code
let vertexShaderSource = null;
let fragmentShaderSource = null;
function initXR() {
xrButton = new XRDeviceButton({
onRequestSession: onRequestSession,
onEndSession: onEndSession,
textEnterXRTitle: "START AR",
textXRNotFoundTitle: "AR NOT FOUND",
textExitXRTitle: "EXIT AR",
supportedSessionTypes: ['immersive-ar']
});
document.querySelector('header').appendChild(xrButton.domElement);
}
function onRequestSession() {
// Requests an immersive session with environment integration.
let options = {
requiredFeatures: ['depth'],
};
navigator.xr.requestSession('immersive-ar', options).then((session) => {
session.mode = 'immersive-ar';
xrButton.setSession(session);
fetchShaders().then(() => {
onSessionStarted(session);
})
});
}
function onSessionStarted(session) {
session.addEventListener('end', onSessionEnded);
let canvas = document.createElement('canvas');
gl = canvas.getContext('webgl', {
xrCompatible: true
});
initializeGL();
session.updateRenderState({ baseLayer: new XRWebGLLayer(session, gl) });
session.requestReferenceSpace('local').then((refSpace) => {
xrRefSpace = refSpace;
session.requestAnimationFrame(onXRFrame);
});
}
function onEndSession(session) {
session.end();
}
function onSessionEnded(event) {
xrButton.setSession(null);
}
// Helper, fetches shader source code based on the passed in ID of the <script> element.
// Will inspect src attribute value and issue fetch API call to obtain the script body.
async function fetchShader(id) {
const element = document.getElementById(id);
const url = element.src;
const response = await fetch(url);
const text = await response.text();
return text;
}
async function fetchShaders() {
vertexShaderSource = document.getElementById('vertexShader').textContent;
fragmentShaderSource = await fetchShader("fragmentShader") + "\n"
+ await fetchShader("turboFragment");
}
function initializeGL() {
shaderProgram = initShaderProgram(vertexShaderSource, fragmentShaderSource);
programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
depthDistance: gl.getAttribLocation(shaderProgram, 'aDepthDistance'),
},
uniformLocations: {
},
};
vertexBuffer = gl.createBuffer();
}
function initShaderProgram(vsSource, fsSource) {
const vertexShader = loadShader(gl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(gl.FRAGMENT_SHADER, fsSource);
// Create the shader program
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program: " +
gl.getProgramInfoLog(shaderProgram)
);
return null;
}
return shaderProgram;
}
function loadShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(
"An error occurred compiling the shaders: " +
gl.getShaderInfoLog(shader)
);
gl.deleteShader(shader);
return null;
}
return shader;
}
// Component-wise multiplication of 2 vec3s:
function scaleByVec(out, lhs, rhs) {
out[0] = lhs[0] * rhs[0];
out[1] = lhs[1] * rhs[1];
out[2] = lhs[2] * rhs[2];
return out;
}
function clamp(out, input, lower_bound, upper_bound) {
out[0] = Math.max(lower_bound[0], Math.min(input[0], upper_bound[0]));
out[1] = Math.max(lower_bound[1], Math.min(input[1], upper_bound[1]));
out[2] = Math.max(lower_bound[2], Math.min(input[2], upper_bound[2]));
return out;
}
// Called every time a XRSession requests that a new frame be drawn.
function onXRFrame(t, frame) {
const session = frame.session;
session.requestAnimationFrame(onXRFrame);
const baseLayer = session.renderState.baseLayer;
const pose = frame.getViewerPose(xrRefSpace);
if(pose) {
gl.bindFramebuffer(gl.FRAMEBUFFER, session.renderState.baseLayer.framebuffer);
// Clear the framebuffer
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for(const view of pose.views) {
const viewport = baseLayer.getViewport(view);
gl.viewport(viewport.x, viewport.y,
viewport.width, viewport.height);
const depthData = frame.getDepthInformation(view);
if(depthData){
renderDepthInformationCPU(depthData, view, viewport);
}
}
}
}
function renderDepthInformationCPU(depthData, view, viewport) {
const RESOLUTION = 5;
const depth_width = depthData.width;
const depth_height = depthData.height;
const norm_tex_from_norm_view = depthData.normTextureFromNormView.matrix;
const norm_view_from_norm_tex = mat4.invert(mat4.create(), norm_tex_from_norm_view);
const inverse_depth_dimensions = vec3.fromValues(1.0 / depth_width,
1.0 / depth_height,
0);
const viewport_dimensions = vec3.fromValues(viewport.width, viewport.height, 0);
const depth_vec = vec3.fromValues(depth_width-1, depth_height-1, 0);
const zeroes_vec = vec3.fromValues(0, 0, 0);
const vertices_data = [];
for(let x = 0; x < depth_width; x = x + RESOLUTION) {
for(let y = 0; y < depth_height; y = y + RESOLUTION) {
const distance = depthData.getDepth(x, y);
const depth_coords = vec3.fromValues(x, y, 0);
// Calculate the view coordinates that correspond to (x, y) depth coordiante:
// 1. Normalize to [0, 1] range:
const depth_coords_norm = scaleByVec(vec3.create(),
depth_coords, inverse_depth_dimensions);
// 2. Map from normalized texture coordinates to normalized view coordinates:
const depth_coords_view_norm = vec3.transformMat4(vec3.create(),
depth_coords_norm,
norm_view_from_norm_tex);
// 3. Denormalize by viewport dimensions:
const depth_coords_view = scaleByVec(vec3.create(),
depth_coords_view_norm, viewport_dimensions);
clamp(depth_coords_view, depth_coords_view, zeroes_vec, viewport_dimensions);
// Convert to NDC:
depth_coords_view[0] = (2.0 * depth_coords_view[0]) / viewport.width - 1;
depth_coords_view[1] = (2.0 * depth_coords_view[1]) / viewport.height - 1;
if(depth_coords_view[0] > 1 || depth_coords_view[0] < -1 ||
depth_coords_view[1] > 1 || depth_coords_view[1] < -1) {
continue;
}
vertices_data.push(depth_coords_view[0], depth_coords_view[1], distance);
}
}
gl.useProgram(programInfo.program);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices_data), gl.DYNAMIC_DRAW);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
2, // 2 components
gl.FLOAT,
false, // don't normalize
12, // stride = 3 floats * 4 bytes
0 // start at offset 0 of the buffer
);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexPosition
);
gl.vertexAttribPointer(
programInfo.attribLocations.depthDistance,
1, // 1 component
gl.FLOAT,
false, // don't normalize
12, // stride = 3 floats * 4 bytes
8 // start at offset of 2 floats * 4 bytes of the buffer
);
gl.enableVertexAttribArray(
programInfo.attribLocations.depthDistance
);
gl.drawArrays(gl.POINTS, 0, vertices_data.length / 3);
}
// Start the XR application.
initXR();
</script>
</body>
</html>
precision mediump float;
varying float vDepthDistance;
const highp float kMaxDepth = 8.0; // In meters.
const float kInvalidDepthThreshold = 0.01;
vec3 TurboColormap(in float x);
// Returns a color corresponding to the depth passed in. Colors range from red
// to green to blue, where red is closest and blue is farthest.
//
// Uses Turbo color mapping :
// https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
vec3 DepthGetColorVisualization(in float x) {
return step(kInvalidDepthThreshold, x) * TurboColormap(x);
}
void main(void) {
highp float normalized_depth = clamp(vDepthDistance / 8.0, 0.0, 1.0);
gl_FragColor = vec4(DepthGetColorVisualization(normalized_depth), 0.75);
}
// Insert turbo.glsl here.
precision mediump float;
uniform sampler2D uDepthTexture;
uniform mat4 uUvTransform;
varying vec2 vTexCoord;
float DepthGetMillimeters(in sampler2D depth_texture, in vec2 depth_uv) {
// Depth is packed into the luminance and alpha components of its texture.
// The texture is a normalized format, storing millimeters.
vec2 packedDepthAndVisibility = texture2D(depth_texture, depth_uv).ra;
return dot(packedDepthAndVisibility, vec2(255.0, 256.0 * 255.0));
}
const highp float kMaxDepth = 8000.0; // In millimeters.
const float kInvalidDepthThreshold = 0.01;
vec3 TurboColormap(in float x);
// Returns a color corresponding to the depth passed in. Colors range from red
// to green to blue, where red is closest and blue is farthest.
//
// Uses Turbo color mapping:
// https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html
vec3 DepthGetColorVisualization(in float x) {
return step(kInvalidDepthThreshold, x) * TurboColormap(x);
}
void main(void) {
vec2 texCoord = (uUvTransform * vec4(vTexCoord.xy, 0, 1)).xy;
highp float normalized_depth = clamp(
DepthGetMillimeters(uDepthTexture, texCoord) / kMaxDepth, 0.0, 1.0);
gl_FragColor = vec4(DepthGetColorVisualization(normalized_depth), 0.75);
}
// Insert turbo.glsl here.
/*
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Taken from:
// third_party/arcore-android-sdk/src/samples/augmented_image_c/app/src/main/assets/shaders/background_show_depth_color_visualization.frag
// & modified.
vec3 TurboColormap(in float x) {
const vec4 kRedVec4 = vec4(0.55305649, 3.00913185, -5.46192616, -11.11819092);
const vec4 kGreenVec4 = vec4(0.16207513, 0.17712472, 15.24091500, -36.50657960);
const vec4 kBlueVec4 = vec4(-0.05195877, 5.18000081, -30.94853351, 81.96403246);
const vec2 kRedVec2 = vec2(27.81927491, -14.87899417);
const vec2 kGreenVec2 = vec2(25.95549545, -5.02738237);
const vec2 kBlueVec2 = vec2(-86.53476570, 30.23299484);
// Adjusts color space via 6 degree poly interpolation to avoid pure red.
x = clamp(x * 0.9 + 0.03, 0.0, 1.0);
vec4 v4 = vec4( 1.0, x, x * x, x * x * x);
vec2 v2 = v4.zw * v4.z;
return vec3(
dot(v4, kRedVec4) + dot(v2, kRedVec2),
dot(v4, kGreenVec4) + dot(v2, kGreenVec2),
dot(v4, kBlueVec4) + dot(v2, kBlueVec2)
);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment