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、演示
相关推荐
时寒的笔记7 小时前
LF11期 day21-day22:逆向瑞数加密 欧冶案例分析(一)
javascript
深圳市机智人激光雷达7 小时前
时空解算与图优化:激光雷达 3D 建图的技术原理与实现流程
人工智能·3d·机器人·自动化·自动驾驶·激光雷达
海伯森技术7 小时前
海伯森3D线光谱共焦精密测量技术及产业化应用
大数据·人工智能·3d
Coovally AI模型快速验证7 小时前
上海 AI Lab联合发布无需人工标注的TrackRef3D:全自动3D指代分割,mIoU达38.8领跑SOTA
人工智能·3d
lbb 小魔仙7 小时前
稳定比技巧更重要:海外多地区数据采集的经验教训
开发语言·javascript·ecmascript
七77.7 小时前
【3D 场景生成】MIDI: Multi-Instance Diffusion for Single Image to 3D Scene Generation
3d·世界模型
布兰妮甜7 小时前
Vue 视图不更新?常见赋值踩坑点汇总
前端·javascript·vue.js·vue踩坑·vue视图不更新
我有满天星辰7 小时前
【Dart 语言学习教程 】第三章:函数式编程与高阶特性
开发语言·javascript·ecmascript
@zulnger7 小时前
selenium 操作浏览器
前端·javascript·selenium
爱编程的小金7 小时前
告别手写分页逻辑:usePagination 从 50 行到 3 行
javascript·vue·前端分页·alova·usepagination