After much trial and effort, I have created a version of one of my aircraft demos where you can bank along with the aircraft:
Sopwith Pup Over the Ocean
I had to replace the default SkyBox with a cube that I could rotate. Essentially, I stopped the aircraft from rotating and plugged the aircraft xyz rotation values into the cube.
Repositioning the sun was more complex. I ended up having to use the following equations to position it properly:
// Heading (Longitude)
var Yrad = ACHead * DegRad;
var X1 = LightX * Math.cos(-Yrad) - LightZ * Math.sin(-Yrad);
var Z1 = LightX * Math.sin(-Yrad) + LightZ * Math.cos(-Yrad);
// Pitch (Latitude)
var Xrad = Mod360(-ACPtch) * DegRad;
var Y1 = LightY * Math.cos(Xrad) - Z1 * Math.sin(Xrad);
var ZF = LightY * Math.sin(Xrad) + Z1 * Math.cos(Xrad);
// Bank
var Zrad = ACBank * DegRad;
var XF = X1 * Math.cos(Zrad) - Y1 * Math.sin(Zrad);
var YF = X1 * Math.sin(Zrad) + Y1 * Math.cos(Zrad);
// Set
light.position.set(XF,YF,ZF);
Unfortunately, this is a complication that will prevent the use of this method in an open world flight simulation since you would have to use the same equations to reposition every object, in addition to rotating the object. (The light did not have to be rotated.)
A few questions:
-
Can I replace the above equations with a single quaternion calculation? I know nothing about quaternions, but am willing to learn.
-
Is there a way to brighten the aircraft? I had to use a brightly-colored Skybox to increase visibility - which is why the poor pilot is in the middle of the ocean. Since this is a glb model, I included the following in my program:
renderer.gammaFactor = 2.2;
renderer.outputEncoding = THREE.sRGBEncoding;
EDIT: I brightened the aircraft by adding Ambient lighting with an intensity of 0.25.
With the default SkyBox, I could use reflection to brighten the aircraft. However, if my understanding is correct, implementing reflection with a custom Cube will require some special programming - a four camera setup?
- I assume that the default three.js camera calculates positions of all objects and rotates them. So what I am doing is probably a partial duplication of what the camera is doing. Is there any way to have the camera make these kinds of calculations? Or is that simply impractical?
ADDENDUM
Here is the FM2 which has more animated parts:
FM2 over Ocean
I forgot to add that this modification will allow me to create “in-cockpit” views of aircraft, such as the FM2.
You should also be able to create a simple “in cockpit” space combat simulation since there would not be a lot of external objects for which calculations are required. But I would need to figure out how to rotate the objects - which I haven’t done yet.
For reference, the webpage with links to all my demos is at:
Phil Crowther’s Aviation Webpage