webgl_postprocessing_material_ao
对应 three.js 示例地址 。
仅需关注 init
函数的内容,其他部分都是示例小程序所使用的描述配置。
js
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader.js";
import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { GTAOPass } from "three/examples/jsm/postprocessing/GTAOPass.js";
import { OutputPass } from "three/examples/jsm/postprocessing/OutputPass.js";
import { MeshPostProcessingMaterial } from "three/examples/jsm/materials/MeshPostProcessingMaterial.js";
/** @type {import("@minisheeep/mp-three-examples").OfficialExampleInfo} */
const exampleInfo = {
name: "webgl_postprocessing_material_ao",
useLoaders: [RGBELoader, PLYLoader],
info: [
[
{
tag: "a",
link: "https://threejs.org",
content: "three.js"
},
{
tag: "text",
content: "- Mesh Post Processing Material by"
},
{
tag: "a",
link: "https://github.com/Rabbid76",
content: "Rabbid76"
}
]
],
init: ({ window, canvas, GUI, Stats, needToDispose, useFrame }) => {
let renderer, camera, scene, composer, controls, stats;
const sceneParameters = {
output: 0,
envMapIntensity: 1,
ambientLightIntensity: 0,
lightIntensity: 50,
shadow: true
};
const aoParameters = {
radius: 0.5,
distanceExponent: 2,
thickness: 10,
scale: 1,
samples: 16,
distanceFallOff: 1
};
init();
function init() {
stats = new Stats(renderer);
renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setAnimationLoop(animate);
renderer.shadowMap.enabled = sceneParameters.shadow;
const plyLoader = new PLYLoader();
const rgbeloader = new RGBELoader();
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 50);
camera.position.set(0, 3, 5);
controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1, 0);
controls.update();
controls.enablePan = false;
controls.enableDamping = true;
const width = window.innerWidth;
const height = window.innerHeight;
scene = new THREE.Scene();
composer = new EffectComposer(renderer);
const gtaoPass = new GTAOPass(scene, camera, width, height);
gtaoPass.output = GTAOPass.OUTPUT.Off;
const renderPasse = new RenderPass(scene, camera);
const outputPass = new OutputPass();
composer.addPass(gtaoPass);
composer.addPass(renderPasse);
composer.addPass(outputPass);
rgbeloader.load("textures/equirectangular/royal_esplanade_1k.hdr", function(texture) {
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.environment = texture;
});
const groundMaterial = new MeshPostProcessingMaterial({
color: 8355711,
envMapIntensity: sceneParameters.envMapIntensity,
aoPassMap: gtaoPass.gtaoMap
});
const objectMaterial = new MeshPostProcessingMaterial({
color: 16777215,
roughness: 0.5,
metalness: 0.5,
envMapIntensity: sceneParameters.envMapIntensity,
aoPassMap: gtaoPass.gtaoMap
});
const emissiveMaterial = new MeshPostProcessingMaterial({
color: 0,
emissive: 16777215,
aoPassMap: gtaoPass.gtaoMap
});
plyLoader.load("models/ply/binary/Lucy100k.ply", (geometry) => {
geometry.computeVertexNormals();
const lucy = new THREE.Mesh(geometry, objectMaterial);
lucy.receiveShadow = true;
lucy.castShadow = true;
lucy.scale.setScalar(1e-3);
lucy.rotation.set(0, Math.PI, 0);
lucy.position.set(0.04, 1.8, 0.02);
scene.add(lucy);
});
const ambientLight = new THREE.AmbientLight(16777215, sceneParameters.ambientLightIntensity);
const lightGroup = new THREE.Group();
const planeGeometry = new THREE.PlaneGeometry(6, 6);
const cylinderGeometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 64);
const sphereGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const lightSphereGeometry = new THREE.SphereGeometry(0.1, 32, 32);
scene.background = new THREE.Color(12575709);
scene.add(ambientLight);
scene.add(lightGroup);
const targetObject = new THREE.Object3D();
targetObject.position.set(0, 1, 0);
scene.add(targetObject);
const lightColors = [16728128, 4259648, 4210943];
for (let j = 0; j < 3; ++j) {
const light = new THREE.SpotLight(
lightColors[j],
sceneParameters.lightIntensity,
0,
Math.PI / 9
);
light.castShadow = true;
light.shadow.camera.far = 15;
light.position.set(
5 * Math.cos(Math.PI * j * 2 / 3),
2.5,
5 * Math.sin(Math.PI * j * 2 / 3)
);
light.target = targetObject;
lightGroup.add(light);
}
const groundPlane = new THREE.Mesh(planeGeometry, groundMaterial);
groundPlane.rotation.x = -Math.PI / 2;
groundPlane.position.set(0, 0, 0);
groundPlane.receiveShadow = true;
scene.add(groundPlane);
const pedestal = new THREE.Mesh(cylinderGeometry, groundMaterial);
pedestal.position.set(0, 0.5, 0);
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
const sphereMesh = new THREE.InstancedMesh(sphereGeometry, objectMaterial, 6);
sphereMesh.receiveShadow = true;
sphereMesh.castShadow = true;
scene.add(sphereMesh);
[...Array(6).keys()].forEach(
(i) => sphereMesh.setMatrixAt(
i,
new THREE.Matrix4().makeTranslation(
Math.cos(Math.PI * i / 3),
0.5,
Math.sin(Math.PI * i / 3)
)
)
);
const lightSphereMesh = new THREE.InstancedMesh(lightSphereGeometry, emissiveMaterial, 4);
scene.add(lightSphereMesh);
[...Array(4).keys()].forEach(
(i) => lightSphereMesh.setMatrixAt(
i,
new THREE.Matrix4().makeTranslation(
0.4 * Math.cos(Math.PI * (i + 0.5) / 2),
1.1,
0.45 * Math.sin(Math.PI * (i + 0.5) / 2)
)
)
);
const updateGtaoMaterial = () => gtaoPass.updateGtaoMaterial(aoParameters);
const updateOutput = () => {
composer.removePass(gtaoPass);
composer.insertPass(gtaoPass, sceneParameters.output == 1 ? 1 : 0);
switch (sceneParameters.output) {
default:
case 0:
gtaoPass.output = GTAOPass.OUTPUT.Off;
gtaoPass.enabled = true;
renderPasse.enabled = true;
break;
case 1:
gtaoPass.output = GTAOPass.OUTPUT.Default;
gtaoPass.enabled = true;
renderPasse.enabled = true;
break;
case 2:
gtaoPass.output = GTAOPass.OUTPUT.Diffuse;
gtaoPass.enabled = false;
renderPasse.enabled = true;
break;
case 3:
gtaoPass.output = GTAOPass.OUTPUT.Denoise;
gtaoPass.enabled = true;
renderPasse.enabled = false;
break;
}
groundMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null;
objectMaterial.aoPassMap = sceneParameters.output === 0 ? gtaoPass.gtaoMap : null;
};
updateOutput();
updateGtaoMaterial();
const gui = new GUI();
gui.add(sceneParameters, "output", {
"material AO": 0,
"post blended AO": 1,
"only diffuse": 2,
"only AO": 3
}).onChange(() => updateOutput());
gui.add(sceneParameters, "envMapIntensity").min(0).max(1).step(0.01).onChange(() => {
groundMaterial.envMapIntensity = sceneParameters.envMapIntensity;
objectMaterial.envMapIntensity = sceneParameters.envMapIntensity;
});
gui.add(sceneParameters, "ambientLightIntensity").min(0).max(1).step(0.01).onChange(() => {
ambientLight.intensity = sceneParameters.ambientLightIntensity;
});
gui.add(sceneParameters, "lightIntensity").min(0).max(100).step(1).onChange(() => {
lightGroup.children.forEach(
(light) => light.intensity = sceneParameters.lightIntensity
);
});
gui.add(sceneParameters, "shadow").onChange((value) => {
renderer.shadowMap.enabled = value;
lightGroup.children.forEach((light) => light.castShadow = value);
});
gui.add(aoParameters, "radius").min(0.01).max(2).step(0.01).onChange(() => updateGtaoMaterial());
gui.add(aoParameters, "distanceExponent").min(1).max(4).step(0.01).onChange(() => updateGtaoMaterial());
gui.add(aoParameters, "thickness").min(0.01).max(10).step(0.01).onChange(() => updateGtaoMaterial());
gui.add(aoParameters, "distanceFallOff").min(0).max(1).step(0.01).onChange(() => updateGtaoMaterial());
gui.add(aoParameters, "scale").min(0.01).max(2).step(0.01).onChange(() => updateGtaoMaterial());
gui.add(aoParameters, "samples").min(2).max(32).step(1).onChange(() => updateGtaoMaterial());
window.addEventListener("resize", onWindowResize);
needToDispose(renderer, scene, controls, composer, controls, rgbeloader, plyLoader);
}
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
composer.setSize(width, height);
}
function animate() {
controls.update();
stats.begin();
composer.render();
stats.end();
}
}
};
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;