-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
MeshWobbleMaterial
(#63)
This PR introduces `MeshWobbleMaterial` - Add `MeshWobbleMaterial` and its story - Adjust `MeshDistortMaterial` to expose a parameters interface
- Loading branch information
Showing
6 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { IUniform, MeshStandardMaterial, MeshStandardMaterialParameters } from 'three' | ||
|
||
export interface MeshWobbleMaterialParameters { | ||
time?: number | ||
factor?: number | ||
} | ||
|
||
export class MeshWobbleMaterial extends MeshStandardMaterial { | ||
_time: IUniform<number> | ||
_factor: IUniform<number> | ||
|
||
constructor({ | ||
time = 0, | ||
factor = 1, | ||
...parameters | ||
}: MeshStandardMaterialParameters & MeshWobbleMaterialParameters = {}) { | ||
super(parameters) | ||
this.setValues(parameters) | ||
this._time = { value: time } | ||
this._factor = { value: factor } | ||
} | ||
|
||
// FIXME Use `THREE.WebGLProgramParametersWithUniforms` type when able to target @types/three@0.160.0 | ||
override onBeforeCompile(shader: { vertexShader: string; uniforms: { [uniform: string]: IUniform } }) { | ||
shader.uniforms['time'] = this._time | ||
shader.uniforms['factor'] = this._factor | ||
|
||
shader.vertexShader = ` | ||
uniform float time; | ||
uniform float factor; | ||
${shader.vertexShader} | ||
` | ||
shader.vertexShader = shader.vertexShader.replace( | ||
'#include <begin_vertex>', | ||
`float theta = sin( time + position.y ) / 2.0 * factor; | ||
float c = cos( theta ); | ||
float s = sin( theta ); | ||
mat3 m = mat3( c, 0, s, 0, 1, 0, -s, 0, c ); | ||
vec3 transformed = vec3( position ) * m; | ||
vNormal = vNormal * m;` | ||
) | ||
} | ||
|
||
get time() { | ||
return this._time.value | ||
} | ||
|
||
set time(v) { | ||
this._time.value = v | ||
} | ||
|
||
get factor() { | ||
return this._factor.value | ||
} | ||
|
||
set factor(v) { | ||
this._factor.value = v | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters