Comments (2)
NaN
suggests that values are being passed as zero somehow.
from react-three-fiber.
@CodyJasonBennett thank you for your response.
I did some digging into how fiber calculates the viewport
and discovered that, essentially, the returned width
is calculated like this:
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 getCurrentViewport
function, as can be seen in the source files. The code for the function is as follows:
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 size
object. The width
and height
properties from the size
object are both equal to 0
(as you mentioned). Since
// 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 useThree()
hook test I mentioned in the issue, we can see that the test for the size
object expects it to be populated with 0
values for height and width, as can be seen here:
expect(result.size).toEqual({ height: 0, width: 0, top: 0, left: 0, updateStyle: false })
As such, I wonder if viewport
having NaN
values is indeed intended behavior or a limitation of the ReactThreeTestRenderer, since the calculation of the properties of the viewport
directly depend upon the properties of the size
state, which in turn is officially EXPECTED to have zeros?
A potential quick fix I have in mind (based on the getCurrentViewport()
function logic) is to make the default camera of the ReactThreeTestRenderer orthographic. This will get rid of the NaN
values, however the width
and height
properties will then have 0
as their values, which does not seem useful for testing. Please let me know what you think. Thank you.
from react-three-fiber.
Related Issues (20)
- How to animate material during rendering
- Can't catch all useGLTF errors with React Error Boundaries HOT 7
- How to render text on top of material from list of materials? HOT 1
- Invalidate is not behaving as expected HOT 5
- Can we modify useLoader to accept an empty string or null? HOT 1
- XRFrame type resolving incorrectly HOT 2
- Error loading OBJ model in React Native HOT 2
- Help: TypeError in is.equ HOT 4
- Primitive with children mounting / unmounting + documentation HOT 4
- This sandbox doesn't work HOT 1
- Canvas eventHandler: onWebGLContextLost, onWebGLContextRestored HOT 2
- Cannot catch error HOT 1
- Is it possible to use react-three-fiber with React-Native CLI application? HOT 1
- help: using the onPointerDown event of a` <group/>` and the event.handlers of `useThree()` together HOT 1
- Types: Removed classes from three.js are still available at R3F HOT 3
- Rendering `<Context.Consumer.Provider>` is not supported HOT 1
- R3F & StereoCamera
- Breaking changes in React experimental branch HOT 1
- Cannot read properties of undefined (reading 'boundingSphere') HOT 1
- Forced landscape HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-three-fiber.