webgl_gpgpu_water
对应 three.js 示例地址 。
仅需关注 init
函数的内容,其他部分都是示例小程序所使用的描述配置。
js
import * as THREE from "three";
import { GPUComputationRenderer } from "three/examples/jsm/misc/GPUComputationRenderer.js";
import { SimplexNoise } from "three/examples/jsm/math/SimplexNoise.js";
/** @type {import("@minisheeep/mp-three-examples").OfficialExampleInfo} */
const exampleInfo = {
name: "webgl_gpgpu_water",
useLoaders: [],
info: [
[
{
tag: "a",
link: "https://threejs.org",
content: "three.js"
},
{
tag: "text",
content: "-"
},
{
tag: "text",
content: "webgl gpgpu water"
}
],
[
{
tag: "text",
content: "Move mouse to disturb water."
},
{
tag: "text",
content: "'W' key toggles wireframe."
}
]
],
init: ({ window, canvas, GUI, Stats, needToDispose, useFrame }) => {
const WIDTH = 128;
const BOUNDS = 512;
const BOUNDS_HALF = BOUNDS * 0.5;
let stats;
let camera, scene, renderer;
let mouseMoved = false;
const mouseCoords = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let waterMesh;
let meshRay;
let gpuCompute;
let heightmapVariable;
let waterUniforms;
let smoothShader;
let readWaterLevelShader;
let readWaterLevelRenderTarget;
let readWaterLevelImage;
const waterNormal = new THREE.Vector3();
const NUM_SPHERES = 5;
const spheres = [];
let spheresEnabled = true;
const simplex = new SimplexNoise();
init();
function init() {
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3e3);
camera.position.set(0, 200, 350);
camera.lookAt(0, 0, 0);
scene = new THREE.Scene();
const sun = new THREE.DirectionalLight(16777215, 3);
sun.position.set(300, 400, 175);
scene.add(sun);
const sun2 = new THREE.DirectionalLight(4235328, 2);
sun2.position.set(-100, 350, -200);
scene.add(sun2);
renderer = new THREE.WebGLRenderer({ canvas });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setAnimationLoop(animate);
stats = new Stats(renderer);
canvas.addEventListener("pointermove", onPointerMove);
window.addEventListener("resize", onWindowResize);
const gui = new GUI();
const effectController = {
mouseSize: 20,
viscosity: 0.98,
spheresEnabled,
wireframe: false
};
const valuesChanger = function() {
heightmapVariable.material.uniforms["mouseSize"].value = effectController.mouseSize;
heightmapVariable.material.uniforms["viscosityConstant"].value = effectController.viscosity;
spheresEnabled = effectController.spheresEnabled;
for (let i = 0; i < NUM_SPHERES; i++) {
if (spheres[i]) {
spheres[i].visible = spheresEnabled;
}
}
};
gui.add(effectController, "mouseSize", 1, 100, 1).onChange(valuesChanger);
gui.add(effectController, "viscosity", 0.9, 0.999, 1e-3).onChange(valuesChanger);
gui.add(effectController, "spheresEnabled").onChange(valuesChanger);
gui.add(effectController, "wireframe").onChange((value) => {
waterMesh.material.wireframe = value;
waterMesh.material.needsUpdate = true;
});
const buttonSmooth = {
smoothWater: function() {
smoothWater();
}
};
gui.add(buttonSmooth, "smoothWater");
initWater();
createSpheres();
valuesChanger();
needToDispose(renderer, scene);
}
function initWater() {
const materialColor = 16576;
const geometry = new THREE.PlaneGeometry(BOUNDS, BOUNDS, WIDTH - 1, WIDTH - 1);
const material = new THREE.ShaderMaterial({
uniforms: THREE.UniformsUtils.merge([
THREE.ShaderLib["phong"].uniforms,
{
heightmap: { value: null }
}
]),
vertexShader: `
uniform sampler2D heightmap;
#define PHONG
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <uv_pars_vertex>
#include <displacementmap_pars_vertex>
#include <envmap_pars_vertex>
#include <color_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
vec2 cellSize = vec2( 1.0 / WIDTH, 1.0 / WIDTH );
#include <uv_vertex>
#include <color_vertex>
// # include <beginnormal_vertex>
// Compute normal from heightmap
vec3 objectNormal = vec3(
( texture2D( heightmap, uv + vec2( - cellSize.x, 0 ) ).x - texture2D( heightmap, uv + vec2( cellSize.x, 0 ) ).x ) * WIDTH / BOUNDS,
( texture2D( heightmap, uv + vec2( 0, - cellSize.y ) ).x - texture2D( heightmap, uv + vec2( 0, cellSize.y ) ).x ) * WIDTH / BOUNDS,
1.0 );
//<beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinbase_vertex>
#include <skinnormal_vertex>
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
//# include <begin_vertex>
float heightValue = texture2D( heightmap, uv ).x;
vec3 transformed = vec3( position.x, position.y, heightValue );
//<begin_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <displacementmap_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
vViewPosition = - mvPosition.xyz;
#include <worldpos_vertex>
#include <envmap_vertex>
#include <shadowmap_vertex>
}
`,
fragmentShader: THREE.ShaderChunk["meshphong_frag"]
});
material.lights = true;
material.uniforms["diffuse"].value = new THREE.Color(materialColor);
material.uniforms["specular"].value = new THREE.Color(1118481);
material.uniforms["shininess"].value = Math.max(50, 1e-4);
material.uniforms["opacity"].value = material.opacity;
material.defines.WIDTH = WIDTH.toFixed(1);
material.defines.BOUNDS = BOUNDS.toFixed(1);
waterUniforms = material.uniforms;
waterMesh = new THREE.Mesh(geometry, material);
waterMesh.rotation.x = -Math.PI / 2;
waterMesh.matrixAutoUpdate = false;
waterMesh.updateMatrix();
scene.add(waterMesh);
const geometryRay = new THREE.PlaneGeometry(BOUNDS, BOUNDS, 1, 1);
meshRay = new THREE.Mesh(
geometryRay,
new THREE.MeshBasicMaterial({ color: 16777215, visible: false })
);
meshRay.rotation.x = -Math.PI / 2;
meshRay.matrixAutoUpdate = false;
meshRay.updateMatrix();
scene.add(meshRay);
gpuCompute = new GPUComputationRenderer(WIDTH, WIDTH, renderer);
const heightmap0 = gpuCompute.createTexture();
fillTexture(heightmap0);
heightmapVariable = gpuCompute.addVariable(
"heightmap",
`
#include <common>
uniform vec2 mousePos;
uniform float mouseSize;
uniform float viscosityConstant;
uniform float heightCompensation;
void main() {
vec2 cellSize = 1.0 / resolution.xy;
vec2 uv = gl_FragCoord.xy * cellSize;
// heightmapValue.x == height from previous frame
// heightmapValue.y == height from penultimate frame
// heightmapValue.z, heightmapValue.w not used
vec4 heightmapValue = texture2D( heightmap, uv );
// Get neighbours
vec4 north = texture2D( heightmap, uv + vec2( 0.0, cellSize.y ) );
vec4 south = texture2D( heightmap, uv + vec2( 0.0, - cellSize.y ) );
vec4 east = texture2D( heightmap, uv + vec2( cellSize.x, 0.0 ) );
vec4 west = texture2D( heightmap, uv + vec2( - cellSize.x, 0.0 ) );
// https://web.archive.org/web/20080618181901/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
float newHeight = ( ( north.x + south.x + east.x + west.x ) * 0.5 - heightmapValue.y ) * viscosityConstant;
// Mouse influence
float mousePhase = clamp( length( ( uv - vec2( 0.5 ) ) * BOUNDS - vec2( mousePos.x, - mousePos.y ) ) * PI / mouseSize, 0.0, PI );
newHeight += ( cos( mousePhase ) + 1.0 ) * 0.28;
heightmapValue.y = heightmapValue.x;
heightmapValue.x = newHeight;
gl_FragColor = heightmapValue;
}
`,
heightmap0
);
gpuCompute.setVariableDependencies(heightmapVariable, [heightmapVariable]);
heightmapVariable.material.uniforms["mousePos"] = { value: new THREE.Vector2(1e4, 1e4) };
heightmapVariable.material.uniforms["mouseSize"] = { value: 20 };
heightmapVariable.material.uniforms["viscosityConstant"] = { value: 0.98 };
heightmapVariable.material.uniforms["heightCompensation"] = { value: 0 };
heightmapVariable.material.defines.BOUNDS = BOUNDS.toFixed(1);
const error = gpuCompute.init();
if (error !== null) {
console.error(error);
}
smoothShader = gpuCompute.createShaderMaterial(
`
uniform sampler2D smoothTexture;
void main() {
vec2 cellSize = 1.0 / resolution.xy;
vec2 uv = gl_FragCoord.xy * cellSize;
// Computes the mean of texel and 4 neighbours
vec4 textureValue = texture2D( smoothTexture, uv );
textureValue += texture2D( smoothTexture, uv + vec2( 0.0, cellSize.y ) );
textureValue += texture2D( smoothTexture, uv + vec2( 0.0, - cellSize.y ) );
textureValue += texture2D( smoothTexture, uv + vec2( cellSize.x, 0.0 ) );
textureValue += texture2D( smoothTexture, uv + vec2( - cellSize.x, 0.0 ) );
textureValue /= 5.0;
gl_FragColor = textureValue;
}
`,
{ smoothTexture: { value: null } }
);
readWaterLevelShader = gpuCompute.createShaderMaterial(
`
uniform vec2 point1;
uniform sampler2D levelTexture;
// Integer to float conversion from https://stackoverflow.com/questions/17981163/webgl-read-pixels-from-floating-point-render-target
float shift_right( float v, float amt ) {
v = floor( v ) + 0.5;
return floor( v / exp2( amt ) );
}
float shift_left( float v, float amt ) {
return floor( v * exp2( amt ) + 0.5 );
}
float mask_last( float v, float bits ) {
return mod( v, shift_left( 1.0, bits ) );
}
float extract_bits( float num, float from, float to ) {
from = floor( from + 0.5 ); to = floor( to + 0.5 );
return mask_last( shift_right( num, from ), to - from );
}
vec4 encode_float( float val ) {
if ( val == 0.0 ) return vec4( 0, 0, 0, 0 );
float sign = val > 0.0 ? 0.0 : 1.0;
val = abs( val );
float exponent = floor( log2( val ) );
float biased_exponent = exponent + 127.0;
float fraction = ( ( val / exp2( exponent ) ) - 1.0 ) * 8388608.0;
float t = biased_exponent / 2.0;
float last_bit_of_biased_exponent = fract( t ) * 2.0;
float remaining_bits_of_biased_exponent = floor( t );
float byte4 = extract_bits( fraction, 0.0, 8.0 ) / 255.0;
float byte3 = extract_bits( fraction, 8.0, 16.0 ) / 255.0;
float byte2 = ( last_bit_of_biased_exponent * 128.0 + extract_bits( fraction, 16.0, 23.0 ) ) / 255.0;
float byte1 = ( sign * 128.0 + remaining_bits_of_biased_exponent ) / 255.0;
return vec4( byte4, byte3, byte2, byte1 );
}
void main() {
vec2 cellSize = 1.0 / resolution.xy;
float waterLevel = texture2D( levelTexture, point1 ).x;
vec2 normal = vec2(
( texture2D( levelTexture, point1 + vec2( - cellSize.x, 0 ) ).x - texture2D( levelTexture, point1 + vec2( cellSize.x, 0 ) ).x ) * WIDTH / BOUNDS,
( texture2D( levelTexture, point1 + vec2( 0, - cellSize.y ) ).x - texture2D( levelTexture, point1 + vec2( 0, cellSize.y ) ).x ) * WIDTH / BOUNDS );
if ( gl_FragCoord.x < 1.5 ) {
gl_FragColor = encode_float( waterLevel );
} else if ( gl_FragCoord.x < 2.5 ) {
gl_FragColor = encode_float( normal.x );
} else if ( gl_FragCoord.x < 3.5 ) {
gl_FragColor = encode_float( normal.y );
} else {
gl_FragColor = encode_float( 0.0 );
}
}
`,
{
point1: { value: new THREE.Vector2() },
levelTexture: { value: null }
}
);
readWaterLevelShader.defines.WIDTH = WIDTH.toFixed(1);
readWaterLevelShader.defines.BOUNDS = BOUNDS.toFixed(1);
readWaterLevelImage = new Uint8Array(4 * 1 * 4);
readWaterLevelRenderTarget = new THREE.WebGLRenderTarget(4, 1, {
wrapS: THREE.ClampToEdgeWrapping,
wrapT: THREE.ClampToEdgeWrapping,
minFilter: THREE.NearestFilter,
magFilter: THREE.NearestFilter,
format: THREE.RGBAFormat,
type: THREE.UnsignedByteType,
depthBuffer: false
});
}
function fillTexture(texture) {
const waterMaxHeight = 10;
function noise(x, y) {
let multR = waterMaxHeight;
let mult = 0.025;
let r = 0;
for (let i = 0; i < 15; i++) {
r += multR * simplex.noise(x * mult, y * mult);
multR *= 0.53 + 0.025 * i;
mult *= 1.25;
}
return r;
}
const pixels = texture.image.data;
let p = 0;
for (let j = 0; j < WIDTH; j++) {
for (let i = 0; i < WIDTH; i++) {
const x = i * 128 / WIDTH;
const y = j * 128 / WIDTH;
pixels[p + 0] = noise(x, y);
pixels[p + 1] = pixels[p + 0];
pixels[p + 2] = 0;
pixels[p + 3] = 1;
p += 4;
}
}
}
function smoothWater() {
const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable);
const alternateRenderTarget = gpuCompute.getAlternateRenderTarget(heightmapVariable);
for (let i = 0; i < 10; i++) {
smoothShader.uniforms["smoothTexture"].value = currentRenderTarget.texture;
gpuCompute.doRenderTarget(smoothShader, alternateRenderTarget);
smoothShader.uniforms["smoothTexture"].value = alternateRenderTarget.texture;
gpuCompute.doRenderTarget(smoothShader, currentRenderTarget);
}
}
function createSpheres() {
const sphereTemplate = new THREE.Mesh(
new THREE.SphereGeometry(4, 24, 12),
new THREE.MeshPhongMaterial({ color: 16776960 })
);
for (let i = 0; i < NUM_SPHERES; i++) {
let sphere = sphereTemplate;
if (i < NUM_SPHERES - 1) {
sphere = sphereTemplate.clone();
}
sphere.position.x = (Math.random() - 0.5) * BOUNDS * 0.7;
sphere.position.z = (Math.random() - 0.5) * BOUNDS * 0.7;
sphere.userData.velocity = new THREE.Vector3();
scene.add(sphere);
spheres[i] = sphere;
}
}
function sphereDynamics() {
const currentRenderTarget = gpuCompute.getCurrentRenderTarget(heightmapVariable);
readWaterLevelShader.uniforms["levelTexture"].value = currentRenderTarget.texture;
for (let i = 0; i < NUM_SPHERES; i++) {
const sphere = spheres[i];
if (sphere) {
const u = 0.5 * sphere.position.x / BOUNDS_HALF + 0.5;
const v = 1 - (0.5 * sphere.position.z / BOUNDS_HALF + 0.5);
readWaterLevelShader.uniforms["point1"].value.set(u, v);
gpuCompute.doRenderTarget(readWaterLevelShader, readWaterLevelRenderTarget);
renderer.readRenderTargetPixels(
readWaterLevelRenderTarget,
0,
0,
4,
1,
readWaterLevelImage
);
const pixels = new Float32Array(readWaterLevelImage.buffer);
waterNormal.set(pixels[1], 0, -pixels[2]);
const pos = sphere.position;
pos.y = pixels[0];
waterNormal.multiplyScalar(0.1);
sphere.userData.velocity.add(waterNormal);
sphere.userData.velocity.multiplyScalar(0.998);
pos.add(sphere.userData.velocity);
if (pos.x < -256) {
pos.x = -256 + 1e-3;
sphere.userData.velocity.x *= -0.3;
} else if (pos.x > BOUNDS_HALF) {
pos.x = BOUNDS_HALF - 1e-3;
sphere.userData.velocity.x *= -0.3;
}
if (pos.z < -256) {
pos.z = -256 + 1e-3;
sphere.userData.velocity.z *= -0.3;
} else if (pos.z > BOUNDS_HALF) {
pos.z = BOUNDS_HALF - 1e-3;
sphere.userData.velocity.z *= -0.3;
}
}
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function setMouseCoords(x, y) {
mouseCoords.set(
x / renderer.domElement.clientWidth * 2 - 1,
-(y / renderer.domElement.clientHeight) * 2 + 1
);
mouseMoved = true;
}
function onPointerMove(event) {
if (event.isPrimary === false) return;
setMouseCoords(event.clientX, event.clientY);
}
function animate() {
render();
stats.update();
}
function render() {
const uniforms = heightmapVariable.material.uniforms;
if (mouseMoved) {
raycaster.setFromCamera(mouseCoords, camera);
const intersects = raycaster.intersectObject(meshRay);
if (intersects.length > 0) {
const point = intersects[0].point;
uniforms["mousePos"].value.set(point.x, point.z);
} else {
uniforms["mousePos"].value.set(1e4, 1e4);
}
mouseMoved = false;
} else {
uniforms["mousePos"].value.set(1e4, 1e4);
}
gpuCompute.compute();
if (spheresEnabled) {
sphereDynamics();
}
waterUniforms["heightmap"].value = gpuCompute.getCurrentRenderTarget(heightmapVariable).texture;
renderer.render(scene, camera);
}
}
};
export {
exampleInfo as default
};
ts
import { Loader, TypedArray } from 'three';
/**
* 官网示例的多端使用封装把版本
* */
export interface OfficialExampleInfo extends MiniProgramMeta {
/*** 示例名称(保持和官网一致)*/
name: string;
/** main */
init: (context: LoadContext) => void;
}
export interface LoadContext {
window: EventTarget & { innerWidth: number; innerHeight: number; devicePixelRatio: number };
/** HTMLCanvasElement */
canvas: any;
/** https://www.npmjs.com/package/lil-gui */
GUI: any;
/**
* https://www.npmjs.com/package/stats.js
* 也可以使用其他受支持的版本
* */
Stats: any;
/** 收集需要 dispose 的对象(官方示例没有处理这部分)*/
needToDispose: (...objs: any[]) => void | ((fromFn: () => any[]) => void);
/**基于 raq 的通用封装 */
useFrame(animateFunc: (/** ms */ delta: number) => void): { cancel: () => void };
/** 显示加载模态框 */
requestLoading(text?: string): Promise<void>;
/** 隐藏加载模态框*/
cancelLoading(): void;
/** 保存文件的通用封装*/
saveFile(
fileName: string,
data: ArrayBuffer | TypedArray | DataView | string
): Promise<string | null>;
/** 示例使用 DracoDecoder 时的资源路径 */
DecoderPath: {
GLTF: string;
STANDARD: string;
};
/** 为资源路径拼上 CDN 前缀 */
withCDNPrefix(path: string): string;
/**
* 在小程序中应使用 import { VideoTexture } from '@minisheep/three-platform-adapter/override/jsm/textures/VideoTexture.js';
* 正常情况(web) 可直接使用 THREE.VideoTexture
* */
getVideoTexture(videoOptions: VideoOptions): Promise<[{ isVideoTexture: true }, video: any]>;
/**
* 在小程序中应使用 import { CameraTexture } from '@minisheep/three-platform-adapter/override/jsm/textures/CameraTexture.js';
* 正常情况(web) 可参考示例 webgl_materials_video_webcam
* */
getCameraTexture(): { isVideoTexture: true };
/** 用于动态修改 info 中的占位符*/
bindInfoText(template: `$${string}$`, initValue?: string): { value: string };
/** 分屏控件对应的事件回调 */
onSlideStart(handle: () => void): void;
/** 分屏控件对应的事件回调 */
onSlideEnd(handle: () => void): void;
/** 分屏控件对应的事件回调 */
onSlideChange(handle: (offset: number, boxSize: number) => void): void;
}
export type VideoOptions = {
src: string;
/** 相当于 HTMLVideoElement 的 naturalWidth (小程序中获取不到)*/
width: number;
/** 相当于 HTMLVideoElement 的 naturalHeight (小程序中获取不到)*/
height: number;
loop?: boolean;
autoplay?: boolean;
muted?: boolean;
};
/** 示例小程序中使用的一些配置 */
export interface MiniProgramMeta {
/** 用于统计加载相关信息 */
useLoaders: Record<string, Loader>;
/** 通用 info */
info: TagItem[][];
/** 特殊 info */
infoPanel?: {
left?: [string, string][];
right?: [string, string][];
};
/** 分屏控件配置 */
needSlider?: {
/** 方向 */
direction?: 'horizontal' | 'vertical';
/** 初始偏移 0-100 */
initPosition?: number;
};
/** 操作摇杆控件 */
needArrowControls?: boolean;
/** 默认需要的画布类型 */
canvasType?: '2d' | 'webgl' | 'webgl2';
/** 为保持效果一致所需要的画布样式 */
canvasStyle?: {
bgColor?: string;
width?: number | string;
height?: number | string;
};
/** 部分示例需要在加载前进行一些提示 */
initAfterConfirm?: {
/**
* 提示类型
* @default 'default'
* */
type?: 'warning' | 'default';
text: string[];
};
}
export interface BaseTag<T extends string> {
tag: T;
content: string;
}
export interface ATag extends BaseTag<'a'> {
link: string;
}
export type TextTag = BaseTag<'text'>;
export type TagItem = TextTag | ATag;