diff --git a/.storybook/stories/MeshDistortMaterial.stories.ts b/.storybook/stories/MeshDistortMaterial.stories.ts index 55dab67..7e48d93 100644 --- a/.storybook/stories/MeshDistortMaterial.stories.ts +++ b/.storybook/stories/MeshDistortMaterial.stories.ts @@ -44,9 +44,7 @@ export const MDMStory = async () => { async function setupMeshDistortMaterial() { const geometry = new THREE.SphereGeometry(1, 32, 32) - meshDistortMaterial = new MeshDistortMaterial({ color: '#f25042' }) - - meshDistortMaterial._radius.value = 0.2 + meshDistortMaterial = new MeshDistortMaterial({ color: '#f25042', radius: 0.2 }) const mesh = new THREE.Mesh(geometry, meshDistortMaterial) mesh.scale.set(2, 4, 1) diff --git a/.storybook/stories/MeshWobbleMaterial.stories.ts b/.storybook/stories/MeshWobbleMaterial.stories.ts new file mode 100644 index 0000000..c346516 --- /dev/null +++ b/.storybook/stories/MeshWobbleMaterial.stories.ts @@ -0,0 +1,76 @@ +import * as THREE from 'three' +import { Setup } from '../Setup' +import GUI from 'lil-gui' +import { Meta } from '@storybook/html' +import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' +import { MeshWobbleMaterial } from '../../src/materials/MeshWobbleMaterial' + +export default { + title: 'Shaders/MeshWobbleMaterial', +} as Meta // TODO: this should be `satisfies Meta` but commit hooks lag behind TS + +let gui: GUI, materialGuiFolder: GUI +let scene: THREE.Scene, + camera: THREE.PerspectiveCamera, + renderer: THREE.WebGLRenderer, + animateLoop: (arg0: (time: number) => void) => void, + meshWobbleMaterial: MeshWobbleMaterial + +export const MWMStory = async () => { + const setupResult = Setup() + scene = setupResult.scene + camera = setupResult.camera + renderer = setupResult.renderer + animateLoop = setupResult.render + + gui = new GUI({ title: MWMStory.storyName }) + camera.position.set(0, 0, 5) + + const controls = new OrbitControls(camera, renderer.domElement) + controls.update() + + const ambientLight = new THREE.AmbientLight() + scene.add(ambientLight) + + const dirLight = new THREE.DirectionalLight(0xabcdef, 10) + dirLight.position.set(1, 20, 1) + dirLight.castShadow = true + dirLight.shadow.mapSize.width = 1024 + dirLight.shadow.mapSize.height = 1024 + scene.add(dirLight) + + setupMeshWobbleMaterial() +} + +async function setupMeshWobbleMaterial() { + const geometry = new THREE.TorusGeometry(1, 0.25, 16, 100) + meshWobbleMaterial = new MeshWobbleMaterial({ color: '#f25042', factor: 0.75 }) + + const mesh = new THREE.Mesh(geometry, meshWobbleMaterial) + + scene.add(mesh) + + createMeshWobbleGUI() + + animateLoop((time) => { + meshWobbleMaterial.time = time * 0.0025 + }) +} + +/** + * Create gui for material properties + */ +function createMeshWobbleGUI() { + if (materialGuiFolder) { + materialGuiFolder.destroy() + } + + const matProps = gui.addFolder('MeshWobbleMaterial id: ' + meshWobbleMaterial.id) + + matProps.addColor(meshWobbleMaterial, 'color') + matProps.add(meshWobbleMaterial._factor, 'value').min(0.5).max(4).step(0.1).name('factor') + + materialGuiFolder = matProps +} + +MWMStory.storyName = 'Torus' diff --git a/README.md b/README.md index 3fe258f..b863b79 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ import { pcss, ... } from '@pmndrs/vanilla'
@@ -193,6 +194,12 @@ const mesh = new THREE.Mesh(geometry, new MeshDiscardMaterial())
This material makes your geometry distort following simplex noise.
+#### MeshWobbleMaterial
+
+[![storybook](https://img.shields.io/badge/-storybook-%23ff69b4)](https://pmndrs.github.io/drei-vanilla/?path=/story/shaders-meshwobblematerial--mwm-story)
+
+This material makes your geometry wobble and wave around. It was taken from the [threejs-examples](https://threejs.org/examples/#webgl_materials_modified) and adapted into a self-contained material.
+
#### MeshTransmissionMaterial
[![storybook](https://img.shields.io/badge/-storybook-%23ff69b4)](https://pmndrs.github.io/drei-vanilla/?path=/story/shaders-meshtransmissionmaterial--mtm-story)
diff --git a/src/materials/MeshDistortMaterial.ts b/src/materials/MeshDistortMaterial.ts
index 7ed5000..2ef97e4 100644
--- a/src/materials/MeshDistortMaterial.ts
+++ b/src/materials/MeshDistortMaterial.ts
@@ -2,17 +2,28 @@ import { IUniform, MeshPhysicalMaterial, MeshPhysicalMaterialParameters } from '
// @ts-ignore
import distort from '../helpers/glsl/distort.vert.glsl'
+export interface MeshDistortMaterialParameters {
+ time?: number
+ distort?: number
+ radius?: number
+}
+
export class MeshDistortMaterial extends MeshPhysicalMaterial {
_time: IUniform