Hello engineers!
I’m trying to create a low-poly water surface effect.
As I want to make it compatible with version R155, I’m currently attempting to modify this code from version R106.
This is the version I referenced, written by someone else in r106.
.
.
.
.
In the version I’m trying
Although I can see the water surface, it doesn’t behave as I expected it to. I’d appreciate it if you could take a look and see if I made any mistakes in my modifications.
However, up until now, I haven’t encountered any error messages. Despite the fact that the water surface is visible, it doesn’t behave as I anticipated. Could you please take a look and see if I made any mistakes in my modifications?
Furthermore, I’ve transformed the code into ES6 syntax, but since I’m not very familiar with ES5, I can’t rule out the possibility that I misunderstood something from the start.
my:
https://gotoo.co/demo/elizabeth/gotoo5th3D/lowPolyWater/
code:
import * as THREE from '../js/three/build/three.module.js';
import * as BufferGeometryUtils from '../js/three/examples/jsm/utils/BufferGeometryUtilsLocal.js';
class ThreeScene {
constructor() {
this.scene = new THREE.Scene();
this.scene.fog = new THREE.Fog(0xf7d9aa, 100, 950);
// this.Ocean = new Ocean();
this.seaGeom = new THREE.PlaneGeometry(500, 500, 20, 20);
this.waves = [];/* 先製造一個海浪的數據 */
this.seaMesh;
this.height = window.innerHeight;
this.width = window.innerWidth;
this.aspectRatio = this.width / this.height;
this.fieldOfView = 60;
this.nearPlane = 1;
this.farPlane = 10000;
this.camera = new THREE.PerspectiveCamera(
this.fieldOfView,
this.aspectRatio,
this.nearPlane,
this.farPlane
);
this.camera.position.x = 0;
this.camera.position.z = 250;
this.camera.position.y = 250;
this.renderer = new THREE.WebGLRenderer({ alpha: true });
this.renderer.setSize(this.WIDTH, this.HEIGHT);
this.renderer.shadowMap.enabled = true;
this.container = document.getElementById('world');
this.container.appendChild(this.renderer.domElement);
}
init() {
window.addEventListener('resize', this.onWindowResize(this), false);
this.creatWaves();
this.createLights();
this.createOcean();
}
onWindowResize(that) {
window.addEventListener('resize', function (e) {
that.camera.aspect = window.innerWidth / window.innerHeight;
that.camera.updateProjectionMatrix();
that.renderer.setSize(window.innerWidth, window.innerHeight);
that.windowWidth = window.innerWidth;
that.windowHeight = window.innerHeight;
});
}
createLights() {
this.hemisphereLight = new THREE.HemisphereLight(0xaaaaaa, 0x000000, 0.9);
this.shadowLight = new THREE.DirectionalLight(0xffffff, 0.9);
this.shadowLight.position.set(-150, 350, 350);
this.shadowLight.castShadow = true;
this.shadowLight.shadow.camera.left = -400;
this.shadowLight.shadow.camera.right = 400;
this.shadowLight.shadow.camera.top = 400;
this.shadowLight.shadow.camera.bottom = -400;
this.shadowLight.shadow.camera.near = 1;
this.shadowLight.shadow.camera.far = 1000;
this.shadowLight.shadow.mapSize.width = 2048;
this.shadowLight.shadow.mapSize.height = 2048;
this.scene.add(this.hemisphereLight);
this.scene.add(this.shadowLight);
}
creatWaves() {
this.seaGeom.rotateX(-Math.PI / 2);
BufferGeometryUtils.mergeVertices(this.seaGeom);
this.seaGeom.vertices = this.seaGeom.attributes.position.array;
let l = this.seaGeom.vertices.length;
for (let i = 0; i < l; i += 3) {
const v1 = this.seaGeom.vertices[i];
const v2 = this.seaGeom.vertices[i + 1];
const v3 = this.seaGeom.vertices[i + 2];
this.waves.push({
x: v1,
y: v2,
z: v3,
ang: Math.random() * Math.PI * 2,
speed: 0.016 + Math.random() * 0.032,
});
}
let mat = new THREE.MeshPhongMaterial({
transparent: true,
color: 0x68c3c0,
// flatShading: THREE.FlatShading //??
});
this.seaMesh = new THREE.Mesh(this.seaGeom, mat);//生成海洋元素
this.seaMesh.receiveShadow = true;
// console.log(this.waves);
}
moveWaves() {
const verts = this.seaMesh.geometry.vertices;
const l = verts.length / 3;// i < l
for (let i = 0; i < l; i += 3) {
const vprops = this.waves[i];
let v1 = verts[i];
let v2 = verts[i + 1];
v1 = vprops.x + Math.cos(vprops.ang);
v2 = vprops.y + Math.sin(vprops.ang) * 2;
vprops.ang += vprops.speed;
}
this.seaMesh.geometry.verticesNeedUpdate = true;
}
createOcean() {
this.seaMesh.position.y = 200;
this.seaMesh.position.z = -35;
this.scene.add(this.seaMesh);
}
}
window.addEventListener('load', function () {
const app = new ThreeScene();
app.init();
animate();
function animate() {
requestAnimationFrame(animate);
app.moveWaves();
app.renderer.render(app.scene, app.camera);
}
});