Skip to content

Commit

Permalink
feat: create Trance
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeCatelSoyhuce committed Jan 14, 2024
1 parent e3db301 commit d541312
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"editor.formatOnSave": true,
"files.autoSave": "onFocusChange",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.stylelint": true
"source.fixAll.eslint": "explicit",
"source.fixAll.stylelint": "explicit"
},
"cSpell.words": [
"npm",
Expand Down
40 changes: 40 additions & 0 deletions src/trance/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useRef } from 'react'
import { Canvas } from '@react-three/fiber'
import { OrbitControls } from '@react-three/drei'

import { Scene } from './trance'

const Trance = () => {
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 (
<Canvas
camera={{ zoom: 1 }}
gl={{ preserveDrawingBuffer: true }}
onDoubleClick={handleCanvasClick}
ref={canvas}
style={{ width: '100%', height: '100%' }}
>
<Scene />
<OrbitControls />
</Canvas>
)
}

export { Trance }
15 changes: 15 additions & 0 deletions src/trance/trance.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

Check warning on line 1 in src/trance/trance.stories.tsx

View workflow job for this annotation

GitHub Actions / lint

'React' is defined but never used
import type { Meta, StoryObj } from '@storybook/react'

import { Trance } from '.'

const meta: Meta<typeof Trance> = {
title: '3D/Trance',
component: Trance
}

export default meta

type Story = StoryObj<typeof Trance>

export const Primary: Story = {}
45 changes: 45 additions & 0 deletions src/trance/trance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* 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 sc = 0.1
let iterations = 2
let startScale = 0.5*pow(2, iterations)
let os = getSpace()
for (let i = 0; i<iterations; i++) {
mirrorXYZ()
displace(sc*startScale,sc*startScale,sc*startScale)
startScale *= 0.5
}
let sp = getSpace()
let v = smoothstep(nsin(time*10.*length(os) + 5*fractalNoise(30*sp)), .0, 0.25)
color(v,v*0.2,v*0.1)
box(sc,sc,sc)
`)

const Scene = () => {
const el = useRef<Mesh<BoxGeometry, ShaderMaterial>>()

useFrame(() => {
if (el.current) {
el.current.material.uniforms.time.value += 0.025
}
})

return (
<>
<color args={['#fff']} attach='background' />
<primitive object={mesh} ref={el} scale={1} />
</>
)
}

export { Scene }

0 comments on commit d541312

Please sign in to comment.