Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose environment map in the UI #671

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions brayns/engine/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,34 +405,33 @@ void Scene::setMaterialsColorMap(MaterialsColorMap colorMap)

bool Scene::setEnvironmentMap(const std::string& envMap)
{
bool success = true;
if (envMap.empty())
{
_backgroundMaterial->removeTexture(TT_DIFFUSE);
_updateValue(_hasEnvironmentMap, false);
}
else
{
try
{
_backgroundMaterial->setTexture(envMap, TT_DIFFUSE);
_updateValue(_hasEnvironmentMap, true);
}
catch (const std::runtime_error& e)
{
BRAYNS_ERROR << "Cannot load environment map: " << e.what()
BRAYNS_DEBUG << "Cannot load environment map: " << e.what()
<< std::endl;
return false;
_backgroundMaterial->removeTexture(TT_DIFFUSE);
success = false;
}
}

_updateValue(_environmentMap, success ? envMap : "");
if (_backgroundMaterial->isModified())
markModified();
return true;
return success;
}

bool Scene::hasEnvironmentMap() const
{
return _hasEnvironmentMap;
return !_environmentMap.empty();
}

void Scene::_computeBounds()
Expand Down
7 changes: 6 additions & 1 deletion brayns/engine/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class Scene : public BaseObject
*/
bool setEnvironmentMap(const std::string& envMap);

/**
* @return the current set environment map texture file, or empty if no
* environment is set
*/
const std::string& getEnvironmentMap() const { return _environmentMap; }
/** @return true if an environment map is currently set in the scene. */
bool hasEnvironmentMap() const;

Expand Down Expand Up @@ -224,7 +229,7 @@ class Scene : public BaseObject

ParametersManager& _parametersManager;
MaterialPtr _backgroundMaterial;
bool _hasEnvironmentMap{false};
std::string _environmentMap;

// Model
size_t _modelID{0};
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ services:
build: .
restart: always
ports:
- "8200:8200"
- "8200:8200"
2 changes: 1 addition & 1 deletion js/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
}
83 changes: 74 additions & 9 deletions js/apps/viewer/src/app/renderer-settings/renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import React, {ChangeEvent, PureComponent} from 'react';

import {
EnvironmentMap,
GET_ENVIRONMENT_MAP,
GET_RENDERER,
GET_RENDERER_PARAMS,
Renderer as RendererType,
RENDERER_PARAMS,
RendererParams,
SCHEMA,
SET_ENVIRONMENT_MAP,
SET_RENDERER,
SET_RENDERER_PARAMS
} from 'brayns';
import {JSONSchema7} from 'json-schema';
import {Subscription} from 'rxjs';
import {mergeMap} from 'rxjs/operators';

import IconButton from '@material-ui/core/IconButton';
import InputAdornment from '@material-ui/core/InputAdornment';
import TextField from '@material-ui/core/TextField';
import CloseIcon from '@material-ui/icons/Close';

import brayns, {onReady} from '../../common/client';
import {
ColorField,
Expand All @@ -26,7 +34,6 @@ import {dispatchNotification} from '../../common/events';

import {findSchemaForType} from './utils';


const MAX_ACC_FRAMES = 1000;


Expand All @@ -36,7 +43,9 @@ export default class Renderer extends PureComponent<Props, State> {
backgroundColor: [0, 0, 0],
maxAccumFrames: 0,
samplesPerPixel: 1,
headLight: false
headLight: false,
environmentMap: '',
isEnvMapValid: true
};

private subs: Subscription[] = [];
Expand Down Expand Up @@ -89,6 +98,14 @@ export default class Renderer extends PureComponent<Props, State> {
}, false);
}

updateEnvironmentMap = (evt: ChangeEvent<HTMLInputElement>) => {
this.setEnvironmentMap(evt.target.value);
}

clearEnvMap = () => {
this.setEnvironmentMap('');
}

