threejs GLTFLoader

package.json

{
  "name": "gltf-loader-code",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "dependencies": {
    "three": "0.110.0"
  },
  "devDependencies": {
    "html-webpack-plugin": "4.5.0",
    "ts-loader": "6.2.2",
    "typescript": "4.3.5",
    "webpack": "4.42.1",
    "webpack-cli": "3.3.12",
    "webpack-dev-server": "3.11.2"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "noEmit": true,
        "module": "esnext",
        "target": "es6",
        "lib": ["ES2019","dom"],
        "sourceMap": true,
        "moduleResolution": "node",
        "forceConsistentCasingInFileNames": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "skipLibCheck": true,
        "suppressImplicitAnyIndexErrors": true,
        "experimentalDecorators": true,
        "noUnusedLocals": true,
        "downlevelIteration": true,
        "strict": true,
        "strictPropertyInitialization": false,
        "allowSyntheticDefaultImports": true,
        "skipDefaultLibCheck": true
    },
    "exclude": [
        "node_modules",
    ]
}

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const { resolve } = require('path');

module.exports = {
    devtool: 'source-map',
    plugins: [
        new HtmlWebpackPlugin({
            template: resolve('./index.html')
        })
    ],
    resolve: {
        extensions: ['.ts', '.js']
    },
    module:{
            rules:[
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    loader: 'ts-loader',
                    options: {
                        transpileOnly: true,
                        experimentalWatchApi: true,
                    }
                },
            ],
        },
    devServer: {
        compress: true,
        stats: {
            assets: false,
            builtAt: true,
            modules: false,
            entrypoints: false,
            contentBase: resolve(__dirname, './'),
            /**
             * ts-node transpileOnly: true
             * if you enable this option, webpack 4 will give you "export not found" warnings any time you re-export a type:
             * The reason this happens is that when typescript doesn't do a full type check, 
             * it does not have enough information to determine whether an imported name is a type or not, 
             * so when the name is then exported, typescript has no choice but to emit the export. 
             * Fortunately, the extraneous export should not be harmful, so you can just suppress these warnings:
             */
            // warningsFilter: /export .* was not found in/
        },
    },
};

src/index.ts

import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';

// 創(chuàng)建相機(jī)
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.x = 150;
camera.position.z = 50;
camera.position.y= 100;

// 創(chuàng)建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

// 創(chuàng)建場景
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeaeaea);

// 添加燈光
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);


// 添加控制器
new OrbitControls(camera, document.body);

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

const loader = new GLTFLoader();
loader.load('./t.glb', (gltf) => {
    scene.add(gltf.scene);
});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末搪泳,一起剝皮案震驚了整個濱河市丰包,隨后出現(xiàn)的幾起案子述雾,更是在濱河造成了極大的恐慌,老刑警劉巖卖怜,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耍鬓,死亡現(xiàn)場離奇詭異碉钠,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)痘拆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門仰禽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人纺蛆,你說我怎么就攤上這事吐葵。” “怎么了桥氏?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵温峭,是天一觀的道長。 經(jīng)常有香客問我字支,道長凤藏,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任堕伪,我火速辦了婚禮揖庄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘欠雌。我一直安慰自己蹄梢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布富俄。 她就那樣靜靜地躺著检号,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蛙酪。 梳的紋絲不亂的頭發(fā)上齐苛,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機(jī)與錄音桂塞,去河邊找鬼凹蜂。 笑死,一個胖子當(dāng)著我的面吹牛阁危,可吹牛的內(nèi)容都是我干的玛痊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼狂打,長吁一口氣:“原來是場噩夢啊……” “哼擂煞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起趴乡,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤对省,失蹤者是張志新(化名)和其女友劉穎蝗拿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蒿涎,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡哀托,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了劳秋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仓手。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖玻淑,靈堂內(nèi)的尸體忽然破棺而出嗽冒,到底是詐尸還是另有隱情,我是刑警寧澤补履,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布添坊,位于F島的核電站,受9級特大地震影響干像,放射性物質(zhì)發(fā)生泄漏帅腌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一麻汰、第九天 我趴在偏房一處隱蔽的房頂上張望速客。 院中可真熱鬧,春花似錦五鲫、人聲如沸溺职。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浪耘。三九已至,卻和暖如春塑崖,著一層夾襖步出監(jiān)牢的瞬間七冲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工规婆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留澜躺,地道東北人。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓抒蚜,卻偏偏與公主長得像掘鄙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子嗡髓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容