<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kishan Suvagiya</title>
    <description>The latest articles on DEV Community by Kishan Suvagiya (@kishan_suvagiya_507c6f952).</description>
    <link>https://dev.to/kishan_suvagiya_507c6f952</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2818677%2Fd69a453b-693e-4d57-b5aa-d61036cd05c8.jpg</url>
      <title>DEV Community: Kishan Suvagiya</title>
      <link>https://dev.to/kishan_suvagiya_507c6f952</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kishan_suvagiya_507c6f952"/>
    <language>en</language>
    <item>
      <title>Discrepancy Between Original 3D Jeans Model and Output in React.js Using Three.js</title>
      <dc:creator>Kishan Suvagiya</dc:creator>
      <pubDate>Wed, 05 Feb 2025 09:49:29 +0000</pubDate>
      <link>https://dev.to/kishan_suvagiya_507c6f952/discrepancy-between-original-3d-jeans-model-and-output-in-reactjs-using-threejs-37bn</link>
      <guid>https://dev.to/kishan_suvagiya_507c6f952/discrepancy-between-original-3d-jeans-model-and-output-in-reactjs-using-threejs-37bn</guid>
      <description>&lt;p&gt;I am facing an issue in development. I am implementing a 3D model of jeans in React.js using the Three.js library. In fact, the model is showing in the output, but there is a difference between the original designer model and the output. I am attaching the complete code with a screenshot for reference.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqlqixs5rlph730yhvmnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqlqixs5rlph730yhvmnl.png" alt="Output from code" width="337" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F468m0czkp7fvlu6c80t9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F468m0czkp7fvlu6c80t9.png" alt="Original 3D model" width="330" height="580"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;const canvasRef = useRef();&lt;br&gt;
  const containerRef = useRef();&lt;br&gt;
  const sceneRef = useRef(null);&lt;br&gt;
  const rendererRef = useRef(null);&lt;br&gt;
  const cameraRef = useRef(null);&lt;br&gt;
  const controlsRef = useRef(null);&lt;br&gt;
  const modelRef = useRef(null);&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    if (!canvasRef.current || !containerRef.current) return;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Scene setup
const scene = new THREE.Scene();
sceneRef.current = scene;

// Get container dimensions instead of window
const container = containerRef.current;
const containerWidth = container.clientWidth;
const containerHeight = container.clientHeight || window.innerHeight * 0.7; // Set a default height

// Camera setup
const camera = new THREE.PerspectiveCamera(
  35,
  containerWidth / containerHeight,
  1,
  1000
);
camera.position.set(5, 5, 5);
cameraRef.current = camera;
scene.add(camera);

// Renderer setup
const renderer = new THREE.WebGLRenderer({
  canvas: canvasRef.current,
  antialias: true,
});
renderer.setSize(containerWidth, containerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(new THREE.Color("#ffffff"));
renderer.shadowMap.enabled = true; // Enable shadows
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
rendererRef.current = renderer;

// Controls setup
const controls = new OrbitControls(camera, canvasRef.current);
controls.enableDamping = true;
controls.enablePan = false;
controls.enableZoom = true;
controls.update();
controlsRef.current = controls;

// Lighting setup
const ambientLight = new THREE.AmbientLight(0xffffff, 1); // Increased intensity
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5); // Adjusted intensity
directionalLight.position.set(0, 10, 10);
scene.add(directionalLight);


// Add fill light from the front
const fillLight = new THREE.PointLight(0xffffff, 0.5);
fillLight.position.set(0, 3, 3);
scene.add(fillLight);

const backLight = new THREE.DirectionalLight(0xffffff, 0.4);
backLight.position.set(0, -2, -5);
scene.add(backLight);

// Model loading
const loader = new GLTFLoader();
const textureLoader = new THREE.TextureLoader();

