1. 程式人生 > >64 - three.js 筆記 - 載入紋理並應用到網格

64 - three.js 筆記 - 載入紋理並應用到網格

紋理最基礎的用法就是在材質上貼圖,將圖片通過THREE.TextureLoader載入,然後設定為材質的map屬性。

1、示例

http://ithanmang.com/threeJs/home/201809/20180905/01-basic-texture.html
效果
這裡寫圖片描述

2、Texture

2.1、簡介

Texture
建立一個紋理來應用於一個表面或者作為一個反射或折射貼圖。
可以用來載入很多格式的圖片
PNG, JPG, GIF, DDS......
也可以載入視訊貼圖MP4, OGG/OGV,載入視訊使用THREE.VideoTexture

2.2、使用方法

載入紋理使用THREE.TextureLoader

注意: 紋理的載入時非同步執行的,作為紋理的圖片大小其長寬最好是2的次方,例如256 * 256 、512 * 512 、1024 * 1024...

例項化載入器
 // 例項化載入器
var loader = new THREE.TextureLoader();

// 載入檔案
loader.load(
    // 檔案 url
    'textures/land_ocean_ice_cloud_2048.jpg',

    // 載入完成後的回撥函式
    function ( texture ) {
        // 紋理被載入時建立材質
        var material = new
THREE.MeshBasicMaterial( { map: texture } ); }, // 加載出錯的回撥函式 function ( err ) { console.error( 'An error happened.' ); } );

也可以直接不用在回撥函式中建立材質

 let texture = new THREE.TextureLoader().load('../../textures/general/'+imageUrl);

 let material = new THREE.MeshPhongMaterial();
 material.
map = texture;

因為紋理的載入時非同步的,所以如果紋理圖片比較大的話等材質已經建立完畢了但是紋理還未加載出來。

3、示例程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../../../three.png">
    <title>基礎紋理</title>
    <style>
        body {
            margin: 0;
            overflow: hidden; /* 溢位隱藏 */
        }
        #loading {
            position: fixed;
            top: 50%;
            left: 50%;
            color: #FFFFFF;
            font-size: 20px;
            margin-top: -30px;
            margin-left: -40px;
        }
    </style>
    <script src="../../libs/build/three-r93.js"></script>
    <script src="../../libs/examples/js/Detector.js"></script>
    <script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
    <script src="../../libs/examples/js/libs/stats.min.js"></script>
    <script src="../../libs/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<p id="loading">loading......</p>
<script>

    let scene, camera, renderer, controls, guiControls, tween;
    let stats = initStats();

    /* 場景 */
    function initScene() {

        scene = new THREE.Scene();
        scene.background = new THREE.Color(0x050505);

    }

    /* 相機 */
    function initCamera() {

        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(0, 0, -50);
        camera.lookAt(new THREE.Vector3(0, 0, 0));

    }

    /* 渲染器 */
    function initRender() {

        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

    }

    /* 燈光 */
    function initLight() {

        scene.add(new THREE.AmbientLight(0x0c0c0c));

        let spotLight1 = new THREE.SpotLight(0xffffff);
        spotLight1.position.set(-400, -400, -400);

        let spotLight2 = new THREE.SpotLight(0xffffff);
        spotLight2.position.set(400, 400, 400);

        scene.add(spotLight1);
        scene.add(spotLight2);

    }

    /* 控制器 */
    function initControls() {

        /* 地圖控制元件 */
        controls = new THREE.OrbitControls(camera, renderer.domElement);

        /* 屬性引數 */

    }

    /* 除錯外掛 */
    function initGui() {

        guiControls = new function () {


        };

        let gui = new dat.GUI();


    }

    /* 場景中的內容 */
    let sphere;
    let box;
    function initContent() {

        sphere = createMesh(new THREE.SphereGeometry(5, 20, 20), 'metal-rust.jpg');
        box = createMesh(new THREE.BoxGeometry(7, 7, 7), 'brick-wall.jpg');

        box.translateX(7);
        sphere.translateX(-7);
        scene.add(sphere);
        scene.add(box);

        removeLoading();

    }

    /* 移除載入元素 */
    function removeLoading() {

        document.getElementById('loading').style.display = 'none';

    }
    /* 建立帶有紋理的網格 */
    function createMesh(geometry, imageUrl) {

        let texture = new THREE.TextureLoader().load('../../textures/general/'+imageUrl);

        let material = new THREE.MeshPhongMaterial();
        material.map = texture;

        let mesh = new THREE.Mesh(geometry, material);

        return mesh;

    }

    /* 效能外掛 */
    function initStats() {

        let stats = new Stats();

        document.body.appendChild(stats.domElement);

        return stats;

    }

    /* 視窗變動觸發 */
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    /* 資料更新 */
    function update() {

        stats.update();

        controls.update();

        if (sphere) {
            // sphere.rotationY
            sphere.rotateY(0.01);
        }
        if (box) {
            box.rotateY(0.01);
        }

    }

    /* 初始化 */
    function init() {

        initScene();
        initCamera();
        initRender();
        initLight();
        initControls();
        initContent();
        initGui();
        /* 監聽事件 */
        window.addEventListener('resize', onWindowResize, false);

    }

    /* 迴圈渲染 */
    function animate() {

        requestAnimationFrame(animate);
        renderer.render(scene, camera);
        update();

    }

    /* 初始載入 */
    (function () {
        console.log("three init start...");

        init();
        animate();

        console.log("three init end...");
    })();

</script>
</body>
</html>