i know components cant have use Effect by themselves so i was wondering how to rephrase this code to be used as a component for the parent app if its possible and what would the parent app look like when rephrased. I’ve looked around for examples and tutorials but the scene is always in the main parent app but i want to split up my code instead…
Heres my code:
import { useEffect } from 'react';
import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
// import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
// import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
// import { MathEx } from '@ykob/js-util';
// import Background from './background';
import fs from '../glsl/cage.fs';
import vs from '../glsl/cage.vs';
import backgroundvs from '../glsl/backgroundvs.vs'
import backgroundfs from '../glsl/backgroundfs.fs'
import handFs from'../glsl/handFs.fs'
import handVs from'../glsl/handVs.vs'
import coral from'../img//Coral_002_SD/Coraln.jpg'
import SceneInit from './lib/SceneInit';
import Content from './App.jsx'
import RadarChart from './components/radarChart';
function CardT {
useEffect(() => {
const portF = new SceneInit('myThreeJsCanvas');
portF.initialize();
portF.animate();
// portF.clock = new THREE.Clock()
//
let loader = new GLTFLoader();
// Load a glTF resource
loader.load('../img/cage.gltf', function (gltf) {
const cage = gltf.scene
const uniformData = {
// time: {
// type: 'f',
// value: 0.0
// },
// colorA: { type: 'vec3', value: new THREE.Color(0xFFC000) },
// colorB: { type: 'vec3', value: new THREE.Color(0xFFC000) }
}
const cageMat = new THREE.ShaderMaterial({
uniforms: uniformData,
vertexShader: vs,
fragmentShader: fs,
wireframe: true,
blending: THREE.NormalBlending,
depthTest: false,
})
// const cageGeom = createTubeWireframe(geometry, {
// thickness: 0.03, // thickness in world units of tubes
// radiusSegments: 6, // number of segments around the tubes
// mode: 'quad' // face layout, use quads instead of triangles
// });
cage.scale.set(6, 6, 6)
cage.transparent = true
cage.traverse((o) => {
if (o.isMesh) o.material = cageMat
})
portF.scene.add(cage);
})
// hands
const hUniform = {
uTime:{value:0},
coral:{type:"t", value: new THREE.TextureLoader().load(coral)},
iResolution:{ value: new THREE.Vector3()},
// uRadius:{value: 0.5},
// uTexture: {value: new THREE.TextureLoader().load(wavyTexture)}
}
async function loadmodels() {
let loader2 = new GLTFLoader()
const [...model] = await Promise.all([
loader2.loadAsync('./modelsGLTF/lphand/lphand.gltf'),
loader2.loadAsync('./modelsGLTF/2hand/2hand.gltf'),
loader2.loadAsync('./modelsGLTF/handTOPb/handTOPb.gltf'),
]);
const handMat = new THREE.ShaderMaterial({
uniforms:hUniform,
fragmentShader:handFs,
vertexShader:handVs,
blending: THREE.NormalBlending,
})
const polyHand = model[0].scene
const zHand = model[1].scene
const hHand = model[2].scene
polyHand.position.set(0,-14,-2)
zHand.position.set(7,-14,8)
hHand.position.set(-7,-22,8)
polyHand.rotateX(3)
polyHand.rotateY(1.1)
zHand.rotateX(3.1)
zHand.rotateZ(4.8)
hHand.rotateX(4)
hHand.rotateZ(2)
polyHand.scale.set(0.25,0.25,0.25)
zHand.scale.set(0.07,0.07,0.07)
hHand.scale.set(1,1,1)
portF.scene.add(polyHand)
portF.scene.add(zHand)
portF.scene.add(hHand)
polyHand.traverse((h) => {
if (h.isMesh) h.material = handMat
})
zHand.traverse((hb) => {
if (hb.isMesh) hb.material = handMat
})
hHand.traverse((hc) => {
if (hc.isMesh) hc.material = handMat
})
}
loadmodels()
// particle
// bg
const bgUniforms = {
uTime:{value:0},
uRadius:{value: 0.5}
}
const bgGeometry = new THREE.SphereGeometry(100);
// Define Material
const bgMaterial = new THREE.ShaderMaterial({
vertexShader: backgroundvs,
fragmentShader: backgroundfs,
side: THREE.BackSide,
uniforms: bgUniforms,
depthTest: false
});
const bg = new THREE.Mesh(bgGeometry, bgMaterial)
portF.scene.add(bg)
// bg2
const otAo = new THREE.TextureLoader().load('../img/fP/futuristic-p-ao.png')
otAo.wrapS = THREE.RepeatWrapping;
otAo.wrapT = THREE.RepeatWrapping;
otAo.repeat.set( 3, 3 );
const otGeo = new THREE.SphereGeometry(120,120);
const otMat = new THREE.MeshPhysicalMaterial({
// color:0xf08080,
// emissive:0x00eeff,
emissiveIntensity: 0.4,
clearcoatMap:otAo,
// displacementMap:otDis,
side:THREE.DoubleSide,
roughness:0,
metalness:0,
transmission:1,
transmissionMap:otAo,
clearcoat:1,
ior:1,
})
const otbg = new THREE.Mesh(otGeo,otMat)
portF.scene.add(otbg)
// card
const texture1 = new THREE.TextureLoader().load('../img/joshcoin3small.png')
const texture2 = new THREE.TextureLoader().load('../img/joshcoin3small2.jpg')
const cardgeoFront = new THREE.PlaneGeometry(6, 8);
const cardgeoBack = new THREE.PlaneGeometry(6, 8);
const mat1 = new THREE.MeshBasicMaterial({
map: texture1
})
const mat2 = new THREE.MeshBasicMaterial({
map: texture2,
side: THREE.BackSide
})
const fcard = new THREE.Mesh(cardgeoFront, mat1);
const bcard = new THREE.Mesh(cardgeoBack, mat2);
const card = new THREE.Group();
card.add(fcard);
card.add(bcard);
portF.scene.add(card);
card.rotateZ(0.4)
const animate = () => {
hUniform.uTime.value = (portF.clock.getElapsedTime())
portF.onWindowResize()
requestAnimationFrame(animate);
};
animate();
}, []);
return (
<div>
<canvas id="myThreeJsCanvas" />
</div>
);
}
export default CardT;
Im relatively new to three.js and react so sorry for any obvious errors