Utilizing ThreeJS: Implementing a Sprite in Conjunction with the OculusRiftEffect

I am currently working on a project for the OculusRift using the OculusRiftEffect found at https://github.com/mrdoob/three.js/blob/master/examples/js/effects/OculusRiftEffect.js and incorporating Sprites into the design. However, I am facing an issue where the sprites are not appearing in the correct position in each eye, resulting in a 'double vision' effect as shown in the screenshot. It seems that the house sprite is displayed differently in each eye, causing the problem. While experimenting with the code (you can check out the demo plunker here), it is evident that the positioning is more accurate towards the edges of the screen but deviates near the center. I believe this discrepancy is related to the shading/rendering in OculusRiftEffect, but my understanding is limited. Any guidance on how to resolve this issue would be highly appreciated. Thank you!

Sample code:

var _scene, _camera, _renderer, _effect, _sprite;

function init() {
  _scene = new THREE.Scene();
  _camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, .1, 100000);
  _camera.lookAt(new THREE.Vector3());
  _renderer = new THREE.WebGLRenderer({
        antialias: true,
      canvas: document.getElementById('legit')
    });

  _renderer.setSize(window.innerWidth, window.innerHeight);

  _effect = new THREE.OculusRiftEffect(_renderer, {
    worldScale: 1000
  });
  _effect.setSize(window.innerWidth, window.innerHeight);

  THREE.ImageUtils.crossOrigin = 'anonymous';

  _sprite = new THREE.Sprite(
    new THREE.SpriteMaterial({
        map: new THREE.Texture(document.getElementById('icon')),
        color: 0xff0000
    })
  );

  _sprite.scale.set(200, 200, 1);
  _sprite.position.set(500, 800, 1);
  _scene.add(_sprite);

  _scene.add(new THREE.Mesh(
    new THREE.SphereGeometry(3000, 64, 32),
    new THREE.MeshBasicMaterial({
      color: 0xffffff,
      wireframe: true,
      side: THREE.DoubleSide
    })
  ));

  animate();
}

function animate() {
  requestAnimationFrame(animate);
  render();
}

function render() {
    _renderer.render(_scene, _camera);
    _effect.render(_scene, _camera);
}

document.addEventListener('DOMContentLoaded', init);

Answer №1

While I may not have much experience with the Oculus plugin, it seems like there may be a misunderstanding in your grasp of how sprites function.

Think of a sprite as a visual tool - a graphical surface that can be rendered... It exists within a space that is flexible and can be interpreted in different ways.

Do you think your house perceives itself as part of a heads-up display rather than a small element within a larger system off in the distance?

An approach to achieving your desired outcome could involve duplicating an image equidistant from the central point of each eye, all within the confines of screen space. This method could produce the desired visual effect.

In terms of positioning, it's unclear if the alignment in your image is accurate, as there may be some distortion due to the exaggerated wide-angle effect.

Answer №2

After examining Three.js sprites closely, it appears that they are placed based on screen coordinates rather than coordinates within the scene. This means they do not take into account the per-eye projection matrix offset that affects the rest of the scene. As a result, I doubt they will work correctly when combined with the Oculus Rift distortion effect.

Answer №3

Both pailhead and Jherico have noted that achieving this with the Oculus plugin is not feasible. I came up with a workaround that I have found to be effective: simply use a PlaneGeometry and make it a child of the camera. Position it to constantly face the camera's position, acting like a sprite and rendering accurately for the OculusRiftEffect. Here's a basic example (assuming camera and scene are set up):

var sprite = new THREE.Mesh(
    new THREE.PlaneGeometry(100, 100),
    new THREE.MeshBasicMaterial({color: 0xffffff})
);
//assuming camera is at (0,0,0) we need to set an offset
sprite.position.set(20, 20, -100);
sprite.lookAt(camera.position);

camera.add(sprite);

//normally camera doesn't require being added
//but it is necessary if it has child meshes
scene.add(camera);

Since the plane is a child of the camera, once it's positioned with the right offset, it will follow the camera's movements. Keep in mind that it won't function exactly like a Sprite, as it can't be independently moved around the scene, but it's the closest solution I've come across.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having issues getting Nunjucks templates to render correctly in Express

There seems to be an issue with setting up the template hierarchy in Express or possibly an error in how Nunjucks is being utilized. The goal is to have a template that includes common parts and specific pages that extend this template. It appears that the ...

Encountering problems during the installation of Ionic - Error code 'EPROTO'

After attempting to install Ionic on my system, I encountered the following error message: npm ERR! code EPROTO npm ERR! errno EPROTO npm ERR! request to https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz failed, reason: write EPROTO 0:er ...

'view' is not recognized as a valid property of the 'div' element in Ionic3, so it cannot be bound

