у меня есть код, подскажи как вместо строки import * as THREE from 'three'; подключить…
- User
у меня есть код, подскажи как вместо строки import * as THREE from 'three'; подключить только нужные мне модули для работы этого кода? Чтобы не подключать всю библиотеку three код: import * as THREE from 'three'; import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js'; const loader = new THREE.TextureLoader(); const gltfLoader = new GLTFLoader(); const model = 'cube animationglb16.glb'; const texture = loader.load('img/image.png'); texture.colorSpace = THREE.SRGBColorSpace; const renderer = new THREE.WebGL1Renderer({ antialias: true, alpha: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; document.body.appendChild(renderer.domElement); const scene = new THREE.Scene(); scene.fog = new THREE.FogExp2(0xE2E9D4, 0.05); const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.x = 5; camera.position.y = 5; camera.position.z = 5; const light2 = new THREE.AmbientLight( 0xffffff, 2.8 ); scene.add(light2); gltfLoader.load( model, ( gltf ) => { const modelWithCube = gltf.scene; const animations = gltf.animations; const mixer = new THREE.AnimationMixer(modelWithCube); for (let i = 0; i < animations.length; i++) { const clip = animations[i]; animations[i].playbackSpeed = 0.01; mixer.clipAction(clip).play(); } const array = modelWithCube.children; console.log(modelWithCube.castShadow); for (let i = 0; i < array.length; i++) { if (modelWithCube.children[i].type == 'Mesh') { modelWithCube.children[i].material.side = THREE.DoubleSide; modelWithCube.children[i].castShadow = true; modelWithCube.children[i].receiveShadow = true; } if (modelWithCube.children[i].name == 'Cube004') { modelWithCube.children[i].material.castShadow = true; modelWithCube.children[i].scale.set(0,0,0); } if (modelWithCube.children[i].name == 'Cube005') { } } scene.add( modelWithCube ); const clock = new THREE.Clock(); function animate() { const deltaTime = clock.getDelta(); requestAnimationFrame(animate); mixer.update(deltaTime/4); const cameraLookAtCube = modelWithCube.children[2]; camera.lookAt(cameraLookAtCube.position); const cameraPositionCube = modelWithCube.children[0]; camera.position.copy( cameraPositionCube.position ); } animate(); }); function resizeRendererToDisplaySize(renderer) { const canvas = renderer.domElement; const width = canvas.clientWidth; const height = canvas.clientHeight; const needResize = canvas.width !== width || canvas.height !== height; if (needResize) { renderer.setSize(width, height, false); } return needResize; } let time = 0; function render() { if (resizeRendererToDisplaySize(renderer)) { canvas = renderer.domElement; camera.aspect = canvas.clientWidth / canvas.clientHeight; camera.updateProjectionMatrix(); } time += 0.01; renderer.render( scene, camera ); requestAnimationFrame( render ); } render();
- Assistant
Для подключения только нужных модулей из библиотеки 'three', вам необходимо изменить импорт следующим образом:
import { TextureLoader, WebGL1Renderer, Scene, PerspectiveCamera, AmbientLight, DoubleSide, Clock } from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; const loader = new TextureLoader(); const gltfLoader = new GLTFLoader(); const model = 'cube animationglb16.glb'; const texture = loader.load('img/image.png'); texture.colorSpace = THREE.SRGBColorSpace; const renderer = new WebGL1Renderer({ antialias: true, alpha: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; document.body.appendChild(renderer.domElement); const scene = new Scene(); scene.fog = new THREE.FogExp2(0xE2E9D4, 0.05); const camera = new PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.x = 5; camera.position.y = 5; camera.position.z = 5; const light2 = new AmbientLight(0xffffff, 2.8); scene.add(light2); gltfLoader.load(model, (gltf) => { const modelWithCube = gltf.scene; const animations = gltf.animations; const mixer = new THREE.AnimationMixer(modelWithCube); for (let i = 0; i < animations.length; i++) { const clip = animations[i]; animations[i].playbackSpeed = 0.01; mixer.clipAction(clip).play(); } const array = modelWithCube.children; console.log(modelWithCube.castShadow); for (let i = 0; i < array.length; i++) { if (modelWithCube.children[i].type == 'Mesh') { modelWithCube.children[i].material.side = DoubleSide; modelWithCube.children[i].castShadow = true; modelWithCube.children[i].receiveShadow = true; } if (modelWithCube.children[i].name == 'Cube004') { modelWithCube.children[i].material.castShadow = true; modelWithCube.children[i].scale.set(0, 0, 0); } if (modelWithCube.children[i].name == 'Cube005') { } } scene.add(modelWithCube); const clock = new Clock(); function animate() { const deltaTime = clock.getDelta(); requestAnimationFrame(animate); mixer.update(deltaTime / 4); const cameraLookAtCube = modelWithCube.children[2]; camera.lookAt(cameraLookAtCube.position); const cameraPositionCube = modelWithCube.children[0]; camera.position.copy(cameraPositionCube.position); } animate(); }); function resizeRendererToDisplaySize(renderer) { const canvas = renderer.domElement; const width = canvas.clientWidth; const height = canvas.clientHeight; const needResize = canvas.width !== width || canvas.height !== height; if (needResize) { renderer.setSize(width, height, false); } return needResize; } let time = 0; function render() { if (resizeRendererToDisplaySize(renderer)) { canvas = renderer.domElement; camera.aspect = canvas.clientWidth / canvas.clientHeight; camera.updateProjectionMatrix(); } time += 0.01; renderer.render(scene, camera); requestAnimationFrame(render); } render();Таким образом, вы подключите только необходимые модули для работы вашего кода, а не всю библиотеку 'three'.