-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RTTR: NaN values in state.viewport #3133
Comments
|
@CodyJasonBennett thank you for your response. I did some digging into how fiber calculates the const w = h * (width / height)
return { width: w, height: h, top, left, factor: width / w, distance, aspect } This is coming from the definition of the function getCurrentViewport(
camera: Camera = get().camera,
target: THREE.Vector3 | Parameters<THREE.Vector3['set']> = defaultTarget,
size: Size = get().size
): Omit<Viewport, 'dpr' | 'initialDpr'> {
const { width, height, top, left } = size
const aspect = width / height
if (target instanceof THREE.Vector3) tempTarget.copy(target)
else tempTarget.set(...target)
const distance = camera.getWorldPosition(position).distanceTo(tempTarget)
if (isOrthographicCamera(camera)) {
return {
width: width / camera.zoom,
height: height / camera.zoom,
top,
left,
factor: 1,
distance,
aspect,
}
} else {
const fov = (camera.fov * Math.PI) / 180 // convert vertical fov to radians
const h = 2 * Math.tan(fov / 2) * distance // visible height
const w = h * (width / height)
return {
width: w,
height: h,
top,
left,
factor: width / w,
distance,
aspect,
}
}
} The width calculation is dependent on values being attained from the state's // height is equal to 0
const w = h * (width / height) This results in a division by 0, leading to the width being given a NaN value. Now, if we take a closer look at the official expect(result.size).toEqual({ height: 0, width: 0, top: 0, left: 0, updateStyle: false }) As such, I wonder if A potential quick fix I have in mind (based on the |
This commit resolves the issue where width and height properties were not properly structured as part of a size object in the configure function, causing errors in viewport calculation. Now, the function correctly creates and includes a size object with width, height, top, and left properties, ensuring accurate viewport handling as well as camera handling. Closes pmndrs#3133
Updated the test for the useThree hook to accurately check the size property. The test now uses specific dimensions (width: 1280, height: 800) to create the component, ensuring that the size object is correctly tested against expected values. This change improves the test's effectiveness in catching potential size-related issues in the configure function. Related to pmndrs#3133
Hi, I am writing unit tests for an R3F application by using the ReactThreeTestRenderer and I am running into a strange issue where the
viewport
object (and other state, such as camera, obtained by calling theuseThree()
) is randomly populated withNaN
values, such as thewidth
property. Because of this, all the R3F code that is related to theviewport
in any way does not perform as expected and cannot be tested correctly.I have run the test on different machines to ensure this behaviour is not related to my specific setup. I have also created a minimal reproduction of the issue in this devbox.
The code for the test is as follows:
the returned viewport object is:
I have also combed the source files for the test-renderer: while there is indeed a test for the
useThree()
hook, theviewport
and other states likecamera
are not tested. This leads me to believe that perhaps this specific value ofviewport
is intentional or an inherent limitation of the ReactThreeTestRenderer. However, in this case, why do some properties in theviewport
, such as theheight
property, have completely valid values while others haveNaN
values?Note: the issue remains even if:
width
andheight
values are explicitly passed in theReactThreeTestRenderer.create
function like so:getCurrentViewport
function is used:camera
. It too hasNaN
values in theprojectionMatrix
andaspect
and more.Package Versions:
react
: 18.2.0react-dom
: 18.2.0jest
: 29.7.0three
: 0.160.0@react-three/fiber
: 8.15.12@react-three/test-renderer
: 8.2.0@CodyJasonBennett your input would be highly valuable. Thank you
The text was updated successfully, but these errors were encountered: