Babylonjs学习笔记(二)——创建基本材质

书接上回,这里讨论给网格添加材质!!!

准备好材质

1、创建材质球
javascript 复制代码
/**
 * 创建网格材质
 * @param scene 场景对象
 * @returns 材质对象
 */
const createGroundMaterial=(scene:Scene):StandardMaterial=>{
  const texArray:Texture[] =[]

  // 材质uv缩放
  const uvScale = 4;

  const groundMaterial = new StandardMaterial('groundMaterial',scene)

  // diffuse 漫射纹理
  const diffuseTex = new Texture('./textures/random_bricks_thick_diff_4k.jpg',scene);
  groundMaterial.diffuseTexture  = diffuseTex
  texArray.push(diffuseTex)

  // normal 法线纹理
  const normalTex = new Texture('./textures/random_bricks_thick_nor_gl_4k.jpg',scene)
  groundMaterial.bumpTexture = normalTex;
  texArray.push(normalTex)

  // AO环境遮挡
  const aoTex = new Texture('./textures/random_bricks_thick_ao_4k.jpg',scene)
  groundMaterial.ambientTexture = aoTex;
  texArray.push(aoTex)

  // spec 镜面纹理
  const speTex = new Texture('./textures/random_bricks_thick_spec_4k.jpg',scene)
  groundMaterial.specularTexture = speTex;
  texArray.push(speTex)

  texArray.forEach((tex)=>{
    tex.uScale = uvScale;
    tex.vScale = uvScale;
  })
  return groundMaterial
}



/**
 * 创建盒子网格材质
 * @param scene 场景对象
 * @returns 材质对象
 */
const createBoxMaterial = (scene:Scene):StandardMaterial=>{
  const boxMat = new StandardMaterial('boxMat',scene)
  const aoTex = new Texture('./textures/aerial_beach_02_ao_4k.jpg',scene)
  boxMat.ambientTexture = aoTex;

  const normalTex = new Texture('./textures/aerial_beach_02_nor_gl_4k.jpg',scene)
  boxMat.bumpTexture = normalTex;

  const diffuseTex = new Texture('./textures/aerial_beach_02_diff_4k.jpg',scene)
  boxMat.diffuseTexture = diffuseTex

  return boxMat
}
2、应用材质球
javascript 复制代码
  // 创建box
  box = MeshBuilder.CreateBox('box',{size:2},scene)
  // 赋予材质
  box.material = createBoxMaterial(scene)
  box.position.y= 1;

  // 相机目标
  camera.setTarget(box)

  const ground = MeshBuilder.CreateGround('ground',{width:10,height:10},scene)
  // 赋予材质
  ground.material =  createGroundMaterial(scene)
3、演示
相关推荐
李少兄3 小时前
JavaScript 数据类型完全指南
开发语言·javascript·ecmascript
shmily麻瓜小菜鸡6 小时前
JS/TS 易踩坑知识点 — 模块化与工程化类
开发语言·javascript·ecmascript
默_笙8 小时前
🍕 AI 的嘴巴装了水管(上):从"等它说完"到"边说边听"的流式输出指南
前端·javascript
暖焰核心8 小时前
继承全解——继承、默认成员函数、切片、隐藏与虚继承
java·前端·javascript
艾伦野鸽ggg9 小时前
前端异步请求的状态竞争(请求竞态)问题
前端·javascript·axios
是立不是利9 小时前
前端交互基石:深入剖析JavaScript三级联动背后的设计哲学
开发语言·前端·javascript
老刘的望远镜9 小时前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
java·开发语言·javascript
前端精髓10 小时前
React 更新 state 中的对象
javascript·react.js·ecmascript
可乐鸡翅yeah_11 小时前
hls.js 播放质量埋点实战,采集卡顿、起播、错误指标定位线上用户问题
开发语言·前端·javascript·ecmascript·音视频·m3u8·音视频在线播放
Bs_MoneyMagnet13 小时前
基于springboot+vue的宠物殡葬管理平台的设计与实现 源码+文档
java·javascript·vue.js·spring boot·后端·宠物