three.js - Materials

  • Materials are used to put a color on each visible pixel of the geomertries
  • 著色器:the algorithms are written in programs called shaders
  • 基于一個基本場景(scene遥倦、camera炉奴、controls)創(chuàng)建以下元素:
// 分別創(chuàng)建3個Mesh囱晴,球、平面侨赡、環(huán)脚粟,共用1個Material

/*
  * Objects
*/
const material = new THREE.MeshBasicMaterial()

// 球體
const sphere = new THREE.Mesh(
  new THREE.SphereGeometry(0.5, 16, 16),
  material
)
sphere.position.x = -1.5

// 平面
const plane = new THREE.Mesh(
  new THREE.PlaneGeometry(1, 1),
  material
)

// 環(huán)
const torus = new THREE.Mesh(
  new THREE.TorusGeometry(0.3, 0.2, 16, 32),
  material
)
torus.position.x = 1.5

scene.add(sphere, plane, torus)
// rotate the objects in the tick function

/*
  *Animations
*/
const clock = new THREE.Clock()
const tick = () => {
  let elapsedTime = clock.getElapsedTime()

  // update object
  sphere.rotation.y = 0.1 * elapsedTime
  plane.rotation.y = 0.1 * elapsedTime
  torus.rotation.y = 0.1 * elapsedTime

  sphere.rotation.x = 0.15 * elapsedTime
  plane.rotation.x = 0.15 * elapsedTime
  torus.rotation.x = 0.15 * elapsedTime

  controls.update()
  renderer.render(scene, camera)
  window.requestAnimationFrame(tick)
}
tick()
  • materials properties
    /*
      * Textures
    */
    const textureLoader = new THREE.TextureLoader()
    
    const doorColorTexture = textureLoader.load('')
    const doorAlphaTexture = textureLoader.load('')
    const doorAmbientOcclusionTexture = textureLoader.load('')
    const doorHeightTexture = textureLoader.load('')
    const doorNormalTexture = textureLoader.load('')
    const doorMetalnessTexture = textureLoader.load('')
    const doorRoughnessTexture = textureLoader.load('')
    const matCapTexture = textureLoader.load('')
    
    // Most of properties can be written like this
    
    // 寫法1:
    const material = new THREE.MeshBasicMaterial({
      map: doorColorTexture, // 紋理貼圖
      wireframe: true, // show the triangles that compose the geometry
    })
    
    // 寫法2:
    const material = new THREE.MeshBasicMaterial()
    material.map = doorColorTexture
    material.wireframe = true
    
    // Color
    
    // 寫法1:
    const material = new THREE.MeshBasicMaterial({
      color: 'pink'
    })
    
    // 寫法2:
    const material = new THREE.MeshBasicMaterial()
    console.log(material.color); // 打印出一個Color類
    material.color.set('#ff0000')
    
    // 寫法3:
    const material = new THREE.MeshBasicMaterial()
    material.color = new THREE.Color(0xff0000)
    
    // Opacity
    // 使用opacity屬性時缭保,需要同時設置 transparent為true
    // 使用alphaMap屬性時,也需要搭配 transparent = true
    
    const material = new THREE.MeshBasicMaterial({
      color: 'pink'
      transparent: true, 
      opacity: 0.5
    })
    
    // Side - decide whitch side of a face is visible
    
    const material = new THREE.MeshBasicMaterial({
      color: 'pink',
      side: THREE.FrontSide, // 默認
      // side: THREE.BackSide,
      // side: THREE.DoubleSide,
    })
    
    • MeshNormalMaterial - shares common properties width MeshBasicMaterial like wireframe捉片、transparnt平痰、opacity and side,通澄槿遥可用于調試法線宗雇,使用法線向量計算物體表面的顏色
    const material = new THREE.MeshNormalMaterial({
      flatShading: true // 使用平面著色進行渲染
    })
    
    const material = new THREE.MeshMatCapMaterial({
      matcap: matCapTexture
    })
    
    • MeshDepthMaterial - simply color the geometry in white if it's close to the near and in black if it's close to the far value of the camera
    • MeshLamberMaterial - have new properties related to lights
    const material = new THREE.MeshLambertMaterial()
    
    /*
      * Lights
    */
    // 環(huán)境光
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
    scene.add(ambientLight)
    
    // 點光源
    const pointLight = new THREE.PointLight(0xffffff, 0.5)
    pointLight.position.set(2, 3, 4)
    scene.add(pointLight)
    
    • MeshStandardMaterial - used physically based rendering principles(PBR),傾向于模仿現(xiàn)實條件
  • material屬性中的 map 與 texture
    • map
      • color
      • albedo 反射率
      • applied on the geometry
    • alphaMap
      • grayscale image 灰度圖
      • white visible
      • black not visible
    • displacementMap
      • height
      • grayscale image 灰度圖
      • move the vertices to create some relief(高低莹规、凹凸)
      • need enough subdivision(細分)
    • normalMap
      • normal
      • add details, doesn't need subdivision
      • the vertices won't move
      • lure the light about the face orientation 將光線引向目標方向
      • better performances than adding a height texture with a lot of subdivision
    • aoMap
      • occlusion 環(huán)境吸收
      • grayscale image 灰度圖
      • add fake shadows in crevices 在裂縫中添加假陰影
      • not physically accurate 不是自然地赔蒲,不準確
      • helps to create contrast and see details
    • metalnessMap
      • grayscale image 灰度圖
      • white is metallic 金屬的,black is non-metallic 非金屬的
      • mostly for reflection 光反射
    • roughnessMap
      • grayscale image 灰度圖
      • mostly for light dissipation 消散良漱、損耗
      • white is rough 粗糙舞虱,black is smooth 光滑
      • in duo with metalness 與金屬質感相得益彰
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市母市,隨后出現(xiàn)的幾起案子砾嫉,更是在濱河造成了極大的恐慌,老刑警劉巖窒篱,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焕刮,死亡現(xiàn)場離奇詭異舶沿,居然都是意外死亡,警方通過查閱死者的電腦和手機配并,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門括荡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人溉旋,你說我怎么就攤上這事畸冲。” “怎么了观腊?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵邑闲,是天一觀的道長。 經(jīng)常有香客問我梧油,道長苫耸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任儡陨,我火速辦了婚禮褪子,結果婚禮上,老公的妹妹穿的比我還像新娘骗村。我一直安慰自己嫌褪,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布胚股。 她就那樣靜靜地躺著笼痛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪琅拌。 梳的紋絲不亂的頭發(fā)上晃痴,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音财忽,去河邊找鬼倘核。 笑死,一個胖子當著我的面吹牛即彪,可吹牛的內容都是我干的紧唱。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼隶校,長吁一口氣:“原來是場噩夢啊……” “哼漏益!你這毒婦竟也來了?” 一聲冷哼從身側響起深胳,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绰疤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后舞终,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體轻庆,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡癣猾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了余爆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纷宇。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蛾方,靈堂內的尸體忽然破棺而出像捶,到底是詐尸還是另有隱情,我是刑警寧澤桩砰,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布拓春,位于F島的核電站,受9級特大地震影響亚隅,放射性物質發(fā)生泄漏硼莽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一枢步、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧渐尿,春花似錦醉途、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至凉夯,卻和暖如春货葬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背劲够。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工震桶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人征绎。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓蹲姐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親人柿。 傳聞我的和親對象是個殘疾皇子柴墩,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內容

  • Three.js 1.Three.js 介紹 OpenGL(英語:Open Graphics Library,譯名...
    GuitarHusky閱讀 2,486評論 0 1
  • Threejs 為什么凫岖? webGL太難用江咳,太復雜! 但是現(xiàn)代瀏覽器都支持 WebGL 這樣我們就不必使用 Fla...
    強某某閱讀 6,046評論 1 21
  • 準備工作: 1哥放、three.js https://threejs.org/build/three.js 2歼指、搭建項...
    月_關閱讀 5,572評論 0 1
  • 簡介 Three.js是基于原生WebGL封裝運行的三維引擎爹土。可與各種前端技術結合使用东臀。 github鏈接:Git...
    田苗苗_7785閱讀 2,037評論 0 0
  • 學習目標 熟悉并掌握Three.js中的基本概念着饥,主要包括場景,攝像機惰赋,燈光宰掉,渲染器,物體對象赁濒,幾何體轨奄,材質,動畫...
    田苗苗_7785閱讀 1,788評論 2 8