Skip to content

Commit

Permalink
Merge pull request #168 from KhronosGroup/feature/headless_select-cam…
Browse files Browse the repository at this point in the history
…era-index

Feature: select camera index in headless
  • Loading branch information
Benjamin Schmithüsen authored Jan 16, 2019
2 parents 4502d79 + cee54c2 commit ce0abeb
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 136 deletions.
243 changes: 129 additions & 114 deletions electron/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,129 +23,146 @@ const fs = require('fs');
const defaultModel = "assets/models/2.0/BoomBox/glTF/BoomBox.gltf";
const outputFile = "output.png";

let mainWindow;
const argv = process.argv;
const args = argv.lastIndexOf('--') !== -1 ? argv.slice(argv.lastIndexOf('--') + 1) : [];

let argv = process.argv;
let args = argv.lastIndexOf('--') !== -1 ? argv.slice(argv.lastIndexOf('--') + 1) : [];
const ArgumentParser = require('argparse').ArgumentParser;
const parsedArgs = parseArguments(args);
global.sharedObject = { args: parsedArgs };

let ArgumentParser = require('argparse').ArgumentParser;
let parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'glTF Reference Viewer'
});
app.on('ready', createWindow);

parser.addArgument(
['--eye-position'],
{
defaultValue: [0.0, 0.0, 1.0],
nargs: 3,
type: 'float',
help: "The coordinates of the eye (camera)."
}
);
parser.addArgument(
['--target-position'],
{
defaultValue: [0.0, 0.0, 0.0],
nargs: 3,
type: 'float',
help: "The coordinates of the eye focus point."
}
);
parser.addArgument(
'--up',
{
defaultValue: [0.0, 1.0, 0.0],
nargs: 3,
type: 'float',
help: "The up direction vector."
}
);
parser.addArgument(
'--projection',
{
defaultValue: "perspective",
help: "The projection mode of the camera",
choices: ["perspective", "ortographic"]
}
);
parser.addArgument(
'--znear',
{
defaultValue: 0.01,
type: 'float',
help: "The near clip plane"
}
);
parser.addArgument(
'--zfar',
{
defaultValue: 10000.0,
type: 'float',
help: "The far clip plane"
}
);
parser.addArgument(
'--yfov',
{
defaultValue: 45.0,
type: 'float',
help: "The vertical field of view in degrees."
}
);
parser.addArgument(
'--xmag',
{
defaultValue: 1.0,
type: 'float',
help: "The size of the ortographic camera in x direction."
}
);
parser.addArgument(
'--ymag',
{
defaultValue: 1.0,
type: 'float',
help: "The size of the ortographic camera in y direction."
}
);
parser.addArgument(
'--environment',
{
defaultValue: "Courtyard of the Doge's palace",
type: 'string',
help: 'The environment map to use for image based lighting.',
choices: Environments
}
);
parser.addArgument(
'gltf_path',
function parseArguments(args)
{
const parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'glTF Reference Viewer'
});

parser.addArgument(
['--camera-index'],
{
defaultValue: "orbit camera",
help: "Index of the glTF camera to use (instead of the orbit camera)."
}
);
parser.addArgument(
['--eye-position'],
{
defaultValue: [0.0, 0.0, 1.0],
metavar: ['X', 'Y', 'Z'],
nargs: 3,
type: 'float',
help: "The coordinates of the eye (camera)"
}
);
parser.addArgument(
['--target-position'],
{
defaultValue: [0.0, 0.0, 0.0],
metavar: ['X', 'Y', 'Z'],
nargs: 3,
type: 'float',
help: "The coordinates of the eye focus point"
}
);
parser.addArgument(
'--up',
{
defaultValue: [0.0, 1.0, 0.0],
metavar: ['X', 'Y', 'Z'],
nargs: 3,
type: 'float',
help: "The up direction vector"
}
);
parser.addArgument(
'--projection',
{
defaultValue: "perspective",
help: "The projection mode of the camera",
choices: ["perspective", "ortographic"]
}
);
parser.addArgument(
'--znear',
{
defaultValue: 0.01,
type: 'float',
help: "The near clip plane"
}
);
parser.addArgument(
'--zfar',
{
defaultValue: 10000.0,
type: 'float',
help: "The far clip plane"
}
);
parser.addArgument(
'--yfov',
{
defaultValue: 45.0,
type: 'float',
help: "The vertical field of view in degrees"
}
);
parser.addArgument(
'--xmag',
{
defaultValue: 1.0,
type: 'float',
help: "The size of the orthographic camera in x direction"
}
);
parser.addArgument(
'--ymag',
{
defaultValue: 1.0,
type: 'float',
help: "The size of the orthographic camera in y direction"
}
);
parser.addArgument(
'--environment',
{
defaultValue: "Courtyard of the Doge's palace",
type: 'string',
help: 'The environment map to use for image based lighting',
choices: Environments
}
);
parser.addArgument(
'gltf_path',
{
nargs: "?",
help: "The path of the glTF file"
}
);