I am a beginner with Ionic and I'm trying to incorporate Angular Calendar into my Ionic3 application. However, I am encountering an error that says, "Can't bind to 'view' since it isn't a known property of 'div'. Here&ap ...

Error in React Native: Press function is not defined for TextInput

I am currently working on implementing an increment and decrement button feature in my application, which should then display the updated number in a text input field. However, I seem to be facing an issue where the text input is not showing the expected o ...

When using threejs, the color set for setClearColor is supposed to be white. However, when calling an external HTML file, it unexpectedly renders as

When I call an external HTML file in Three.js, the value for setClearColor is white but it renders as black. How can I solve this issue? Click here to view the image Here are the codes from the external file: <div id="3d-modal"></div> <sc ...

Detecting the failure of chrome.extension.sendRequest

Greetings Chrome Developers! I am wondering how one can determine when a chrome.extension.sendRequest call has not been successful. I attempted the following approach with no luck: chrome.extension.sendRequest({ /* message stuff here */ }, function(req){ ...

Utilize JavaScript declarations on dynamically added strings during Ajax loading

Is there a way to append HTML strings to the page while ensuring that JavaScript functions and definitions are applied correctly? I have encountered a confusing issue where I have multiple HTML elements that need to be interpreted by JavaScript. Take, for ...

Is there a way I can utilize a for-loop and if statement in JavaScript to present the information accurately within the table?

My current task involves fetching data via AJAX and then using a for-loop and if-statement to determine which goods belong in each shopping cart. Once identified, I need to display these goods in separate tables corresponding to each customer. Although the ...

multiple elements sharing identical CSS styling

For each component, I made separate CSS files and imported them accordingly. However, despite the individual styling efforts, all components seem to have the same appearance. I specifically worked on styling an image differently for two components, but w ...

Detecting the existence of a scrollbar within the document object model using JavaScript - a guide

Could someone provide a JavaScript syntax for detecting the presence of a scrollbar in the DOM by measuring the body height and window height? ...

When attempting to utilize yarn link, I receive an error message indicating that the other folder is not recognized as a module

After successfully running "yarn link," I encountered an issue when attempting to use it in a different project. The error message displayed was ../../.tsx is not a module. What could be causing this problem? const App: React.FC = () => { const [op ...

Retrieving an Object from an Object Group in javascript

I am encountering an issue while looping through something. https://i.sstatic.net/JkqcP.png However, I'm facing difficulties in obtaining the [["PromiseValue"]] object. If anyone can assist me with this, I would greatly appreciate it. Update : Th ...

Retrieving Data from all Rows in jQuery DataTables

Is there a way to copy all rows in jQuery DataTables into a JavaScript array by clicking the header checkbox? https://i.sstatic.net/57dTB.png I need to locate where jQuery DataTables store the HTML for the remaining page of rows so that I can manipulate ...

Switch up the text within the URL of the background image

Simply put, this is the structure I have: <div id="RelatedPosts1"> <div class="related-posts-thumb" style="background: url(http://xxxxxxx/s72-c/image-1.jpg)"></div> <div class="related-posts-thumb" style="background: url(http: ...

Top 5 Benefits of Utilizing Props over Directly Accessing Parent Data in Vue

When working with VueJS, I've noticed different approaches to accessing parent properties from a component. For example, let's say I need to utilize the parent property items in my component. Option One In this method, the component has a props ...

I require Ajax .on to trigger during the second .on event, rather than the first one

I'm facing a challenge with implementing chained selects that trigger my ajax call on change. The issue arises when the Chained Select populates the second select prematurely causing the ajax call to fire unexpectedly. Is it feasible to bypass the in ...

Vibrant progress bar design with CSS

Could someone assist me in coding a multicolor progress bar? I would like it to display red when the progress is less than 50, green when the progress is between 50 and 90, and blue when the progress is between 90 and 100. How can I achieve this? ...

When utilizing KineticJS on a canvas that has been rotated with css3, the functionality of the events appears to be malfunctioning

Currently, I'm working on a rotating pie-chart widget using Kineticjs. However, I have run into an issue where the events don't seem to function correctly when drawing on a rotated canvas element (with the parent node being rotated 60deg using CS ...

What impact does the spread operator have on the efficiency of code execution

Exploring two techniques for constructing an array of objects: Technique 1 (including all properties, even if duplicates exist): const employees = [ { company: 'ABC', country: 'IN', zip: 123, employeeId: 123, emp ...

Verify if a certain value exists in an array while using ng-if inside ng-repeat

Currently, I have a loop using ng-repeat that goes through a list of wines obtained from an API. Alongside this, there is an array variable containing all the IDs of wines that have been marked as favorites and retrieved from the database. My goal is to sh ...