babylonJs 水材质
babylonjs 3.0(及以上提供了水材质),我们可以通过代码实现特别好看的水材质.
- 需要单独导入特殊材质文件,materials.js(最后会提供源代码)
<script src="./babylon.js"></script>
<script src="./babylonjs.materials.js"></script>
- 创建基础场景
let engine = new BABYLON.Engine(canvas, true)
let scene = new BABYLON.Scene(engine)
let camera = new BABYLON.ArcRotateCamera('ArcRotateCamera', 0, 1, 10, new BABYLON.Vector3(0, 0, 0), scene)
camera.setTarget(BABYLON.Vector3.Zero())
camera.attachControl(canvas, false)
camera.lowerRadiusLimit = 10
let light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), scene)
let ground = BABYLON.Mesh.CreateGround('ground1', 6, 6, 2, scene)
engine.runRenderLoop(function () {
scene.render()
})
window.addEventListener("resize", function () {
engine.resize()
})
- 生成一个ground,作为水底
let ground = BABYLON.Mesh.CreateGround('ground1', 20, 20, 2, scene)
let groundMaterial=new BABYLON.StandardMaterial("groundMaterial",scene)
groundMaterial.diffuseTexture=new BABYLON.Texture("1.jpg",scene)
ground.material=groundMaterial
- 生成水面片,并创建水材质
let water=BABYLON.Mesh.CreateGround('ground1', 20, 20, 2, scene)
//生成水
let waterialMaterial=new BABYLON.WaterMaterial("WaterMaterial",scene)
water.material=waterialMaterial
- 水材质的样子有点奇怪,接下来我们来调整它的属性
//设置水面的纹理贴图
waterialMaterial.bumpTexture = new BABYLON.Texture("waterbump.png", scene)
//设置风力,决定了水波运动速度
waterialMaterial.windForce = 30
//设置风向x
waterialMaterial.windDirection.x=0.2
//设置风向y
waterialMaterial.windDirection.y=0.3
//设置水的波浪高度
waterialMaterial.waveHeight =1
//设置水纹理高度
waterialMaterial.bumpHeight = 1
//设置波浪的长度
waterialMaterial.waveLength = 1
//设置波浪运行速度
waterialMaterial.waveSpeed=30
//设置水的颜色
waterialMaterial.waterColor=new BABYLON.Color3(0,0,0.5)
waterialMaterial.colorBlendFactor=0
//增加地板到水材质中,才能正常显示水
waterialMaterial.addToRenderList(ground)
waterialMaterial.material = water
-
展示效果
-
全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>template</title>
<style>
* {
padding: 0;
margin: 0;
}
body {
overflow: hidden;
}
canvas {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
<script src="./babylon.js"></script>
<script src="./babylonjs.materials.js"></script>
<script>
let engine = new BABYLON.Engine(canvas, true)
let scene = new BABYLON.Scene(engine)
let camera = new BABYLON.ArcRotateCamera('ArcRotateCamera', 0, 1, 500, new BABYLON.Vector3(0, 0, 0), scene)
camera.setTarget(BABYLON.Vector3.Zero())
camera.attachControl(canvas, false)
camera.lowerRadiusLimit = 10
let light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), scene)
engine.runRenderLoop(function () {
scene.render()
})
window.addEventListener("resize", function () {
engine.resize()
})
//生成水底
let ground = BABYLON.Mesh.CreateGround('ground1', 512, 512, 32, scene)
//生成水底材质
let groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene)
groundMaterial.diffuseTexture = new BABYLON.Texture("1.jpg", scene)
ground.material = groundMaterial
//生成水面片
let water = BABYLON.Mesh.CreateGround('ground1', 512, 512, 32, scene)
//生成水
let waterialMaterial = new BABYLON.WaterMaterial("WaterMaterial", scene)
water.material = waterialMaterial
//设置水面的纹理贴图
waterialMaterial.bumpTexture = new BABYLON.Texture("waterbump.png", scene)
//设置风力,决定了水波运动速度
waterialMaterial.windForce = 30
//设置风向x
waterialMaterial.windDirection.x=0.2
//设置风向y
waterialMaterial.windDirection.y=0.3
//设置水的波浪高度
waterialMaterial.waveHeight =1
//设置水纹理高度
waterialMaterial.bumpHeight = 1
//设置波浪的长度
waterialMaterial.waveLength = 1
//设置波浪运行速度
waterialMaterial.waveSpeed=30
//设置水的颜色
waterialMaterial.waterColor=new BABYLON.Color3(0,0,0.5)
waterialMaterial.colorBlendFactor=0
//增加地板到水材质中,才能正常显示水
waterialMaterial.addToRenderList(ground)
waterialMaterial.material = water
</script>
码云地址 https://gitee.com/wyYym/babylon
联系方式 QQ:1684430131