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、演示
相关推荐
布局呆星24 分钟前
Vue Router 笔记(二):正则路由、组件通信与动态路由
前端·javascript·vue.js
rising start43 分钟前
InsightEdu - 轻量智能学习平台
javascript·axios·css3·html5·fastapi·orm·dify
qq_381338501 小时前
前端虚拟列表与无限滚动性能优化实战:从万级数据到丝滑体验
前端·javascript·html·优化
hexu_blog1 小时前
前端vue后端springboot如何实现图片格式转换
前端·javascript·vue.js
代码煮茶1 小时前
Vue3 项目规范实战 | ESLint+Prettier+Git Hooks 搭建前端代码规范体系
前端·javascript·vue.js
hexu_blog2 小时前
前端vue 后端springboot如何实现图片去水印
前端·javascript·vue.js
whuhewei2 小时前
React搜索框组件
前端·javascript·react.js
3D小将2 小时前
3D格式转换之IFC格式转换为GLTF格式技术文档
3d·solidworks模型·rhino模型·sketchup模型·igs模型
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_50:(深入理解 DOM 中的 Text 节点)
前端·javascript·microsoft·ui·html·媒体
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_51:(深入理解 XPathEvaluator 接口)
前端·javascript·ui·html·音视频