loader.load(
  jeansglb,
  (gltf) =&amp;gt; {
    const model = gltf.scene;
    modelRef.current = model;
    model.scale.set(1, 1, 1);

    // Load and apply texture
    textureLoader.load(
      jeans,
      (texture) =&amp;gt; {
        texture.minFilter = THREE.LinearFilter;
        texture.magFilter = THREE.LinearFilter;
        texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
        texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
        texture.repeat.set(1, 1);

        model.traverse((child) =&amp;gt; {
          if (child.isMesh &amp;amp;&amp;amp; child.material instanceof THREE.MeshStandardMaterial) {
            child.material.map = texture;
            child.material.roughness = 0.85;
            child.material.metalness = 0.1;
            child.material.needsUpdate = true;
            child.material.side = THREE.DoubleSide;
          }
        });

        // Center the model
        const box = new THREE.Box3().setFromObject(model);
        const center = box.getCenter(new THREE.Vector3());
        model.position.sub(center);

        // Add model to scene
        scene.add(model);

        // Adjust camera to frame model
        const boundingBox = new THREE.Box3().setFromObject(model);
        const boundingBoxCenter = boundingBox.getCenter(new THREE.Vector3());
        const boundingBoxSize = boundingBox.getSize(new THREE.Vector3());
        const maxDimension = Math.max(
          boundingBoxSize.x,
          boundingBoxSize.y,
          boundingBoxSize.z
        );

        camera.position.copy(boundingBoxCenter);
        camera.position.z += maxDimension * 2;
        controls.target.copy(boundingBoxCenter);
        controls.update();
      },
      undefined,
      (error) =&amp;gt; console.error("Error loading texture:", error)
    );
  },
  (xhr) =&amp;gt; console.log((xhr.loaded / xhr.total) * 100 + "% loaded"),
  (error) =&amp;gt; console.error("Error loading 3D model:", error)
);

// Window resize handler
const handleResize = () =&amp;gt; {
  const newWidth = container.clientWidth;
  const newHeight = container.clientHeight || window.innerHeight * 0.7;

  if (cameraRef.current &amp;amp;&amp;amp; rendererRef.current) {
    cameraRef.current.aspect = newWidth / newHeight;
    cameraRef.current.updateProjectionMatrix();
    rendererRef.current.setSize(newWidth, newHeight);
  }
};

window.addEventListener("resize", handleResize);

// Animation loop
let animationFrameId;
const animate = () =&amp;gt; {
  animationFrameId = requestAnimationFrame(animate);

  if (controlsRef.current) {
    controlsRef.current.update();
  }

  if (rendererRef.current &amp;amp;&amp;amp; sceneRef.current &amp;amp;&amp;amp; cameraRef.current) {
    rendererRef.current.render(sceneRef.current, cameraRef.current);
  }
};
animate();

// Cleanup
return () =&amp;gt; {
  window.removeEventListener("resize", handleResize);
  if (animationFrameId) {
    cancelAnimationFrame(animationFrameId);
  }
  if (rendererRef.current) {
    rendererRef.current.dispose();
  }
  if (modelRef.current) {
    modelRef.current.traverse((child) =&amp;gt; {
      if (child.isMesh) {
        child.geometry.dispose();
        if (child.material.map) child.material.map.dispose();
        child.material.dispose();
      }
    });
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}, []);&lt;/p&gt;

&lt;p&gt;I used the Three.js library to implement a 3D model of jeans in my React.js project. The model successfully renders in the output, but there are noticeable differences between the original designer model and the rendered output.&lt;/p&gt;

&lt;p&gt;I have tried the following:&lt;/p&gt;

&lt;p&gt;Adjusting the camera settings (e.g., position, field of view).&lt;br&gt;
Modifying the light settings (e.g., intensity, type, and position of lights).&lt;br&gt;
Ensuring the model file is correctly exported and imported (e.g., verifying .glb or .obj formats and textures).&lt;br&gt;
Checking for material or texture mismatches in the Three.js code.&lt;br&gt;
Despite these efforts, the rendered model does not match the original. I was expecting the rendered 3D model to look identical to the designer's version in terms of proportions, materials, and textures.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