updateRenderer = async (props: RendererParams, updateState: boolean = true) => {
try {
await brayns.request(SET_RENDERER, props);
Expand Down Expand Up @@ -133,12 +150,15 @@ export default class Renderer extends PureComponent<Props, State> {
currentRendererParams: params
});
}),
brayns.observe(SET_ENVIRONMENT_MAP)
.subscribe(({filename: environmentMap}) => this.setState({environmentMap})),
onReady().pipe(mergeMap(() => getCurrentState()))
.subscribe(([renderer, schema, params]) => {
.subscribe(([renderer, schema, {filename: environmentMap}, params]) => {
this.setState({
...getRendererState(renderer, schema),
paramsSchema: schema,
currentRendererParams: params
currentRendererParams: params,
environmentMap
});
})
]);
Expand All @@ -161,7 +181,9 @@ export default class Renderer extends PureComponent<Props, State> {
samplesPerPixel,
maxAccumFrames,
headLight,
types
types,
environmentMap,
isEnvMapValid
} = this.state;

const fields = currentRendererSchema ? (
Expand All @@ -173,6 +195,22 @@ export default class Renderer extends PureComponent<Props, State> {
/>
) : null;

const hasEnvMap = environmentMap && environmentMap.length;
const envMapInputProps = {};
if (hasEnvMap) {
Object.assign(envMapInputProps, {
endAdornment: (
<InputAdornment position="end">
<IconButton onClick={this.clearEnvMap} disabled={disabled}>
<CloseIcon fontSize="small" />
</IconButton>
</InputAdornment>
)
});
}

const disableBackgroundColor = disabled || (hasEnvMap && isEnvMapValid);

return (
<div>
<SelectField
Expand Down Expand Up @@ -215,8 +253,21 @@ export default class Renderer extends PureComponent<Props, State> {
value={backgroundColor}
onChange={this.updateBackgroundColor}
margin="normal"
disabled={!!disableBackgroundColor}
fullWidth
/>

<TextField
id="environmentmap"
label="Environment map"
value={environmentMap}
onChange={this.updateEnvironmentMap}
margin="normal"
disabled={disabled}
fullWidth
InputProps={envMapInputProps}
error={!disabled && !isEnvMapValid}
helperText={disabled || isEnvMapValid ? '' : 'Invalid path'}
/>

<SwitchField
Expand All @@ -232,13 +283,24 @@ export default class Renderer extends PureComponent<Props, State> {
</div>
);
}
}

private setEnvironmentMap = (filename: string) => {
this.setState({environmentMap: filename}, async () => {
try {
const isEnvMapValid = await brayns.request(SET_ENVIRONMENT_MAP, {filename});
this.setState({isEnvMapValid});
} catch (err) {
dispatchNotification(err);
}
});
}
}

function getCurrentState(): Promise<CurrentState> {
return Promise.all([
brayns.request(GET_RENDERER),
brayns.request(SCHEMA, {endpoint: RENDERER_PARAMS}),
brayns.request(GET_ENVIRONMENT_MAP),
getCurrentRendererParams()
]) as any;
}
Expand Down Expand Up @@ -313,13 +375,16 @@ interface State extends Partial<RendererState> {
paramsSchema?: JSONSchema7;
currentRendererSchema?: JSONSchema7;
currentRendererParams?: any;
environmentMap?: string;
isEnvMapValid?: boolean;
}

