-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove prototype.function.call because it doesn't work in classes
- Loading branch information
1 parent
1d08c90
commit 932021a
Showing
6 changed files
with
238 additions
and
11 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,55 @@ | ||
import { Color, AmbientLight, DirectionalLight, PointLight, SpotLight, Vector3 } from 'three' | ||
import { useEffect } from '@storybook/client-api' | ||
|
||
import { useThree } from '../setup' | ||
|
||
//@ts-ignore | ||
import { hilbert3D, Line2, LineMaterial, LineGeometry } from '../../src' | ||
|
||
export default { | ||
title: 'Geometry/Line', | ||
} | ||
|
||
export const Default = () => { | ||
// must run in this so the canvas has mounted in the iframe & can be accessed by `three` | ||
useEffect(() => { | ||
console.log('calling effect') | ||
const points = hilbert3D(new Vector3(0), 5).map((p: Vector3) => [p.x, p.y, p.z]) as [number, number, number][] | ||
|
||
const lineGeometry = new LineGeometry() | ||
const pValues = points.map((p) => (p instanceof Vector3 ? p.toArray() : p)) | ||
lineGeometry.setPositions(pValues.flat()) | ||
|
||
const line2 = new Line2( | ||
lineGeometry, | ||
new LineMaterial({ | ||
color: 'red', | ||
lineWidth: 3, | ||
}), | ||
) | ||
line2.computeLineDistances() | ||
|
||
const ambientLight = new AmbientLight('white', 0.2) | ||
const directionalLight = new DirectionalLight('pink', 2) | ||
directionalLight.position.set(0, -5, 0) | ||
const pointLight1 = new PointLight('pink', 0.4) | ||
pointLight1.position.set(-10, -10, 10) | ||
const pointLight2 = new PointLight('white', 0.2) | ||
pointLight2.position.set(-10, -10, 10) | ||
const spotLight = new SpotLight('white', 2, 35, Math.PI / 4, 2, 3.5) | ||
spotLight.position.set(-3, 6, -4) | ||
|
||
const { renderer, scene } = useThree({ | ||
// useFrame: (_, delta) => {}, | ||
}) | ||
|
||
scene.background = new Color(0x000000).convertSRGBToLinear() | ||
scene.add(ambientLight, directionalLight, pointLight1, pointLight2, spotLight, line2) | ||
|
||
return () => { | ||
renderer.dispose() | ||
} | ||
}, []) | ||
|
||
return '' | ||
} |
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,69 @@ | ||
import * as React from 'react' | ||
import { Vector2, Vector3, Color } from 'three' | ||
import { ReactThreeFiber } from 'react-three-fiber' | ||
//@ts-ignore | ||
import { LineGeometry, LineMaterial, LineMaterialParameters, Line2 } from 'three-stdlib' | ||
|
||
export type LineProps = { | ||
points: Array<Vector3 | [number, number, number]> | ||
color?: Color | string | number | ||
vertexColors?: Array<Color | [number, number, number]> | ||
lineWidth?: number | ||
} & Omit<ReactThreeFiber.Object3DNode<Line2, typeof Line2>, 'args'> & | ||
Omit< | ||
ReactThreeFiber.Object3DNode<LineMaterial, [LineMaterialParameters]>, | ||
'color' | 'vertexColors' | 'resolution' | 'args' | ||
> | ||
|
||
export const Line = React.forwardRef<Line2, LineProps>(function Line( | ||
{ points, color = 'black', vertexColors, lineWidth, dashed, ...rest }, | ||
ref, | ||
) { | ||
const [line2] = React.useState(() => new Line2()) | ||
const [lineMaterial] = React.useState(() => new LineMaterial()) | ||
const [resolution] = React.useState(() => new Vector2(512, 512)) | ||
|
||
const lineGeom = React.useMemo(() => { | ||
const geom = new LineGeometry() | ||
const pValues = points.map((p) => (p instanceof Vector3 ? p.toArray() : p)) | ||
geom.setPositions(pValues.flat()) | ||
|
||
if (vertexColors) { | ||
const cValues = vertexColors.map((c) => (c instanceof Color ? c.toArray() : c)) | ||
geom.setColors(cValues.flat()) | ||
} | ||
|
||
return geom | ||
}, [points, vertexColors]) | ||
|
||
React.useLayoutEffect(() => { | ||
line2.computeLineDistances() | ||
}, [points, line2]) | ||
|
||
React.useLayoutEffect(() => { | ||
if (dashed) { | ||
lineMaterial.defines.USE_DASH = '' | ||
} else { | ||
// Setting lineMaterial.defines.USE_DASH to undefined is apparently not sufficient. | ||
delete lineMaterial.defines.USE_DASH | ||
} | ||
lineMaterial.needsUpdate = true | ||
}, [dashed, lineMaterial]) | ||
|
||
return ( | ||
<primitive dispose={undefined} object={line2} ref={ref} {...rest}> | ||
<primitive dispose={undefined} object={lineGeom} attach="geometry" /> | ||
<primitive | ||
dispose={undefined} | ||
object={lineMaterial} | ||
attach="material" | ||
color={color} | ||
vertexColors={Boolean(vertexColors)} | ||
resolution={resolution} | ||
linewidth={lineWidth} | ||
dashed={dashed} | ||
{...rest} | ||
/> | ||
</primitive> | ||
) | ||
}) |
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,15 @@ | ||
import { Canvas } from 'react-three-fiber' | ||
import styled from 'styled-components' | ||
|
||
export default function LinesPage(): JSX.Element { | ||
return ( | ||
<Root> | ||
<Canvas></Canvas> | ||
</Root> | ||
) | ||
} | ||
|
||
const Root = styled.main` | ||
width: 100vw; | ||
height: 100vh; | ||
` |
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