admin管理员组文章数量:1404622
I'm trying to embed a Sketchfab 3D model in my React JSX hero section, but every time I do, the model appears extremely small—like a single pixel. However, the model remains tiny. How can I properly size and scale the 3D model inside my hero section so it fits well and is fully visible? im trying to use that exact model directed by the link. Other models appear fine but not this.
Any help is appreciated!
import React, { Suspense, useRef, useEffect, useState } from "react";
import { Canvas, useFrame, useLoader, useThree } from "@react-three/fiber";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { OrbitControls, Environment, Html, useGLTF } from "@react-three/drei";
import { Box3, Vector3 } from "three";
import * as THREE from 'three';
// Model component that loads and animates the 3D model
function Model() {
const gltf = useLoader(GLTFLoader, "/traveler.glb");
const modelRef = useRef();
const mixer = useRef();
const { camera } = useThree();
useEffect(() => {
if (!gltf || !modelRef.current) return;
// Setup animation mixer
if (gltf.animations.length) {
mixer.current = new THREE.AnimationMixer(gltf.scene);
gltf.animations.forEach((clip) => {
mixer.current.clipAction(clip).play();
});
}
// Auto-scale and center model
const box = new THREE.Box3().setFromObject(modelRef.current);
const center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
const fov = camera.fov * (Math.PI / 180);
const distance = Math.abs(maxDim / Math.sin(fov / 2));
camera.position.set(center.x, center.y, distance * 1.5);
camera.lookAt(center);
const scaleFactor = 2 / maxDim;
modelRef.current.scale.setScalar(scaleFactor);
modelRef.current.position.set(-center.x, -center.y, -center.z);
}, [gltf]);
useFrame((_, delta) => {
if (mixer.current) mixer.current.update(delta);
});
return <primitive ref={modelRef} object={gltf.scene} />;
}
// Loader component for displaying during model loading
function Loader() {
return (
<Html center>
<div style={{ color: "white", display: "flex", flexDirection: "column", alignItems: "center" }}>
<div className="loader"></div>
<p>Loading 3D model...</p>
</div>
</Html>
);
}
// Main component
const HeroModel3D = () => {
return (
<div className="hero-model-3d">
<Canvas
camera={{ position: [0, 0, 10], fov: 50 }}
style={{ background: "transparent" }}
>
<ambientLight intensity={0.8} />
<spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} intensity={1} castShadow />
<Suspense fallback={<Loader />}>
<Model />
<Environment preset="city" />
<OrbitControls
enableZoom={true}
enablePan={true}
rotateSpeed={0.5}
autoRotate={false}
/>
</Suspense>
</Canvas>
</div>
);
};
export default HeroModel3D;
This is my inspo
I'm trying to embed a Sketchfab 3D model in my React JSX hero section, but every time I do, the model appears extremely small—like a single pixel. However, the model remains tiny. How can I properly size and scale the 3D model inside my hero section so it fits well and is fully visible? https://sketchfab/3d-models/traveler-f86f5f2a779d496c95c9aca8ba13246f#comments im trying to use that exact model directed by the link. Other models appear fine but not this.
Any help is appreciated!
import React, { Suspense, useRef, useEffect, useState } from "react";
import { Canvas, useFrame, useLoader, useThree } from "@react-three/fiber";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { OrbitControls, Environment, Html, useGLTF } from "@react-three/drei";
import { Box3, Vector3 } from "three";
import * as THREE from 'three';
// Model component that loads and animates the 3D model
function Model() {
const gltf = useLoader(GLTFLoader, "/traveler.glb");
const modelRef = useRef();
const mixer = useRef();
const { camera } = useThree();
useEffect(() => {
if (!gltf || !modelRef.current) return;
// Setup animation mixer
if (gltf.animations.length) {
mixer.current = new THREE.AnimationMixer(gltf.scene);
gltf.animations.forEach((clip) => {
mixer.current.clipAction(clip).play();
});
}
// Auto-scale and center model
const box = new THREE.Box3().setFromObject(modelRef.current);
const center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
const fov = camera.fov * (Math.PI / 180);
const distance = Math.abs(maxDim / Math.sin(fov / 2));
camera.position.set(center.x, center.y, distance * 1.5);
camera.lookAt(center);
const scaleFactor = 2 / maxDim;
modelRef.current.scale.setScalar(scaleFactor);
modelRef.current.position.set(-center.x, -center.y, -center.z);
}, [gltf]);
useFrame((_, delta) => {
if (mixer.current) mixer.current.update(delta);
});
return <primitive ref={modelRef} object={gltf.scene} />;
}
// Loader component for displaying during model loading
function Loader() {
return (
<Html center>
<div style={{ color: "white", display: "flex", flexDirection: "column", alignItems: "center" }}>
<div className="loader"></div>
<p>Loading 3D model...</p>
</div>
</Html>
);
}
// Main component
const HeroModel3D = () => {
return (
<div className="hero-model-3d">
<Canvas
camera={{ position: [0, 0, 10], fov: 50 }}
style={{ background: "transparent" }}
>
<ambientLight intensity={0.8} />
<spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} intensity={1} castShadow />
<Suspense fallback={<Loader />}>
<Model />
<Environment preset="city" />
<OrbitControls
enableZoom={true}
enablePan={true}
rotateSpeed={0.5}
autoRotate={false}
/>
</Suspense>
</Canvas>
</div>
);
};
export default HeroModel3D;
This is my inspo
Share Improve this question asked Mar 10 at 17:42 Sithil SandinuSithil Sandinu 214 bronze badges 1- This question is similar to: 3D Model Appears Too Small in React Three Fiber (GLTFLoader). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Łukasz Daniel Mastalerz Commented Mar 10 at 18:00
1 Answer
Reset to default 0Model element "metarig_45" was exported to GLB with scale and moved position. You can download blender file from sketch fab and try export it again. Or scale object in code.
const metarig_45 = model.getObjectByName("metarig_45");
metarig_45.scale.set(1, 1, 1);
metarig_45.position.set(0, 0, 0);
版权声明:本文标题:javascript - How to Embed a Sketchfab 3D Model in a React JSX Hero Section with Proper Sizing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744831405a2627372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论