Newer
Older
escapegame-2022 / tsubasa / blender_three.js / index.html
@C121092 C121092 on 19 Dec 2022 3 KB add
<!DOCTYPE html>
<html lang="ja">
<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>Document</title>    
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <div id="WebGL-output">
    </div>
 
<script type="module">
    import * as THREE from'https://unpkg.com/three@0.126.1/build/three.module.js';
    import { OrbitControls } from 'https://unpkg.com/three@0.126.1/examples/jsm/controls/OrbitControls.js';
    import { GLTFLoader } from 'https://unpkg.com/three@0.126.1/examples/jsm/loaders/GLTFLoader.js';
 
    let camera;
    let scene;
    let renderer;
    let model;
 
    init();
    animate();
    
    function init() {
        //シーンの作成
        scene = new THREE.Scene();
        scene.background = new THREE.Color( 0x5ea832); // 背景色
 
        //カメラの作成
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
        //カメラセット
        camera.position.set(8, 8, 50); // カメラの位置
        camera.lookAt(new THREE.Vector3(0, 5, 0));
        
         // 滑らかにカメラコントローラーを制御する
        const controls = new OrbitControls(camera, document.body);
        controls.enableDamping = true;
        controls.dampingFactor = 0.2; //回転の滑らかさ
 
        // //光源
        // const dirLight = new THREE.SpotLight(0xffffff,1.2);//color,強度
        // dirLight.position.set(10, 10, 20); // 光源の位置
        // scene.add(dirLight);
 
        //レンダラー
        renderer = new THREE.WebGLRenderer({ 
            alpha: true,
            antialias: true
        });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.physicallyCorrectLight = true
 
        //glbファイルの読み込み
        const loader = new GLTFLoader();
 
        loader.load('tohokukoeki_univ.glb', function(gltf) {
            model = gltf.scene;
            model.traverse((object) => { //モデルの構成要素
                if(object.isMesh) { //その構成要素がメッシュだったら
                object.material.trasparent = true;//透明許可
                object.material.opacity = 0.8;//透過
                }})
            scene.add(model);
        });
        loader.load('ball.glb', function(gltf) {
            model = gltf.scene;
            scene.add(model);
            model.position.set(0, -10, 0);
        });
 
        document.getElementById("WebGL-output").appendChild(renderer.domElement);

    }

    const mouse = new THREE.Vector2();

    function animate() {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
    }; 

    // クリックイベント
    document.addEventListener('click', OnClick, false);

    // クリックしたら呼ばれる
    function OnClick(event) {
        const raycaster = new THREE.Raycaster();
        const mouse = new THREE.Vector2();

        // マウスポインタの位置情報の取得
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;

        // 光線を発射
        raycaster.setFromCamera(mouse, camera);
        // 光線と交わるオブジェクトを収集
        const intersects = raycaster.intersectObjects(scene.children, true);

        // 交わるオブジェクトが1個以上の場合
        if (intersects.length > 0) {
            if (intersects[0].object.name == "球") {
                window.open('./game.html')
            };
            console.log(intersects[0])
        }
    }
 
</script>
    
    
</body>
</html>