본문 바로가기
애니메이션

3D 애니메이션 효과

by dongjin6539 2023. 3. 21.
728x90
반응형

3D 애니메이션 효과

코드 보기 / 완성 화면

 

 

 

코드 블럭

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>webgl01</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html {
            width: 100%;
            height: 100vh;
            overflow: hidden;
            background: rgb(8,2,106);
            background: linear-gradient(90deg, rgba(8,2,106,1) 0%, rgba(9,9,121,1) 18%, rgba(5,75,148,1) 56%, rgba(0,212,255,1) 100%);
        }
    </style>
</head>
<body>
    <div id="canvas"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r77/three.min.js"></script>
    <script>
        let renderer, scene, camera, composer, circle, skelet, particle;

        window.onload = function(){
            init();
            animate();
        };

        function init(){
            renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
            renderer.setPixelRatio((window.devicePixelRatio) ? window.devicePixelRatio : 1);
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.autoClear = false;
            renderer.setClearColor(0x000000, 0.0);
            document.getElementById("canvas").appendChild(renderer.domElement);

            scene = new THREE.Scene();
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
            camera.position.z = 400;
            scene.add(camera);

            // 원
            circle = new THREE.Object3D();
            skelet = new THREE.Object3D();
            particle = new THREE.Object3D();

            // 화면 추가
            scene.add(circle);
            scene.add(skelet);
            scene.add(particle);

            let geom = new THREE.IcosahedronGeometry(7, 1);
            let mat = new THREE.MeshPhongMaterial({
                color: 0xffffff,
                shading: THREE.FlatShading
            });

            let planet = new THREE.Mesh(geom, mat);
            planet.scale.x = planet.scale.y = planet.scale.z = 16;
            circle.add(planet);

            let geom2 = new THREE.IcosahedronGeometry(15, 1);
            let mat2 = new THREE.MeshPhongMaterial({
                color: 0xffffff,
                wireframe: true,
                side: THREE.DoubleSide
            });

            let planet2 = new THREE.Mesh(geom2, mat2);
            planet2.scale.x = planet2.scale.y = planet2.scale.z = 10;
            skelet.add(planet2);

            let geometry = new THREE.TetrahedronGeometry(2, 0);
            let material = new THREE.MeshPhongMaterial({
                color: 0xffffff,
                shading: THREE.FlatShading
            });
            for(let i=0; i<1000; i++){
                let mesh = new THREE.Mesh(geometry, material);
                mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize();
                mesh.position.multiplyScalar(90 + (Math.random() * 700));
                mesh.rotation.set(Math.random()*2, Math.random()*2, Math.random()*2);
                particle.add(mesh);
            };

            // 조명
            let lights = [];
            lights[0] = new THREE.DirectionalLight(0xffffff, 1);
            lights[0].position.set(1,0,0);
            lights[1] = new THREE.DirectionalLight(0x00ffff, 1);
            lights[1].position.set(0.75,1,0.75);
            lights[2] = new THREE.DirectionalLight(0x0000ff, 1);
            lights[2].position.set(-0.75,-1,0.5);
            scene.add(lights[0]);  
            scene.add(lights[1]);  
            scene.add(lights[2]);  

            // 조명
            let ambientLight = new THREE.AmbientLight(0x999999, 0.3);
            scene.add(ambientLight);


            window.addEventListener("resize", onWindowResize, false);
        };

        function onWindowResize(){
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        };

        function animate(){
            requestAnimationFrame(animate);

            circle.rotation.x -= 0.003;
            circle.rotation.y -= 0.003;
            skelet.rotation.x += 0.01;
            skelet.rotation.y += 0.01;
            particle.rotation.x += 0.004;
            particle.rotation.y += 0.004;
            renderer.clear();

            renderer.render(scene, camera);
        };
    </script>
</body>
</html>

 

참고

지금까지 배운 내용을 이용해서 3D 애니메이션을 만들어봤습니다.

지금까지 배운 내용을 이용해서 한다고는 했지만 아직 대부분 모르는 내용이 너무 많았습니다.

아직 처음 보는 메서드나 속성이 많이 있습니다. 

나중에는 더 배워서 이해하면서 해봐야 될거 같네요.

728x90
반응형