
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@vctrl/hooks
Advanced tools
vctrl/hooks is a React hooks package designed to simplify 3D model loading and management within React applications. It's part of the vectreal-core ecosystem and is primarily used in the vctrl/viewer React component and the official website application.
Note: This library is still undergoing heavy development until the first major version is released. This may lead to breaking changes in upcoming updates.
@vctrl/hooks
is a React hooks package designed to simplify 3D model loading, optimization, and exporting within React applications. It's part of the vectreal-core ecosystem and is fully integrated into the official website application.
The package provides powerful hooks for:
useLoadModel
)useOptimizeModel
)useExportModel
)It also includes a React context (ModelContext
) for easy state management and an event system for handling different stages of the model loading process.
To install the package, use npm or yarn:
npm install @vctrl/hooks
# or
yarn add @vctrl/hooks
useLoadModel
is a React hook for loading and managing 3D model files in your application. It supports various file formats and integrates with Three.js for rendering.
.gltf
with associated .bin
and texture files)useOptimizeModel
for model optimizationHere's a basic example of how to use useLoadModel
:
import React from 'react';
import { useLoadModel } from '@vctrl/hooks/use-load-model';
function ModelLoader() {
const { load, file, progress, isLoading } = useLoadModel();
const onFileChange = (event) => {
const files = Array.from(event.target.files);
load(files);
};
return (
<div>
<input type="file" onChange={onFileChange} multiple />
{isLoading && <p>Loading: {progress}%</p>}
{file && <p>Model loaded: {file.name}</p>}
</div>
);
}
Note: Multiple files can be handled, e.g., when uploading a
.gltf
model along with its.bin
file and relevant texture files (e.g.,.jpeg
,.png
).
You can also use the ModelProvider
and useModelContext
to access the model state across your application:
// App.jsx
import React from 'react';
import { ModelProvider } from '@vctrl/hooks/use-load-model';
import ModelConsumer from './ModelConsumer';
function App() {
return (
<ModelProvider>
<ModelConsumer />
</ModelProvider>
);
}
// ModelConsumer.jsx
import React from 'react';
import { useModelContext } from '@vctrl/hooks/use-load-model';
function ModelConsumer() {
const { file, isLoading, progress, load } = useModelContext();
// Use the context values
return (
// ... your component logic
);
}
Note: When using the React context, load models using the
useModelContext
hook.
The useLoadModel
hook returns the following:
file
: The loaded file object (ModelFile | null
).isLoading
: A boolean indicating whether a file is currently being loaded.progress
: A number between 0 and 100 representing the loading progress.load(files)
: A function to handle file uploads. It accepts an array of File
objects or a mixed array of File
objects and directory entries.reset()
: A function to reset the internal state back to its initial values.on(event, handler)
: A function to subscribe to events.off(event, handler)
: A function to unsubscribe from events.optimize
: An object populated by the useOptimizeModel
hook integration, providing optimization functions (see below).If you pass an instance of useOptimizeModel
to useLoadModel
, it will integrate optimization functions into the optimize
object:
import React from 'react';
import { useLoadModel } from '@vctrl/hooks/use-load-model';
import { useOptimizeModel } from '@vctrl/hooks/use-optimize-model';
function ModelLoader() {
const optimizer = useOptimizeModel();
const { load, file, optimize } = useLoadModel(optimizer);
const handleSimplify = async () => {
await optimize.simplifyOptimization();
// The optimized model is now in file.model
};
return (
// ... your component logic
);
}
The optimize
object includes quick access optimization:
simplifyOptimization(options)
: Simplifies the model using mesh simplification.dedupOptimization(options)
: Removes duplicate vertices and meshes.quantizeOptimization(options)
: Reduces the precision of vertex attributes.normalsOptimization(options)
: Overrides the normals of each object in the scene.texturesCompressionOptimization(options)
: Compresses the relevant textures in the model file using texture compression.useOptimizeModel
is a React hook used to optimize 3D models, particularly GLTF-based scenes. It can be used in conjunction with useLoadModel
or independently.
useLoadModel
and ModelContext
There are two ways to use the useOptimizeModel
hook:
Directly with useLoadModel
import React from 'react';
import { useLoadModel } from '@vctrl/hooks/use-load-model';
import { useOptimizeModel } from '@vctrl/hooks/use-optimize-model';
function ModelLoader() {
const optimizer = useOptimizeModel();
const { load, file, optimize } = useLoadModel(optimizer);
const handleSimplify = async () => {
await optimize.simplifyOptimization();
// The optimized model is now in file.model
};
const handleDedup = async () => {
await optimize.dedupOptimization();
};
const handleQuantize = async () => {
await optimize.quantizeOptimization();
};
return (
<div>
<input
type="file"
onChange={(e) => load(Array.from(e.target.files))}
multiple
/>
<button onClick={handleSimplify}>Simplify Model</button>
<button onClick={handleDedup}>Deduplicate Model</button>
<button onClick={handleQuantize}>Quantize Model</button>
</div>
);
}
Note: Changes are applied to the
file.model
field fromuseLoadModel
automatically when using optimizations.
With ModelProvider
and useModelContext
// App.jsx
import React from 'react';
import { ModelProvider } from '@vctrl/hooks/use-load-model';
import { useOptimizeModel } from '@vctrl/hooks/use-optimize-model';
import Scene from './Scene';
function App() {
const optimizer = useOptimizeModel();
return (
<ModelProvider optimizer={optimizer}>
<Scene />
</ModelProvider>
);
}
// Scene.jsx
import React from 'react';
import { useModelContext } from '@vctrl/hooks/use-load-model';
function Scene() {
const { optimize } = useModelContext();
const handleSimplify = async () => {
await optimize.simplifyOptimization();
};
const handleDedup = async () => {
await optimize.dedupOptimization();
};
const handleQuantize = async () => {
await optimize.quantizeOptimization();
};
return (
<div>
<button onClick={handleSimplify}>Simplify Model</button>
<button onClick={handleDedup}>Deduplicate Model</button>
<button onClick={handleQuantize}>Quantize Model</button>
</div>
);
}
The useOptimizeModel
hook returns the following:
load(model)
: Loads a Three.js Object3D
model into the optimizer.getModel()
: Retrieves the optimized model as a binary array buffer.simplifyOptimization(options)
: Simplifies the current model using the MeshoptSimplifier
.dedupOptimization(options)
: Removes duplicate vertices and meshes.quantizeOptimization(options)
: Reduces the precision of vertex attributes.normalsOptimization
: Overrides normalstexturesOptimization(options)
: Compresses related textures.getSize()
: Object with byte size of gltf scene and a formatted megabyte stringreset()
: Resets the current optimizer model and report state.report
: @gltf-transform gltf report object with relevant details about a gltf sceneerror
: Stores any possible optimization errorsloading
: Boolean for when the model is being loadeduseExportModel
is a React hook for exporting 3D models from a Three.js scene.
import React from 'react';
import { useExportModel } from '@vctrl/hooks/use-export-model';
function ExportButton({ file }) {
const { handleGltfExport } = useExportModel(
() => console.log('Export complete'),
(error) => console.error('Export error:', error),
);
const exportAsGlb = () => {
handleGltfExport(file, true); // Export as GLB (binary)
};
const exportAsGltf = () => {
handleGltfExport(file, false); // Export as GLTF
};
return (
<div>
<button onClick={exportAsGlb}>Export as GLB</button>
<button onClick={exportAsGltf}>Export as GLTF</button>
</div>
);
}
The useExportModel
hook returns the following:
handleGltfExport(file, binary)
: Function to handle exporting the model.
file
: The ModelFile
object to export.binary
: A boolean indicating whether to export in binary format (true
for GLB, false
for GLTF).ModelContext
is a React context that provides the state and functions from useLoadModel
(and optionally useOptimizeModel
) to child components.
import { ModelProvider, useModelContext } from '@vctrl/hooks/use-load-model';
function App() {
return (
<ModelProvider>
<ModelConsumer />
</ModelProvider>
);
}
function ModelConsumer() {
const { file, isLoading, progress, load, optimize } = useModelContext();
// Use the context values
}
Note: When using the React context, load models using the
useModelContext
hook.
The package currently supports the following 3D model file formats:
.gltf
).glb
).usdz
)These are defined in the ModelFileTypes
enum in types.ts
.
The file loading process, particularly for GLTF files, is handled internally by the hooks. The process includes:
GLTFLoader
to parse the modified GLTF content.The loading process includes progress updates, which are communicated through the event system.
The package uses a reducer pattern for state management. The state includes:
file
: The currently loaded model file.isLoading
: A boolean indicating if a file is being loaded.progress
: The current loading progress.supportedFileTypes
: An array of supported file types.Actions for updating the state are defined in the Action
type in types.ts
.
The package integrates with Three.js for handling 3D model rendering and manipulation. The loaded models are compatible with Three.js scenes, with the model stored as a Three.js Object3D
in the ModelFile
interface.
This package is part of a monorepo workspace managed with Nx. To contribute or modify the package:
npm install
or yarn install
.nx build vctrl/hooks
.nx test vctrl/hooks
.This project is licensed under the GNU Affero General Public License v3.0. Please refer to the LICENSE file in the package root for licensing information.
Contributions are welcome! Please read the contributing guidelines in the vectreal-core monorepo before submitting pull requests.
For issues, feature requests, or questions, please file an issue in the GitHub repository.
0.9.5 (2025-03-14)
FAQs
vctrl/hooks is a React hooks package designed to simplify 3D model loading and management within React applications. It's part of the vectreal-core ecosystem and is primarily used in the vctrl/viewer React component and the official website application.
The npm package @vctrl/hooks receives a total of 3 weekly downloads. As such, @vctrl/hooks popularity was classified as not popular.
We found that @vctrl/hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.