const parsedArgs = parser.parseArgs(args);

if (parsedArgs.gltf_path === null)
{
nargs: "?",
help: "The path of the glTF file."
console.log("%s\n", parser.description);
console.info("IMPORTANT NOTICE: \n\
Add '-- --' to get your arguments through to the tool. \n\
Example: 'npm run start-offscreen -- -- --help'");
console.error("\nNo gltf_path was given, defaulting to '%s'\n", defaultModel);
parsedArgs.gltf_path = defaultModel;
}
);
args = parser.parseArgs(args);

if (args.gltf_path === null)
{
console.log("%s\n", parser.description);
console.info("IMPORTANT NOTICE: \n\
Add '-- --' to get your arguments through to the tool. \n\
Example: 'npm run start-offscreen -- -- --help'");
console.error("\nNo gltf_path was given, defaulting to '%s'\n", defaultModel);
args.gltf_path = defaultModel;
return parsedArgs;
}

global.sharedObject = { args: args };

function createWindow()
{
mainWindow = new BrowserWindow({
const mainWindow = new BrowserWindow({
width: 1920, height: 1080,
//show: false,
//frame: false,
show: false,
frame: false,
webPreferences: {
offscreen: true,
//transparent: true,
Expand Down Expand Up @@ -191,5 +208,3 @@ function createWindow()
});
});
}

app.on('ready', createWindow);
10 changes: 6 additions & 4 deletions headless.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@

var gltfPath = path.parse(args.gltf_path)

viewer.loadFromPath("/" + gltfPath.base, gltfPath.dir);

let yfov_rad = args.yfov * Math.PI / 180.0
viewer.setCamera(args.eye_position, args.target_position, args.up, args.projection, args.znear, args.zfar, yfov_rad, args.xmag, args.ymag);
viewer.loadFromPath("/" + gltfPath.base, gltfPath.dir).then(() =>
{
const yfov_rad = args.yfov * Math.PI / 180.0;
viewer.setCamera(args.eye_position, args.target_position, args.up, args.projection, args.znear, args.zfar, yfov_rad, args.xmag, args.ymag);
viewer.renderingParameters.cameraIndex = args.camera_index;
});
}

function rendererReady()
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class gltfRenderer
{
let currentCamera = undefined;

if(this.parameters.cameraIndex !== "default")
if(!this.parameters.userCameraActive())
{
currentCamera = gltf.cameras[this.parameters.cameraIndex].clone();
}
Expand Down
11 changes: 9 additions & 2 deletions src/rendering_parameters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ImageMimeType } from "./image";

const UserCameraIndex = "orbit camera";

class gltfRenderingParameters
{
constructor(
Expand All @@ -23,7 +25,12 @@ class gltfRenderingParameters
this.useShaderLoD = useShaderLoD;
this.debugOutput = debugOutput;
this.sceneIndex = 0;
this.cameraIndex = "default";
this.cameraIndex = UserCameraIndex;
}

userCameraActive()
{
return this.cameraIndex === UserCameraIndex;
}
}

Expand Down Expand Up @@ -59,4 +66,4 @@ const Environments =
"Dining room of the Ennis-Brown House": { folder: "ennis", mipLevel: 10, type: ImageMimeType.HDR }
};

export { gltfRenderingParameters, Environments, ToneMaps, DebugOutput };
export { UserCameraIndex, gltfRenderingParameters, Environments, ToneMaps, DebugOutput };
4 changes: 2 additions & 2 deletions src/user_interface.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Environments, ToneMaps, DebugOutput } from './rendering_parameters.js';
import { UserCameraIndex, Environments, ToneMaps, DebugOutput } from './rendering_parameters.js';

class gltfUserInterface
{
Expand Down Expand Up @@ -90,7 +90,7 @@ class gltfUserInterface

initializeCameraSelection(cameras)
{
const camerasWithUserCamera = [ "default" ].concat(cameras);
const camerasWithUserCamera = [ UserCameraIndex ].concat(cameras);
this.cameraSelection = this.gltfFolder.add(this.renderingParameters, "cameraIndex", camerasWithUserCamera).name("Camera Index");
}

Expand Down
Loading

0 comments on commit ce0abeb

Please sign in to comment.