1、核电站在水上所以要求要做这个;怕被告所以实例不方便展示;效果如下图
闲言少叙代码如下
var createScene = function (engine) {
var scene = new BABYLON.Scene(engine);
// Camera
var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2.5, 50, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
// Light
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
// Skybox
var skybox = BABYLON.Mesh.CreateBox("skyBox", 5000.0, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("https://minio.cnbabylon.com/public/Assets/TropicalSunnyDay", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
// Water material
var waterMaterial = new BABYLON.WaterMaterial("waterMaterial", scene, new BABYLON.Vector2(512, 512));
waterMaterial.bumpTexture = new BABYLON.Texture("https://minio.cnbabylon.com/public/Assets/waterbump.png", scene);
waterMaterial.windForce = -10;
waterMaterial.waveHeight = 0.5;
waterMaterial.bumpHeight = 0.1;
waterMaterial.waveLength = 0.1;
waterMaterial.waveSpeed = 50.0;
waterMaterial.colorBlendFactor = 0;
waterMaterial.windDirection = new BABYLON.Vector2(1, 1);
waterMaterial.colorBlendFactor = 0;
// Ground
var groundTexture = new BABYLON.Texture("https://minio.cnbabylon.com/public/Assets/sand.jpg", scene);
groundTexture.vScale = groundTexture.uScale = 4.0;
var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene);
groundMaterial.diffuseTexture = groundTexture;
var ground = BABYLON.Mesh.CreateGround("ground", 512, 512, 32, scene, false);
ground.position.y = -2;
ground.material = groundMaterial;
// Water mesh
var waterMesh = BABYLON.Mesh.CreateGround("waterMesh", 512, 512, 32, scene, false);
waterMesh.material = waterMaterial;
// Sphere
var sphereMaterial = new BABYLON.StandardMaterial("sphereMaterial", scene);
sphereMaterial.diffuseTexture = new BABYLON.Texture("https://minio.cnbabylon.com/public/Assets/wood.jpg", scene);
var sphere = BABYLON.Mesh.CreateSphere("sphere", 16, 10, scene);
sphere.position.y = 20;
sphere.material = sphereMaterial;
// Configure water material
waterMaterial.addToRenderList(ground);
waterMaterial.addToRenderList(skybox);
waterMaterial.addToRenderList(sphere);
BABYLON.SceneLoader.LoadAssetContainer("https://minio.cnbabylon.com/public/Assets/", "fish.glb", scene, function (container) {
// Scale and position the loaded model (First mesh loaded from gltf is the root node)
container.meshes[0].scaling.scaleInPlace(1)
container.meshes[0].position.z = 2
container.meshes[0].position.y = -8
for (var index = 0; index < container.meshes.length; index++) {
waterMaterial.addToRenderList(container.meshes[index]);
}
// Add loaded file to the scene
container.addAllToScene();
});
// RAY CAST TO FIND WATER HEIGHT
//var angle = 0;
let i = 0;
scene.registerBeforeRender(function() {
let time = waterMaterial._lastTime / 100000;
let x = sphere.position.x;
let z = sphere.position.z;
sphere.position.y = Math.abs((Math.sin(((x / 0.05) + time * waterMaterial.waveSpeed)) * waterMaterial.waveHeight * waterMaterial.windDirection.x * 5.0) + (Math.cos(((z / 0.05) + time * waterMaterial.waveSpeed)) * waterMaterial.waveHeight * waterMaterial.windDirection.y * 5.0));
});
return scene;
};