What’s on our mind?

Collection of articles, design, site, and resources made by designers and publisher @Menu View

** index43
class Stage {
    constructor() {
        this.renderParam = {
            clearColor: 0xffffff,
            width: window.innerWidth,
            height: window.innerHeight
        }

        this.cameraParam = {
            fov: 50,
            near: 0.01,
            far: 1000,
            lookAt: new THREE.Vector3(0, 0, 0),
            x: 0,
            y: 0,
            z: 7
        }

        this.scene = null;
        this.camera = null;
        this.renderer = null;
        this.isInitialized = false;
    }

    init() {
        this._setScene();
        this._setRender();
        this._setCamera();
        this.isInitialized = true;
    }

    _setScene() {
        this.scene = new THREE.Scene();
    }

    _setRender() {
        this.renderer = new THREE.WebGLRenderer({
            canvas: document.getElementById("webgl-canvas"),
            antialias: true,
            alpha: true
        });
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setSize(this.renderParam.width, this.renderParam.height);
    }

    _setCamera() {
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;

        if (!this.isInitialized) {
            this.camera = new THREE.PerspectiveCamera(
                this.cameraParam.fov,
                this.renderParam.width / this.renderParam.height,
                this.cameraParam.near,
                this.cameraParam.far
            );

            this.camera.position.set(
                this.cameraParam.x,
                this.cameraParam.y,
                this.cameraParam.z
            );
            this.camera.lookAt(this.cameraParam.lookAt);

            this.orbitcontrols = new THREE.OrbitControls(
                this.camera,
                this.renderer.domElement
            );
            this.orbitcontrols.enableDamping = true;
        }

        this.camera.aspect = windowWidth / windowHeight;
        this.camera.updateProjectionMatrix();
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setSize(windowWidth, windowHeight);
    }

    _render() {
        this.renderer.render(this.scene, this.camera);
        this.orbitcontrols.update();
    }

    onResize() {
        this._setCamera();
    }

    onRaf() {
        this._render();
    }
}

class Mesh {
    constructor(stage) {
        this.geometryParam = {
            width: 1,
            height: 7
        }

        this.uniforms = {
            time: {
                type: "f",
                value: 0.0
            }
        }

        this.stage = stage;
    }

    init() {
        this._setMesh();
    }

    _setMesh() {
        const geometry = new THREE.IcosahedronBufferGeometry(
            this.geometryParam.width,
            this.geometryParam.height
        );

        const material = new THREE.ShaderMaterial({
            vertexShader: document.getElementById("js-vertex-shader").textContent,
            fragmentShader: document.getElementById("js-fragment-shader").textContent,
            uniforms: this.uniforms,
            side: THREE.DoubleSide
        });

        const material1 = new THREE.ShaderMaterial({
            vertexShader: document.getElementById("js-vertex-shader").textContent,
            fragmentShader: document.getElementById("js-fragment-line").textContent,
            uniforms: this.uniforms,
            side: THREE.DoubleSide
        });

        this.mesh = new THREE.Mesh(geometry, material);
        this.meshPoints = new THREE.Points(geometry, material1);
        
        this.stage.scene.add(this.mesh);
        this.stage.scene.add(this.meshPoints);
        this.stage.scene.add(this.meshSphere);

        this.meshPoints.scale.set(1.5 + 0.1, 1.5 + 0.1, 1.5 + 0.1);
    }

    _render() {
        this.uniforms.time.value += 0.001;
        this.stage.scene.rotation.x += 0.1 * Math.sin(0.001 * 4 * Math.PI);
        this.stage.scene.rotation.y += 0.2 * Math.sin(0.001 * 2 * Math.PI);
        this.stage.scene.rotation.z += 0.3 * Math.sin(0.001 * 2 * Math.PI);
    }

    onRaf() {
        this._render();
    }
}

(() => {
    const stage = new Stage();
    const mesh = new Mesh(stage);

    stage.init();
    mesh.init();

    window.addEventListener("resize", () => {
        stage.onResize();
    });

    const _raf = () => {
        window.requestAnimationFrame(() => {
            stage.onRaf();
            mesh.onRaf();

            _raf();
        });
    }

    _raf();
})();