-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(troika-three-text): ALPHA: BatchedText for batching many Text in…
…stances in a single draw call
- Loading branch information
Showing
11 changed files
with
1,030 additions
and
280 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
import { Object3DFacade } from "troika-3d"; | ||
import { BatchedText } from "troika-three-text"; | ||
import { TEXT_MESH_PROPS } from "./Text3DFacade"; | ||
|
||
export class BatchedText3DFacade extends Object3DFacade { | ||
constructor(parent) { | ||
super(parent, new BatchedText()) | ||
|
||
// Orphaned container for children so they don't end up in the scene | ||
this._texts = new Object3DFacade() | ||
} | ||
|
||
afterUpdate() { | ||
TEXT_MESH_PROPS.forEach(prop => { | ||
this.threeObject[prop] = this[prop]; | ||
}) | ||
this._texts.children = this.children | ||
this._texts.afterUpdate() | ||
this._texts.forEachChild(text => this.threeObject.addText(text.threeObject)) | ||
super.afterUpdate() | ||
} | ||
|
||
shouldUpdateChildren() { | ||
return false | ||
} | ||
} |
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
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
79 changes: 79 additions & 0 deletions
79
packages/troika-examples/text-batched/BatchedTextExample.jsx
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,79 @@ | ||
import React from "react"; | ||
import { Canvas3D } from "troika-3d"; | ||
import { Text3DFacade, BatchedText3DFacade } from "troika-3d-text"; | ||
import { FONTS } from '../text/TextExample' | ||
import { Color } from "three/src/math/Color"; | ||
|
||
export default function BatchedTextExample ({ stats, width, height }) { | ||
const [texts, setTexts] = React.useState(randomizeText()); | ||
|
||
function randomizeText() { | ||
const all = [ | ||
"One", "Two", "Three", "Four", "Five", | ||
"Six", "Seven", "Eight", "Nine", "Ten", | ||
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", | ||
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty" | ||
] | ||
const subset = all.slice(0, Math.max(8, Math.floor(Math.random() * all.length))) | ||
return subset.map((text, i) => ({ | ||
facade: Text3DFacade, | ||
text, | ||
font: Object.values(FONTS)[i % Object.values(FONTS).length], | ||
fontSize: randRange(0.05, 0.25), | ||
x: randRange(-1, 1), | ||
y: randRange(-1, 1), | ||
z: randRange(-1, 1), | ||
rotateZ: randRange(-0.01, 0.01), | ||
anchorX: "50%", | ||
anchorY: "50%", | ||
color: randColor(), | ||
animation: { | ||
from: { rotateY: 0 }, | ||
to: { rotateY: Math.PI * 2 }, | ||
duration: randRange(800, 3000), | ||
iterations: Infinity | ||
} | ||
})) | ||
} | ||
|
||
return <div> | ||
<Canvas3D | ||
antialias | ||
stats={stats} | ||
width={width} | ||
height={height} | ||
onBackgroundClick={() => { | ||
setTexts(randomizeText()) | ||
}} | ||
camera={{ | ||
fov: 75, | ||
aspect: width / height, | ||
x: 0, | ||
y: 0, | ||
z: 2.5 | ||
}} | ||
objects={[ | ||
{ | ||
facade: BatchedText3DFacade, | ||
children: texts, | ||
animation: { | ||
from: { rotateY: 0 }, | ||
to: { rotateY: Math.PI * 2 }, | ||
duration: 10000, | ||
iterations: Infinity | ||
} | ||
} | ||
]} | ||
/> | ||
</div>; | ||
} | ||
|
||
function randRange (min, max) { | ||
return min + Math.random() * (max - min); | ||
} | ||
function randColor() { | ||
return new Color().setHSL(Math.random(), 1, 0.5) | ||
} | ||
// function randFromArray(array) { | ||
// return array[Math.floor(Math.random() * array.length)]; | ||
// } |
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
Oops, something went wrong.