// this my code react three fiber typescript import { Environment, Loader,…
- User
// this my code react three fiber typescript
import { Environment, Loader, OrbitControls, useGLTF } from "@react-three/drei"; import { Canvas } from "@react-three/fiber";
const World = () => { const gltf = useGLTF("/earth.glb");
return ( <> </> ); };
export default World;
//add this code but convert in react three fiber and typescript
///// LOADING GLB/GLTF MODEL FROM BLENDER loader.load('models/gltf/Skull.glb', function (gltf) {
codegltf.scene.traverse((obj) => { if (obj.isMesh) { sampler = new MeshSurfaceSampler(obj).build() } }) transformMesh()})
///////////////////////////////////////////////////////////////////////// ///// TRANSFORM MESH INTO POINTS let sampler let uniforms = { mousePos: {value: new THREE.Vector3()}} let pointsGeometry = new THREE.BufferGeometry() const cursor = {x:0, y:0} const vertices = [] const tempPosition = new THREE.Vector3()
function transformMesh(){ // Loop to sample a coordinate for each points for (let i = 0; i < 99000; i ++) { // Sample a random position in the model sampler.sample(tempPosition) // Push the coordinates of the sampled coordinates into the array vertices.push(tempPosition.x, tempPosition.y, tempPosition.z) }
code// Define all points positions from the previously created array pointsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)) // Define the matrial of the points const pointsMaterial = new THREE.PointsMaterial({ color: 0x5c0b17, size: 0.1, blending: THREE.AdditiveBlending, transparent: true, opacity: 0.8, depthWrite: false, sizeAttenuation: true, alphaMap: new THREE.TextureLoader().load('particle-texture.jpg') }) // Create the custom vertex shader injection pointsMaterial.onBeforeCompile = function(shader) { shader.uniforms.mousePos = uniforms.mousePos shader.vertexShader = ` uniform vec3 mousePos; varying float vNormal; ${shader.vertexShader}`.replace( `#include <begin_vertex>`, `#include <begin_vertex> vec3 seg = position - mousePos; vec3 dir = normalize(seg); float dist = length(seg); if (dist < 1.5){ float force = clamp(1.0 / (dist * dist), -0., .5); transformed += dir * force; vNormal = force /0.5; } ` ) } // Create an instance of points based on the geometry & material const points = new THREE.Points(pointsGeometry, pointsMaterial) // Add them into the main group scene.add(points) - Assistant