interface CurrentState extends Array<JSONSchema7 | RendererType | object> {
interface CurrentState extends Array<JSONSchema7 | RendererType | object | EnvironmentMap> {
0: RendererType;
1: JSONSchema7;
2: object;
length: 3;
2: EnvironmentMap;
3: object;
length: 4;
}

interface WithRenderer extends Array<RendererType | object> {
Expand Down
13 changes: 11 additions & 2 deletions js/packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
GET_CAMERA_PARMS_TYPE,
GET_CAMERA_TYPE,
GET_CLIP_PLANES_TYPE,
GET_ENVIRONMENT_MAP_TYPE,
GET_LOADERS_TYPE,
GET_MODEL_PROPERTIES_TYPE,
GET_MODEL_TRANSFER_FUNCTION_TYPE,
Expand All @@ -55,6 +56,7 @@ import {
SET_APP_PARAMS_TYPE,
SET_CAMERA_PARMS_TYPE,
SET_CAMERA_TYPE,
SET_ENVIRONMENT_MAP_TYPE,
SET_MODEL_PROPERTIES_TYPE,
SET_MODEL_TRANSFER_FUNCTION_TYPE,
SET_RENDERER_PARMS_TYPE,
Expand All @@ -76,6 +78,7 @@ import {
Camera,
CameraParams,
ClipPlane,
EnvironmentMap,
GetModelPropsParams,
GetModelPropsSchemaParams,
InspectCoords,
Expand Down Expand Up @@ -226,6 +229,8 @@ export class Client {
request(method: REMOVE_CLIP_PLANES_TYPE, params: number[]): RequestTask<number[], boolean>;
request(method: SNAPSHOT_TYPE, params: SnapshotParams): RequestTask<SnapshotParams, Snapshot>;
request(method: SCHEMA_TYPE, params: SchemaParams): RequestTask<SchemaParams, JSONSchema7>;
request(method: SET_ENVIRONMENT_MAP_TYPE, params: EnvironmentMap): RequestTask<EnvironmentMap, boolean>;
request(method: GET_ENVIRONMENT_MAP_TYPE): RequestTask<undefined, EnvironmentMap>;
request<R, P>(method: RequestType | string, params?: P): RequestTask<P, R> {
return this.rockets!.request<P, R>(method, params);
}
Expand All @@ -246,6 +251,7 @@ export class Client {
observe(method: REMOVE_CLIP_PLANES_TYPE): Observable<number[]>;
observe(method: SET_STATISTICS_TYPE): Observable<Statistics>;
observe(method: IMAGE_JPEG_TYPE): Observable<Blob>;
observe(method: SET_ENVIRONMENT_MAP_TYPE): Observable<EnvironmentMap>;
observe<R>(method: ObservableType | IMAGE_JPEG_TYPE | string): Observable<R | Blob> {
if (method === IMAGE_JPEG) {
return this.binary.asObservable();
Expand Down Expand Up @@ -406,7 +412,9 @@ export type RequestType = GET_ANIMATION_PARAMS_TYPE
| GET_CLIP_PLANES_TYPE
| REMOVE_CLIP_PLANES_TYPE
| SNAPSHOT_TYPE
| SCHEMA_TYPE;
| SCHEMA_TYPE
| SET_ENVIRONMENT_MAP_TYPE
| GET_ENVIRONMENT_MAP_TYPE;

export type NotificationType = SET_ANIMATION_PARAMS_TYPE
| SET_APP_PARAMS_TYPE
Expand All @@ -430,7 +438,8 @@ export type ObservableType = SET_ANIMATION_PARAMS_TYPE
| SET_SCENE_TYPE
| SET_MODEL_PROPERTIES_TYPE
| SET_MODEL_TRANSFER_FUNCTION_TYPE
| SET_STATISTICS_TYPE;
| SET_STATISTICS_TYPE
| SET_ENVIRONMENT_MAP_TYPE;

export type SchemaType = RequestType
| NotificationType
Expand Down
5 changes: 5 additions & 0 deletions js/packages/sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export type SNAPSHOT_TYPE = typeof SNAPSHOT;
export const SCHEMA = 'schema';
export type SCHEMA_TYPE = typeof SCHEMA;

// Environment map
export const SET_ENVIRONMENT_MAP = 'set-environment-map';
export type SET_ENVIRONMENT_MAP_TYPE = typeof SET_ENVIRONMENT_MAP;
export const GET_ENVIRONMENT_MAP = 'get-environment-map';
export type GET_ENVIRONMENT_MAP_TYPE = typeof GET_ENVIRONMENT_MAP;

/**
* RPC Notifications from client -> server
Expand Down
Loading