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


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

    准备好材质

    1、创建材质球
    1. /**
    2. * 创建网格材质
    3. * @param scene 场景对象
    4. * @returns 材质对象
    5. */
    6. const createGroundMaterial=(scene:Scene):StandardMaterial=>{
    7. const texArray:Texture[] =[]
    8. // 材质uv缩放
    9. const uvScale = 4;
    10. const groundMaterial = new StandardMaterial('groundMaterial',scene)
    11. // diffuse 漫射纹理
    12. const diffuseTex = new Texture('./textures/random_bricks_thick_diff_4k.jpg',scene);
    13. groundMaterial.diffuseTexture = diffuseTex
    14. texArray.push(diffuseTex)
    15. // normal 法线纹理
    16. const normalTex = new Texture('./textures/random_bricks_thick_nor_gl_4k.jpg',scene)
    17. groundMaterial.bumpTexture = normalTex;
    18. texArray.push(normalTex)
    19. // AO环境遮挡
    20. const aoTex = new Texture('./textures/random_bricks_thick_ao_4k.jpg',scene)
    21. groundMaterial.ambientTexture = aoTex;
    22. texArray.push(aoTex)
    23. // spec 镜面纹理
    24. const speTex = new Texture('./textures/random_bricks_thick_spec_4k.jpg',scene)
    25. groundMaterial.specularTexture = speTex;
    26. texArray.push(speTex)
    27. texArray.forEach((tex)=>{
    28. tex.uScale = uvScale;
    29. tex.vScale = uvScale;
    30. })
    31. return groundMaterial
    32. }
    33. /**
    34. * 创建盒子网格材质
    35. * @param scene 场景对象
    36. * @returns 材质对象
    37. */
    38. const createBoxMaterial = (scene:Scene):StandardMaterial=>{
    39. const boxMat = new StandardMaterial('boxMat',scene)
    40. const aoTex = new Texture('./textures/aerial_beach_02_ao_4k.jpg',scene)
    41. boxMat.ambientTexture = aoTex;
    42. const normalTex = new Texture('./textures/aerial_beach_02_nor_gl_4k.jpg',scene)
    43. boxMat.bumpTexture = normalTex;
    44. const diffuseTex = new Texture('./textures/aerial_beach_02_diff_4k.jpg',scene)
    45. boxMat.diffuseTexture = diffuseTex
    46. return boxMat
    47. }
    2、应用材质球
    1. // 创建box
    2. box = MeshBuilder.CreateBox('box',{size:2},scene)
    3. // 赋予材质
    4. box.material = createBoxMaterial(scene)
    5. box.position.y= 1;
    6. // 相机目标
    7. camera.setTarget(box)
    8. const ground = MeshBuilder.CreateGround('ground',{width:10,height:10},scene)
    9. // 赋予材质
    10. ground.material = createGroundMaterial(scene)
    3、演示

  • 相关阅读:
    5、K8s控制器- Deployment
    AM@第二类换元法积分
    【深度优先搜索-中等】1034. 边界着色
    No servers available for service: renren…。 Gateway 网关报503错误 ,已解决
    vue-admin相关问题记录
    我的创作纪念日
    【git 介绍】AhuntSun
    矩阵论复习提纲
    java毕业设计n音乐剧网站mybatis+源码+调试部署+系统+数据库+lw
    JavaScript(JS)的简单了解
  • 原文地址:https://blog.csdn.net/weixin_41718879/article/details/133930930