webgl_shadowmesh
对应 three.js 示例地址 。
仅需关注 init
函数的内容,其他部分都是示例小程序所使用的描述配置。
js
import * as THREE from "three";
import { ShadowMesh } from "three/examples/jsm/objects/ShadowMesh.js";
/** @type {import("@minisheeep/mp-three-examples").OfficialExampleInfo} */
const exampleInfo = {
name: "webgl_shadowmesh",
useLoaders: [],
info: [
[
{
tag: "a",
link: "https://threejs.org",
content: "three.js"
},
{
tag: "text",
content: "- shadow mesh"
}
]
],
init: ({ window, canvas, GUI, Stats, needToDispose, useFrame }) => {
let SCREEN_WIDTH = window.innerWidth;
let SCREEN_HEIGHT = window.innerHeight;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(55, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3e3);
const clock = new THREE.Clock();
const renderer = new THREE.WebGLRenderer({ stencil: true, canvas });
const sunLight = new THREE.DirectionalLight("rgb(255,255,255)", 3);
let useDirectionalLight = true;
let arrowHelper1, arrowHelper2, arrowHelper3;
const arrowDirection = new THREE.Vector3();
const arrowPosition1 = new THREE.Vector3();
const arrowPosition2 = new THREE.Vector3();
const arrowPosition3 = new THREE.Vector3();
let groundMesh;
let lightSphere, lightHolder;
let pyramid, pyramidShadow;
let sphere, sphereShadow;
let cube, cubeShadow;
let cylinder, cylinderShadow;
let torus, torusShadow;
const normalVector = new THREE.Vector3(0, 1, 0);
const planeConstant = 0.01;
const groundPlane = new THREE.Plane(normalVector, planeConstant);
const lightPosition4D = new THREE.Vector4();
let verticalAngle = 0;
let horizontalAngle = 0;
let frameTime = 0;
const TWO_PI = Math.PI * 2;
init();
function init() {
scene.background = new THREE.Color(38655);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
renderer.setAnimationLoop(animate);
window.addEventListener("resize", onWindowResize);
camera.position.set(0, 2.5, 10);
scene.add(camera);
onWindowResize();
sunLight.position.set(5, 7, -1);
sunLight.lookAt(scene.position);
scene.add(sunLight);
lightPosition4D.x = sunLight.position.x;
lightPosition4D.y = sunLight.position.y;
lightPosition4D.z = sunLight.position.z;
lightPosition4D.w = 1e-3;
arrowDirection.subVectors(scene.position, sunLight.position).normalize();
arrowPosition1.copy(sunLight.position);
arrowHelper1 = new THREE.ArrowHelper(
arrowDirection,
arrowPosition1,
0.9,
16776960,
0.25,
0.08
);
scene.add(arrowHelper1);
arrowPosition2.copy(sunLight.position).add(new THREE.Vector3(0, 0.2, 0));
arrowHelper2 = new THREE.ArrowHelper(
arrowDirection,
arrowPosition2,
0.9,
16776960,
0.25,
0.08
);
scene.add(arrowHelper2);
arrowPosition3.copy(sunLight.position).add(new THREE.Vector3(0, -0.2, 0));
arrowHelper3 = new THREE.ArrowHelper(
arrowDirection,
arrowPosition3,
0.9,
16776960,
0.25,
0.08
);
scene.add(arrowHelper3);
const lightSphereGeometry = new THREE.SphereGeometry(0.09);
const lightSphereMaterial = new THREE.MeshBasicMaterial({ color: "rgb(255,255,255)" });
lightSphere = new THREE.Mesh(lightSphereGeometry, lightSphereMaterial);
scene.add(lightSphere);
lightSphere.visible = false;
const lightHolderGeometry = new THREE.CylinderGeometry(0.05, 0.05, 0.13);
const lightHolderMaterial = new THREE.MeshBasicMaterial({ color: "rgb(75,75,75)" });
lightHolder = new THREE.Mesh(lightHolderGeometry, lightHolderMaterial);
scene.add(lightHolder);
lightHolder.visible = false;
const groundGeometry = new THREE.BoxGeometry(30, 0.01, 40);
const groundMaterial = new THREE.MeshLambertMaterial({ color: "rgb(0,130,0)" });
groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.position.y = 0;
scene.add(groundMesh);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeMaterial = new THREE.MeshLambertMaterial({
color: "rgb(255,0,0)",
emissive: 2097152
});
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.z = -1;
scene.add(cube);
cubeShadow = new ShadowMesh(cube);
scene.add(cubeShadow);
const cylinderGeometry = new THREE.CylinderGeometry(0.3, 0.3, 2);
const cylinderMaterial = new THREE.MeshPhongMaterial({
color: "rgb(0,0,255)",
emissive: 32
});
cylinder = new THREE.Mesh(cylinderGeometry, cylinderMaterial);
cylinder.position.z = -2.5;
scene.add(cylinder);
cylinderShadow = new ShadowMesh(cylinder);
scene.add(cylinderShadow);
const torusGeometry = new THREE.TorusGeometry(1, 0.2, 10, 16, TWO_PI);
const torusMaterial = new THREE.MeshPhongMaterial({
color: "rgb(255,0,255)",
emissive: 2097184
});
torus = new THREE.Mesh(torusGeometry, torusMaterial);
torus.position.z = -6;
scene.add(torus);
torusShadow = new ShadowMesh(torus);
scene.add(torusShadow);
const sphereGeometry = new THREE.SphereGeometry(0.5, 20, 10);
const sphereMaterial = new THREE.MeshPhongMaterial({
color: "rgb(255,255,255)",
emissive: 2236962
});
sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(4, 0.5, 2);
scene.add(sphere);
sphereShadow = new ShadowMesh(sphere);
scene.add(sphereShadow);
const pyramidGeometry = new THREE.CylinderGeometry(0, 0.5, 2, 4);
const pyramidMaterial = new THREE.MeshPhongMaterial({
color: "rgb(255,255,0)",
emissive: 4456448,
flatShading: true,
shininess: 0
});
pyramid = new THREE.Mesh(pyramidGeometry, pyramidMaterial);
pyramid.position.set(-4, 1, 2);
scene.add(pyramid);
pyramidShadow = new ShadowMesh(pyramid);
scene.add(pyramidShadow);
const gui = new GUI();
gui.add(
{
lightButtonHandler
},
"lightButtonHandler"
).name("Switch Light");
needToDispose(renderer, scene);
}
function animate() {
frameTime = clock.getDelta();
cube.rotation.x += 1 * frameTime;
cube.rotation.y += 1 * frameTime;
cylinder.rotation.y += 1 * frameTime;
cylinder.rotation.z -= 1 * frameTime;
torus.rotation.x -= 1 * frameTime;
torus.rotation.y -= 1 * frameTime;
pyramid.rotation.y += 0.5 * frameTime;
horizontalAngle += 0.5 * frameTime;
if (horizontalAngle > TWO_PI) horizontalAngle -= TWO_PI;
cube.position.x = Math.sin(horizontalAngle) * 4;
cylinder.position.x = Math.sin(horizontalAngle) * -4;
torus.position.x = Math.cos(horizontalAngle) * 4;
verticalAngle += 1.5 * frameTime;
if (verticalAngle > TWO_PI) verticalAngle -= TWO_PI;
cube.position.y = Math.sin(verticalAngle) * 2 + 2.9;
cylinder.position.y = Math.sin(verticalAngle) * 2 + 3.1;
torus.position.y = Math.cos(verticalAngle) * 2 + 3.3;
cubeShadow.update(groundPlane, lightPosition4D);
cylinderShadow.update(groundPlane, lightPosition4D);
torusShadow.update(groundPlane, lightPosition4D);
sphereShadow.update(groundPlane, lightPosition4D);
pyramidShadow.update(groundPlane, lightPosition4D);
renderer.render(scene, camera);
}
function onWindowResize() {
SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight;
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
}
function lightButtonHandler() {
useDirectionalLight = !useDirectionalLight;
if (useDirectionalLight) {
scene.background.setHex(38655);
groundMesh.material.color.setHex(33280);
sunLight.position.set(5, 7, -1);
sunLight.lookAt(scene.position);
lightPosition4D.x = sunLight.position.x;
lightPosition4D.y = sunLight.position.y;
lightPosition4D.z = sunLight.position.z;
lightPosition4D.w = 1e-3;
arrowHelper1.visible = true;
arrowHelper2.visible = true;
arrowHelper3.visible = true;
lightSphere.visible = false;
lightHolder.visible = false;
} else {
scene.background.setHex(0);
groundMesh.material.color.setHex(9868950);
sunLight.position.set(0, 6, -2);
sunLight.lookAt(scene.position);
lightSphere.position.copy(sunLight.position);
lightHolder.position.copy(lightSphere.position);
lightHolder.position.y += 0.12;
lightPosition4D.x = sunLight.position.x;
lightPosition4D.y = sunLight.position.y;
lightPosition4D.z = sunLight.position.z;
lightPosition4D.w = 0.9;
arrowHelper1.visible = false;
arrowHelper2.visible = false;
arrowHelper3.visible = false;
lightSphere.visible = true;
lightHolder.visible = true;
}
}
}
};
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 {
//为了减少官方代码的改动,实际上等同于 canvas
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: 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;