-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b239d32
commit e3db301
Showing
4 changed files
with
142 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useRef } from 'react' | ||
import { Canvas } from '@react-three/fiber' | ||
import { OrbitControls } from '@react-three/drei' | ||
|
||
import { Scene } from './obscured' | ||
|
||
import NoiseTexture from './noise-texture.svg?url' | ||
|
||
interface Props { | ||
scale: number | ||
} | ||
|
||
const Obscured = ({ scale = 5 }: Props) => { | ||
const canvas = useRef<HTMLCanvasElement>(null) | ||
|
||
const handleCanvasClick = () => { | ||
if (canvas.current) { | ||
canvas.current.toBlob( | ||
(blob) => { | ||
const link = document.createElement('a') | ||
link.href = window.URL.createObjectURL(blob as Blob) | ||
link.download = `${Date.now().toString()}.webp` | ||
link.click() | ||
}, | ||
'image/webp', | ||
1 | ||
) | ||
} | ||
return false | ||
} | ||
|
||
return ( | ||
<div style={{ width: '100vw', height: '100vh', position: 'relative' }}> | ||
<Canvas | ||
camera={{ zoom: 1 }} | ||
gl={{ preserveDrawingBuffer: true }} | ||
onDoubleClick={handleCanvasClick} | ||
ref={canvas} | ||
style={{ width: '100%', height: '100%' }} | ||
> | ||
<Scene scale={scale} /> | ||
<OrbitControls /> | ||
</Canvas> | ||
<div | ||
style={{ | ||
opacity: 0.2, | ||
width: '100%', | ||
height: '100%', | ||
position: 'absolute', | ||
inset: 0, | ||
pointerEvents: 'none', | ||
backgroundImage: `url(${NoiseTexture})` | ||
}} | ||
></div> | ||
</div> | ||
) | ||
} | ||
|
||
export { Obscured } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,20 @@ | ||
import React from 'react' | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { Obscured } from '.' | ||
|
||
const meta: Meta<typeof Obscured> = { | ||
title: '3D/Obscured', | ||
component: Obscured, | ||
argTypes: { | ||
scale: { | ||
control: { type: 'number' } | ||
} | ||
} | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof Obscured> | ||
|
||
export const Primary: Story = {} |
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,56 @@ | ||
/* eslint-disable react/no-unknown-property */ | ||
import React, { useRef } from 'react' | ||
import { useFrame } from '@react-three/fiber' | ||
import { BoxGeometry, Mesh, ShaderMaterial } from 'three' | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import { sculptToThreeJSMesh } from 'shader-park-core' | ||
|
||
const mesh: Mesh = sculptToThreeJSMesh(` | ||
let strength = input(0.4, 0, 3); | ||
let twistMix = input(); | ||
setStepSize(.3); | ||
rotateY(time * .2); | ||
let warpedSpace = warpSpace(getSpace()); | ||
metal(1.9); | ||
shine(1); | ||
color(0,0,0,0); | ||
sphere(.75 + length(warpedSpace) * .2); | ||
function warpSpace(s) { | ||
let t = time / 4.; | ||
rotateY(getRayDirection().y * (1 - twistMix) * 12); | ||
s = getSpace().x * 8.0 * (vec3(0.5, .2, .1) + s); | ||
for(let i = 1.0; i < 3.0; i += 1.0) { | ||
s.x = s.x + strength * sin(2.0 * t + i * 1.5 * s.y) + t * 0.5; | ||
s.y = s.y + strength * cos(2.0 * t + i * 2.5 * s.x); | ||
} | ||
return .5 + 0.5 * cos(time + vec3(s.x, s.y, s.x) + vec3(0., 2., 4.)); | ||
} | ||
`) | ||
|
||
interface Props { | ||
scale: number | ||
} | ||
|
||
const Scene = ({ scale }: Props) => { | ||
const el = useRef<Mesh<BoxGeometry, ShaderMaterial>>() | ||
|
||
useFrame(() => { | ||
if (el.current) { | ||
el.current.material.uniforms.time.value += 0.1 | ||
} | ||
}) | ||
|
||
return ( | ||
<> | ||
<color args={['#000000']} attach='background' /> | ||
<primitive object={mesh} ref={el} /> | ||
</> | ||
) | ||
} | ||
|
||
export { Scene } |