Glass Effect On Text 3D

This component features a physically-based glass transmission material that creates realistic refraction, depth, environment reflections, and subtle chromatic aberration for an authentic glass effect, along with dual animation modes that allow either smooth automatic rotation or responsive cursor-driven interaction confined within the canvas for a more immersive and tactile 3D experience.

@react-three/drei@react-three/fibethree

Installation

Install the required dependencies:

npm install @react-three/drei @react-three/fibe three
Link to required files
Follow this link and download the required files. - https://github.com/railav61/data/tree/main/forkui-data/3d-glass-effect-data
components/ui/glass-effect-on-text-3d.tsx
"use client";
  import { Canvas, useFrame, useThree } from "@react-three/fiber";
  import {
    Environment,
    MeshTransmissionMaterial,
    Text,
    useGLTF,
  } from "@react-three/drei";
  import { Suspense, useRef } from "react";
  import * as THREE from "three";
  
  type CardProps = {
    centerRotationOnly?: boolean;
  };
  
  function TextEffect3d({ centerRotationOnly = true }: CardProps) {
    return (
      <Canvas className="bg-black">
        <Suspense fallback={null}>
          <directionalLight intensity={3} position={[0, 3, 2]} />
          <Environment preset="city" />
  
          <Model centerRotationOnly={centerRotationOnly} />
        </Suspense>
      </Canvas>
    );
  }
  
  export default TextEffect3d;
  
  function Model({ centerRotationOnly }: CardProps) {
    const group = useRef<THREE.Mesh>(null!);
    const { nodes } = useGLTF("/glass-effect-3d/torrus5.glb");
    const { viewport } = useThree();
  
    useFrame((state, delta) => {
      if (!centerRotationOnly) {
        group.current.rotation.y = state.pointer.x * 0.5;
        group.current.rotation.x = -state.pointer.y * 0.5;
  
        group.current.position.x = THREE.MathUtils.lerp(
          group.current.position.x,
          state.pointer.x * 20,
          0.1,
        );
  
        group.current.position.y = THREE.MathUtils.lerp(
          group.current.position.y,
          state.pointer.y * 20,
          0.1,
        );
      } else {
        group.current.rotation.x += delta * 0.5;
      }
    });
  
    return (
      <group scale={viewport.width / 100} >
        <Text
          font="/BebasNeue-Regular.ttf"
          position={[0, 0, -1]}
          fontSize={15}
          fontWeight={500}
          color="white"
          anchorX="center"
          anchorY="middle"
        >
          Hello World
        </Text>
  
        <mesh ref={group} geometry={(nodes.Torus002 as THREE.Mesh).geometry}>
          <MeshTransmissionMaterial
            thickness={1.3}
            roughness={0.05}
            transmission={1}
            ior={1.3}
            chromaticAberration={0.30}
            backside
          />
        </mesh>
      </group>
    );
  }
  

Usecase

* only center rotate

* With cursor follower