-
Notifications
You must be signed in to change notification settings - Fork 426
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
Partially refactor JS utilities to support future webapps in separate folder structure #1364
Merged
ldcWV
merged 6 commits into
facebookresearch:master
from
ldcWV:ldchen/js_utilities_folder
Jul 12, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fecb640
Changes to the JS utilities
76e13d7
Merge branch 'master' of https://github.com/facebookresearch/habitat-…
9c52788
Reorganize utils files
1a4f7f3
Fix merge issues
54bb890
Make getEyeSensorSpecs() callable with resolution width and height pa…
12c1442
Move VR specific boilerplate code from vr_demo.js to vr_utils.js
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// Copyright (c) Facebook, Inc. and its affiliates. | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
/* global Module */ | ||
|
||
export const VIEW_SENSORS = ["left_eye", "right_eye"]; | ||
const pointToArray = p => [p.x, p.y, p.z, p.w]; | ||
|
||
export function getEyeSensorSpecs(resolutionWidth, resolutionHeight) { | ||
const specs = new Module.VectorSensorSpec(); | ||
{ | ||
const spec = new Module.CameraSensorSpec(); | ||
spec.uuid = "left_eye"; | ||
spec.sensorType = Module.SensorType.COLOR; | ||
spec.sensorSubType = Module.SensorSubType.PINHOLE; | ||
spec.resolution = [resolutionWidth, resolutionHeight]; | ||
specs.push_back(spec); | ||
} | ||
{ | ||
const spec = new Module.CameraSensorSpec(); | ||
spec.uuid = "right_eye"; | ||
spec.sensorType = Module.SensorType.COLOR; | ||
spec.sensorSubType = Module.SensorSubType.PINHOLE; | ||
spec.resolution = [resolutionWidth, resolutionHeight]; | ||
specs.push_back(spec); | ||
} | ||
return specs; | ||
} | ||
|
||
// Given the WebXR viewer pose, update the positions/orientations of the view | ||
// sensors | ||
export function updateHeadPose(pose, agent) { | ||
const FWD = Module.Vector3.zAxis(1); | ||
const FWD_ANGLE = Math.atan2(FWD.z(), FWD.x()); | ||
const DOWN = Module.Vector3.yAxis(-1); | ||
const viewYawOffset = 0; | ||
|
||
const headRotation = Module.toQuaternion( | ||
pointToArray(pose.transform.orientation) | ||
); | ||
const pointingVec = headRotation.transformVector(FWD); | ||
const pointingAngle = | ||
Math.atan2(pointingVec.z(), pointingVec.x()) - FWD_ANGLE; | ||
|
||
const agentQuat = Module.Quaternion.rotation( | ||
new Module.Rad(pointingAngle + viewYawOffset), | ||
DOWN | ||
); | ||
const inverseAgentRot = Module.Quaternion.rotation( | ||
new Module.Rad(-pointingAngle), | ||
DOWN | ||
); | ||
|
||
let state = new Module.AgentState(); | ||
agent.getState(state); | ||
state.rotation = Module.toVec4f(agentQuat); | ||
agent.setState(state, false); | ||
|
||
for (var iView = 0; iView < pose.views.length; ++iView) { | ||
const view = pose.views[iView]; | ||
|
||
const sensor = agent.getSubtreeSensors().get(VIEW_SENSORS[iView]); | ||
|
||
const pos = pointToArray(view.transform.position).slice(0, -1); // don't need w for position | ||
sensor.setLocalTransform( | ||
Module.toVec3f( | ||
inverseAgentRot.transformVector(new Module.Vector3(...pos)) | ||
), | ||
Module.toVec4f( | ||
Module.Quaternion.mul( | ||
inverseAgentRot, | ||
Module.toQuaternion(pointToArray(view.transform.orientation)) | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
// GL stuff | ||
let inds = null; | ||
let tex = null; | ||
|
||
// Pass in an html element with a webgl context | ||
export function initGL(gl) { | ||
if (gl === null) { | ||
return null; | ||
} | ||
const vertSrc = | ||
"attribute vec3 p;attribute vec2 u;varying highp vec2 v;void main(){gl_Position=vec4(p,1);v=u;}"; | ||
const fragSrc = | ||
"uniform sampler2D t;varying highp vec2 v;void main(){gl_FragColor=texture2D(t,v);}"; | ||
|
||
// Compiles a GLSL shader. Returns null on failure | ||
function compileShader(gl, src, type) { | ||
const shader = gl.createShader(type); | ||
gl.shaderSource(shader, src); | ||
gl.compileShader(shader); | ||
|
||
if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | ||
return shader; | ||
} else { | ||
console.error("GLSL shader error: " + gl.getShaderInfoLog(shader)); | ||
return null; | ||
} | ||
} | ||
const vert = compileShader(gl, vertSrc, gl.VERTEX_SHADER); | ||
const frag = compileShader(gl, fragSrc, gl.FRAGMENT_SHADER); | ||
if (vert === null || frag === null) { | ||
console.log("Failed to compile shaders"); | ||
return null; | ||
} | ||
|
||
const program = gl.createProgram(); | ||
|
||
gl.attachShader(program, vert); | ||
gl.attachShader(program, frag); | ||
|
||
gl.linkProgram(program); | ||
|
||
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { | ||
console.error("GLSL program error:" + gl.getProgramInfoLog(program)); | ||
return null; | ||
} | ||
|
||
gl.useProgram(program); | ||
|
||
// Vertices | ||
const verts = [ | ||
-1.0, | ||
1.0, | ||
0.0, | ||
-1.0, | ||
-1.0, | ||
0.0, | ||
1.0, | ||
-1.0, | ||
0.0, | ||
1.0, | ||
1.0, | ||
0.0 | ||
]; | ||
const vertBuf = gl.createBuffer(); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); | ||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW); | ||
// Position attribute | ||
const posAttrib = gl.getAttribLocation(program, "p"); | ||
gl.vertexAttribPointer(posAttrib, 3, gl.FLOAT, false, 0, 0); | ||
gl.enableVertexAttribArray(posAttrib); | ||
|
||
// UVs | ||
const UVs = [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0]; | ||
const uvBuf = gl.createBuffer(); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuf); | ||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(UVs), gl.STATIC_DRAW); | ||
// UV attribute | ||
const uvAttrib = gl.getAttribLocation(program, "u"); | ||
gl.vertexAttribPointer(uvAttrib, 2, gl.FLOAT, false, 0, 0); | ||
gl.enableVertexAttribArray(uvAttrib); | ||
|
||
// Indices | ||
inds = [3, 2, 1, 3, 1, 0]; | ||
const indBuf = gl.createBuffer(); | ||
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); | ||
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(inds), gl.STATIC_DRAW); | ||
|
||
// Texture uniform | ||
const texUni = gl.getUniformLocation(program, "t"); | ||
gl.uniform1i(texUni, 0); | ||
} | ||
|
||
export function drawTextureData(gl, texRes, texData) { | ||
if (tex === null) { | ||
tex = gl.createTexture(); | ||
gl.bindTexture(gl.TEXTURE_2D, tex); | ||
|
||
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.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); | ||
} | ||
|
||
gl.bindTexture(gl.TEXTURE_2D, tex); | ||
gl.texImage2D( | ||
gl.TEXTURE_2D, | ||
0, // level | ||
gl.RGBA, // internal format | ||
texRes[1], // width | ||
texRes[0], // height | ||
0, // border | ||
gl.RGBA, // format | ||
gl.UNSIGNED_BYTE, // type | ||
new Uint8Array(texData) // data | ||
); | ||
|
||
gl.activeTexture(gl.TEXTURE0); | ||
|
||
gl.drawElements(gl.TRIANGLES, inds.length, gl.UNSIGNED_SHORT, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any code redundancy between this file and the existing vr_demo.js?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, see here: #1364 (comment)
The existing vr_demo.js will be deleted in #1366, so this redundancy will be resolved as soon as that PR gets merged.