# TransformControls and makeDefault

Combining TransformControls, OrbitControls and Valtio.

Demo: https://pmndrs.github.io/examples/examples/transformcontrols-and-makedefault
Source: https://github.com/pmndrs/examples/tree/main/examples/transformcontrols-and-makedefault
Scaffold: npx degit pmndrs/examples/examples/transformcontrols-and-makedefault
Published: 2021-10-08
Authors: Paul Henschel
Tags: controls, state
Ported from: https://codesandbox.io/s/btsbj
Dependencies: @react-three/drei@10.7.9, @react-three/fiber@9.8.2, react@19.2.9, react-dom@19.2.9, three@0.186.1, valtio@^1.13.2
Binary files, in the repository but not below: src/compressed.glb

## src/App.tsx

```tsx
import { Suspense, useState } from "react";
import * as THREE from "three";
import { Canvas, useThree, type ThreeElements } from "@react-three/fiber";
import {
  OrbitControls,
  TransformControls,
  ContactShadows,
  useGLTF,
  useCursor,
} from "@react-three/drei";
import { proxy, useSnapshot } from "valtio";
import { type GLTF } from "three-stdlib";

import model from "./compressed.glb?url";

// Reactive state model, using Valtio ...
const modes = ["translate", "rotate", "scale"] as const;
const state = proxy<{ current: string | null; mode: number }>({
  current: null,
  mode: 0,
});

type GLTFResult = GLTF & {
  nodes: {
    Curly: THREE.Mesh;
    DNA: THREE.Mesh;
    Headphones: THREE.Mesh;
    Notebook: THREE.Mesh;
    Rocket003: THREE.Mesh;
    Roundcube001: THREE.Mesh;
    Table: THREE.Mesh;
    VR_Headset: THREE.Mesh;
    Zeppelin: THREE.Mesh;
  };
};

function Model({
  name,
  ...props
}: { name: keyof GLTFResult["nodes"] } & ThreeElements["mesh"]) {
  // Ties this component to the state model
  const snap = useSnapshot(state);
  // Fetching the GLTF, nodes is a collection of all the meshes
  // It's cached/memoized, it only gets loaded and parsed once
  const { nodes } = useGLTF(model) as unknown as GLTFResult;
  // Feed hover state into useCursor, which sets document.body.style.cursor to pointer|auto
  const [hovered, setHovered] = useState(false);
  useCursor(hovered);
  return (
    <mesh
      // Click sets the mesh as the new target
      onClick={(e) => (e.stopPropagation(), (state.current = name))}
      // If a click happened but this mesh wasn't hit we null out the target,
      // This works because missed pointers fire before the actual hits
      onPointerMissed={(e) => e.type === "click" && (state.current = null)}
      // Right click cycles through the transform modes
      onContextMenu={(e) =>
        snap.current === name &&
        (e.stopPropagation(), (state.mode = (snap.mode + 1) % modes.length))
      }
      onPointerOver={(e) => (e.stopPropagation(), setHovered(true))}
      onPointerOut={(e) => setHovered(false)}
      name={name}
      geometry={nodes[name].geometry}
      material={nodes[name].material}
      material-color={snap.current === name ? "#ff6080" : "white"}
      {...props}
      dispose={null}
    />
  );
}

function Controls() {
  // Get notified on changes to state
  const snap = useSnapshot(state);
  const scene = useThree((state) => state.scene);
  return (
    <>
      {/* As of drei@7.13 transform-controls can refer to the target by children, or the object prop */}
      {snap.current && (
        <TransformControls
          object={scene.getObjectByName(snap.current)}
          mode={modes[snap.mode]}
        />
      )}
      {/* makeDefault makes the controls known to r3f, now transform-controls can auto-disable them when active */}
      <OrbitControls
        makeDefault
        minPolarAngle={0}
        maxPolarAngle={Math.PI / 1.75}
      />
    </>
  );
}

export default function App() {
  return (
    <Canvas camera={{ position: [0, -10, 80], fov: 50 }} dpr={[1, 2]}>
      <pointLight
        position={[100, 100, 100]}
        intensity={0.8 * Math.PI}
        decay={0}
      />
      <hemisphereLight
        color="#ffffff"
        groundColor="#b9b9b9"
        position={[-7, 25, 13]}
        intensity={0.85 * Math.PI}
      />
      <Suspense fallback={null}>
        <group position={[0, 10, 0]}>
          <Model name="Curly" position={[1, -11, -20]} rotation={[2, 0, -0]} />
          <Model name="DNA" position={[20, 0, -17]} rotation={[1, 1, -2]} />
          <Model
            name="Headphones"
            position={[20, 2, 4]}
            rotation={[1, 0, -1]}
          />
          <Model
            name="Notebook"
            position={[-21, -15, -13]}
            rotation={[2, 0, 1]}
          />
          <Model
            name="Rocket003"
            position={[18, 15, -25]}
            rotation={[1, 1, 0]}
          />
          <Model
            name="Roundcube001"
            position={[-25, -4, 5]}
            rotation={[1, 0, 0]}
            scale={0.5}
          />
          <Model
            name="Table"
            position={[1, -4, -28]}
            rotation={[1, 0, -1]}
            scale={0.5}
          />
          <Model
            name="VR_Headset"
            position={[7, -15, 28]}
            rotation={[1, 0, -1]}
            scale={5}
          />
          <Model
            name="Zeppelin"
            position={[-20, 10, 10]}
            rotation={[3, -1, 3]}
            scale={0.005}
          />
          <ContactShadows
            rotation-x={Math.PI / 2}
            position={[0, -35, 0]}
            opacity={0.25}
            width={200}
            height={200}
            blur={1}
            far={50}
          />
        </group>
      </Suspense>
      <Controls />
    </Canvas>
  );
}
```

## src/index.tsx

```tsx
import { createRoot } from "react-dom/client";
import "./styles.css";
import App from "./App";

createRoot(document.getElementById("root")!).render(<App />);
```

## src/styles.css

```css
@import url("https://rsms.me/inter/inter.css");

* {
  box-sizing: border-box;
}

html,
body,
#root {
  margin: 0;
  padding: 0;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
}

body {
  font-family: "Inter var", sans-serif;
  background: linear-gradient(155deg, #ffffff 20%, #a5a3a3);
  color: #101015;
}

.overlay {
  padding: 40px;
  height: 100%;
  width: 100%;
  pointer-events: none;
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  justify-content: end;
  align-items: center;
}

a {
  pointer-events: all;
  position: absolute;
  top: 40px;
  left: 40px;
  color: #101015;
  text-decoration: none;
}

a.right {
  left: unset;
  right: 40px;
}

h1 {
  text-transform: uppercase;
  text-align: center;
  font-size: 5em;
  letter-spacing: -3px;
  font-weight: 500;
  padding: 0;
  margin: 0;
  line-height: 0.9em;
}

h1 > span {
  font-weight: 200;
}

h2 {
  margin-top: 1em;
  text-align: center;
  font-weight: 400;
  font-size: 0.9em;
  line-height: 1.4em;
  letter-spacing: 2px;
}
```

## src/vite-env.d.ts

```ts
/// <reference types="vite/client" />
```
