threejs 加載材質(zhì)

package.json

{
  "name": "threejs-texture",
  "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",
    ]
}

webpackage.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/
        },
    },
};

index.ts

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

// 創(chuàng)建相機
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;

// 創(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);

// 創(chuàng)建mesh
const geometry = new THREE.BufferGeometry();
// 創(chuàng)建頂點
const vertices = new Float32Array( [
    -2.0, -1.5,  0,
    2.0, -1.5,  0,
    2.0,  1.5,  0,

    2.0,  1.5,  0,
   -2.0,  1.5,  0,
   -2.0, -1.5,  0
] );
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3) );

// 創(chuàng)建材質(zhì)貼圖
const uvs = new Float32Array( [
    0, 0,
    1, 0,
    1, 1,
    1, 1,
    0, 1,
    0, 0
] );
geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2) );

const loader = new THREE.TextureLoader();
const texture = loader.load('./t.png');

const material = new THREE.MeshBasicMaterial( { map: texture, transparent: true } );
console.log(material.side);
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);


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

function animate() {
    requestAnimationFrame( animate );
    renderer.render( scene, camera );
}
animate();
t.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末佛呻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌悔政,老刑警劉巖娩梨,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铣鹏,死亡現(xiàn)場離奇詭異透典,居然都是意外死亡滤奈,警方通過查閱死者的電腦和手機摆昧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜒程,“玉大人绅你,你說我怎么就攤上這事≌烟桑” “怎么了忌锯?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長领炫。 經(jīng)常有香客問我偶垮,道長,這世上最難降的妖魔是什么帝洪? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任似舵,我火速辦了婚禮,結(jié)果婚禮上葱峡,老公的妹妹穿的比我還像新娘砚哗。我一直安慰自己,他們只是感情好砰奕,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布蛛芥。 她就那樣靜靜地躺著提鸟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仅淑。 梳的紋絲不亂的頭發(fā)上称勋,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音漓糙,去河邊找鬼。 笑死烘嘱,一個胖子當(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
  • 正文 獨居荒郊野嶺守林人離奇死亡琳拨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年瞭恰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狱庇。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡惊畏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出密任,到底是詐尸還是另有隱情颜启,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布浪讳,位于F島的核電站缰盏,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏淹遵。R本人自食惡果不足惜乳规,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望合呐。 院中可真熱鬧暮的,春花似錦、人聲如沸淌实。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恨闪,卻和暖如春倘感,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背咙咽。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工老玛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钧敞。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓蜡豹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親溉苛。 傳聞我的和親對象是個殘疾皇子镜廉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

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