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、演示
相关推荐
雨雨雨雨雨别下啦17 小时前
Vue案例——面经
前端·javascript·vue.js
myNameGL18 小时前
ArkTs核心语法
前端·javascript·vue.js
浏览器API调用工程师_Taylor19 小时前
web逆向之小红书无水印图片提取工具
前端·javascript·逆向
yuki_uix19 小时前
性能指标与优化:从 Core Web Vitals 到实战
前端·javascript
简单不容易19 小时前
vue一次解决监听H5软键盘弹出和收起的兼容问题
javascript·vue.js
滕青山19 小时前
在线图片压缩工具核心JS实现
前端·javascript·vue.js
进击的尘埃19 小时前
低代码组件通信:`EventBus`和响应式数据流,到底该选哪个
javascript
angerdream19 小时前
https://editor.csdn.net/md/?articleId=159120195
javascript·vue.js
敲敲敲敲暴你脑袋19 小时前
穷鬼快乐AI工具Ollama
javascript·人工智能·ollama
WayneX19 小时前
Vue 3 + TypeScript + Vite 组件库搭建,自助式生成相应组件文档
前端·javascript·vue.js