Skip to content

Commit 19a98a7

Browse files
committed
move textured-sprites-r3f example to lookbook
1 parent 00f9f6d commit 19a98a7

File tree

12 files changed

+111
-98
lines changed

12 files changed

+111
-98
lines changed
Loading

examples/r3f/src/textured-sprites/BouncingSprites.js renamed to apps/lookbook/src/components/demos/textured-sprites-r3f/BouncingSprites.js

+13-22
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export class BouncingSprites {
2020
this.containerHeight = height;
2121
this.spriteSize = spriteSize;
2222

23-
console.log("new BouncingSprites()", { width, height, spriteSize });
23+
console.log('new BouncingSprites()', {width, height, spriteSize});
2424
}
2525

26-
init({ geometry, atlas, capacity }) {
26+
init({geometry, atlas, capacity}) {
2727
this.spritePool = geometry.instancedPool;
2828
this.textureAtlas = atlas;
2929
this.initalSpriteCount = capacity;
@@ -34,48 +34,41 @@ export class BouncingSprites {
3434

3535
this.createSprites();
3636

37-
console.log("bouncingSprites.init()", this);
37+
console.log('bouncingSprites.init()', this);
3838
}
3939

4040
createSprites(count = this.initalSpriteCount) {
41-
const [halfWidth, halfHeight] = [
42-
this.containerWidth / 2,
43-
this.containerHeight / 2,
44-
];
41+
const [halfWidth, halfHeight] = [this.containerWidth / 2, this.containerHeight / 2];
4542

4643
for (let i = 0; i < count; i++) {
4744
const sprite = this.spritePool.createVO();
4845

4946
sprite.setSize(this.spriteSize, this.spriteSize);
5047

51-
sprite.setPosition(
52-
Math.random() * this.containerWidth - halfWidth,
53-
Math.random() * this.containerHeight - halfHeight
54-
);
48+
sprite.setPosition(Math.random() * this.containerWidth - halfWidth, Math.random() * this.containerHeight - halfHeight);
5549

5650
sprite.setFrame(
5751
// this.frames ? sample(this.frames) : this.textureAtlas.randomFrame()
58-
this.textureAtlas.randomFrame()
52+
this.textureAtlas.randomFrame(),
5953
);
6054

6155
sprite.speedX = Math.random() * this.startSpeedX + this.startSpeedBaseX;
62-
sprite.speedY =
63-
Math.random() * this.startSpeedY -
64-
this.startSpeedY / 2 +
65-
this.startSpeedBaseY;
56+
sprite.speedY = Math.random() * this.startSpeedY - this.startSpeedY / 2 + this.startSpeedBaseY;
6657

6758
sprite.rotation = Math.random() * Math.PI * 2;
6859
sprite.speedRotate = Math.random() * Math.PI * this.speedRotateFactor;
6960

61+
sprite.setColor([1, 1, 1, 1]);
62+
7063
this.sprites.push(sprite);
7164
}
7265
}
7366

7467
update(args) {
75-
console.log("update", args);
68+
console.log('update', args);
7669
}
7770

78-
frame({ delta }) {
71+
frame({delta}) {
7972
const deltaFactor = delta;
8073

8174
const gravity = this.gravity * deltaFactor;
@@ -85,7 +78,7 @@ export class BouncingSprites {
8578
this.sprites.forEach((sprite) => {
8679
sprite.rotation += sprite.speedRotate * deltaFactor;
8780

88-
let { x, y, speedX, speedY } = sprite;
81+
let {x, y, speedX, speedY} = sprite;
8982

9083
x += speedX * deltaFactor;
9184
y += speedY * deltaFactor;
@@ -111,9 +104,7 @@ export class BouncingSprites {
111104
}
112105
} else if (y < -halfHeight) {
113106
// on the bottom edge
114-
speedY =
115-
Math.random() * this.upwindSpeed * this.containerHeight +
116-
this.upwindBaseSpeed * this.containerHeight;
107+
speedY = Math.random() * this.upwindSpeed * this.containerHeight + this.upwindBaseSpeed * this.containerHeight;
117108
y = -halfHeight;
118109
}
119110

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Effects } from "@react-three/drei";
2-
import { extend } from "@react-three/fiber";
1+
import {Effects} from '@react-three/drei';
2+
import {extend} from '@react-three/fiber';
33
import {
44
forwardRefValue,
55
GetStage2D,
@@ -13,21 +13,24 @@ import {
1313
TextureRef,
1414
useFrameLoop,
1515
useTextureAtlas,
16-
} from "twopoint5d-r3f";
17-
import { useEffect, useRef, useState } from "react";
18-
import { FilmPass } from "three/examples/jsm/postprocessing/FilmPass";
19-
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass";
16+
} from '@spearwolf/twopoint5d-r3f';
17+
import {useEffect, useRef, useState} from 'react';
2018

21-
import { LogStageSizeToConsole } from "../utils/LogStageSizeToConsole";
22-
import { WiredBox } from "../utils/WiredBox";
23-
import { BouncingSprites } from "./BouncingSprites";
19+
import {FilmPass} from 'three/examples/jsm/postprocessing/FilmPass';
20+
import {RenderPass} from 'three/examples/jsm/postprocessing/RenderPass';
2421

25-
extend({ RenderPass, FilmPass });
22+
import assetsUrl from '~demos/utils/assetsUrl.ts';
2623

27-
export const DemoOrDie = ({ capacity }) => {
24+
import {WiredBox} from '../WiredBox.tsx';
25+
import {BouncingSprites} from './BouncingSprites.js';
26+
import {LogStageSizeToConsole} from './LogStageSizeToConsole.jsx';
27+
28+
extend({RenderPass, FilmPass});
29+
30+
export function DemoOrDie({capacity}) {
2831
const geometry = useRef();
2932

30-
const atlas = useTextureAtlas("atlas0");
33+
const atlas = useTextureAtlas('atlas0');
3134

3235
const [tick, setTick] = useState(0);
3336
const [tack, setTack] = useState(null);
@@ -44,17 +47,17 @@ export const DemoOrDie = ({ capacity }) => {
4447
if (tick < 5) {
4548
setTimeout(() => setTick(tick + 1), 1000);
4649
} else {
47-
setTack({ isTack: true });
50+
setTack({isTack: true});
4851
}
4952
}, [tick]);
5053

5154
return (
5255
<>
5356
<TextureAtlas
5457
name="atlas0"
55-
url="/examples/assets/lab-walls-tiles.json"
58+
url={assetsUrl('lab-walls-tiles.json')}
5659
nearest
57-
overrideImageUrl="/examples/assets/lab-walls-tiles.png"
60+
overrideImageUrl={assetsUrl('lab-walls-tiles.png')}
5861
/>
5962

6063
<Stage2D name="stage0" noAutoRender noAutoClear>
@@ -66,40 +69,27 @@ export const DemoOrDie = ({ capacity }) => {
6669
</Stage2D>
6770

6871
<Stage2D name="stage1" noAutoRender noAutoClear defaultCamera>
69-
<ParallaxProjection
70-
plane="xy"
71-
origin="bottom left"
72-
width={150}
73-
height={150}
74-
fit="contain"
75-
/>
72+
<ParallaxProjection plane="xy" origin="bottom left" width={150} height={150} fit="contain" />
7673

7774
<LogStageSizeToConsole />
7875

7976
<TexturedSprites>
80-
<TexturedSpritesGeometry
81-
capacity={capacity}
82-
ref={geometry}
83-
></TexturedSpritesGeometry>
77+
<TexturedSpritesGeometry capacity={capacity} ref={geometry}></TexturedSpritesGeometry>
8478
<TexturedSpritesMaterial depthTest={false} depthWrite={false}>
8579
<TextureRef name="atlas0" attach="colorMap" />
8680
</TexturedSpritesMaterial>
8781
</TexturedSprites>
8882
</Stage2D>
8983

90-
<GetStage2D name={["stage0", "stage1"]}>
84+
<GetStage2D name={['stage0', 'stage1']}>
9185
{(stage0, stage1) => (
9286
<Effects disableRenderPass={true}>
9387
<renderPass args={[stage0.scene, stage0.camera]} />
94-
<renderPass
95-
clear={false}
96-
clearDepth={true}
97-
args={[stage1.scene, stage1.camera, undefined, false, false]}
98-
/>
88+
<renderPass clear={false} clearDepth={true} args={[stage1.scene, stage1.camera, undefined, false, false]} />
9989
<filmPass args={[1, 0.5, 10, 0]} />
10090
</Effects>
10191
)}
10292
</GetStage2D>
10393
</>
10494
);
105-
};
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Stage2DContext, useStageResize} from '@spearwolf/twopoint5d-r3f';
2+
import {useContext} from 'react';
3+
4+
export function LogStageSizeToConsole() {
5+
const stage = useContext(Stage2DContext);
6+
7+
useStageResize((width, height) => {
8+
console.log(`stage[${stage?.name}] size is`, width, height);
9+
});
10+
11+
return null;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {OrbitControls} from '@react-three/drei';
2+
import {Canvas} from '@react-three/fiber';
3+
import {StrictMode} from 'react';
4+
import {DemoOrDie} from './DemoOrDie.jsx';
5+
6+
export default function main() {
7+
return (
8+
<Canvas linear flat dpr={[1, 2]} camera={{position: [0, 0, 50]}}>
9+
<StrictMode>
10+
<DemoOrDie capacity={500} />
11+
<OrbitControls />
12+
</StrictMode>
13+
</Canvas>
14+
);
15+
}

apps/lookbook/src/pages/demos/_textured-quads-from-texture-atlas.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"title": "textured quads from texture atlas",
33
"description": "Render instanced quads with textures from a textures-atlas",
44
"url": "/demos/textured-quads-from-texture-atlas",
5-
"tags": ["TextureAtlas", "TextureAtlasLoader", "VertexObjects", "instanced", "textures", "vanilla"],
5+
"tags": ["TextureAtlas", "TextureAtlasLoader", "VertexObjects", "instanced", "texture-atlas", "textures", "vanilla"],
66
"previewImage": "textured-quads-from-texture-atlas.png"
77
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"title": "textured sprites (r3f)",
3+
"description": "Sprites loaded from a texture atlas with post processing example",
4+
"url": "/demos/textured-sprites-r3f",
5+
"tags": [
6+
"<GetStage2D/>",
7+
"<ParallaxProjection/>",
8+
"<Stage2D/>",
9+
"<TextureAtlas/>",
10+
"<TexturedSprites/>",
11+
"<TexturedSpritesGeometry/>",
12+
"<TexturedSpritesMaterial/>",
13+
"<TextureRef/>",
14+
"useFrameLoop",
15+
"useTextureAtlas",
16+
"texture-atlas",
17+
"textures",
18+
"react",
19+
"r3f"
20+
],
21+
"previewImage": "textured-sprites-r3f.png"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
import DemoOrDie from '~components/demos/textured-sprites-r3f/index.jsx';
3+
import Layout from '~layouts/ReactDemo.astro';
4+
import {title} from './_textured-sprites-r3f.json';
5+
---
6+
7+
<Layout title={title}>
8+
<DemoOrDie client:only />
9+
</Layout>
10+
11+
<style>
12+
body {
13+
margin: 0;
14+
height: 100vh;
15+
overflow-y: hidden;
16+
background-color: rgb(100, 118, 136);
17+
background: radial-gradient(circle, rgb(231, 231, 231) 0%, rgb(107, 122, 107) 100%);
18+
color: #eee;
19+
}
20+
</style>

examples/r3f/public/textured-sprites.html

-22
This file was deleted.

examples/r3f/src/textured-sprites/index.jsx

-16
This file was deleted.

packages/twopoint5d-r3f/src/hooks/useStageResize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useEffect, useLayoutEffect, useRef} from 'react';
1+
import {useEffect, useRef} from 'react';
22
import {useStageSize} from './useStageSize.js';
33

44
export type ResizeEffectCallback = (width: number, height: number) => any;
@@ -15,7 +15,7 @@ export const useStageResize = (resizeEffect: ResizeEffectCallback): void => {
1515
const callbackRef = useRef<ResizeEffectCallback>();
1616
const [width, height] = useStageSize();
1717

18-
useLayoutEffect(() => void (callbackRef.current = resizeEffect), [resizeEffect]);
18+
callbackRef.current = resizeEffect;
1919

2020
useEffect(() => {
2121
if (callbackRef.current) {

packages/twopoint5d-r3f/src/hooks/useStageSize.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Stage2D} from '@spearwolf/twopoint5d';
2+
import {StageResize} from '@spearwolf/twopoint5d/events.js';
23
import {useContext, useEffect, useState} from 'react';
34
import {Stage2DContext} from '../components/Stage2D.js';
45

@@ -26,8 +27,8 @@ export function useStageSize() {
2627
if (parentStage) {
2728
updateSize(parentStage.width, parentStage.height);
2829

29-
return parentStage.on('resize', (stage: Stage2D) => {
30-
updateSize(stage.width, stage.height);
30+
return parentStage.on(StageResize, ({width, height}: Stage2D) => {
31+
updateSize(width, height);
3132
});
3233
}
3334
}, [parentStage]);

0 commit comments

Comments
 (0)