欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > Three.js 实现海面效果

Three.js 实现海面效果

2025/2/7 21:06:55 来源:https://blog.csdn.net/guang2586/article/details/145468552  浏览:    关键词:Three.js 实现海面效果

Three.js 实现海面效果
https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=shader&id=oceanShader
在这里插入图片描述

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { Water } from 'three/examples/jsm/objects/Water.js';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'const box = document.getElementById('box')const scene = new THREE.Scene()const camera = new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 1000000)camera.position.set(6, 3, 15)const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)const controls = new OrbitControls(camera, renderer.domElement)controls.enableDamping = trueconst waterGeometry = new THREE.PlaneGeometry(10000, 10000);const water = new Water(waterGeometry,{textureWidth: 512,textureHeight: 512,waterNormals: new THREE.TextureLoader().load(  `https://file.threehub.cn/` +  '/images/texture/waternormals.jpg', function (texture) {texture.wrapS = texture.wrapT = THREE.RepeatWrapping;}),sunDirection: new THREE.Vector3(),sunColor: 0xffffff,waterColor: 0x001e0f,distortionScale: 3.7,fog: scene.fog !== undefined}
);water.rotation.x = - Math.PI / 2;scene.add(water);// 文件地址
const urls = [0, 1, 2, 3, 4, 5].map(k => (`https://file.threehub.cn/` + 'files/sky/skyBox0/' + (k + 1) + '.png'));const textureCube = new THREE.CubeTextureLoader().load(urls);scene.background = textureCube;new GLTFLoader().load('https://z2586300277.github.io/3d-file-server/models/glb/wajueji.glb',gltf => {gltf.scene.traverse(child => {if (child.isMesh) {child.material.envMap = textureCube}})scene.add(gltf.scene)})const gui = new GUI();gui.addColor(water.material.uniforms['waterColor'], 'value').name('waterColor');gui.addColor(water.material.uniforms['sunColor'], 'value').name('sunColor');gui.add(water.material.uniforms['sunDirection'].value, 'x', - 1, 1).name('sunX');animate()function animate() {water.material.uniforms['time'].value += 1.0 / 60.0;requestAnimationFrame(animate)controls.update()renderer.render(scene, camera)}window.onresize = () => {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect = box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}/*** title: Ocean Shader* author: Elegant https://z2586300277.github.io/
*/

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com