-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Text to model docs #7595
Open
perminder-17
wants to merge
7
commits into
processing:dev-2.0
Choose a base branch
from
perminder-17:textToModel-docs
base: dev-2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Text to model docs #7595
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
172558e
update-docs
perminder-17 310063e
fixing
perminder-17 bf0bf87
textToModel-docs-update
perminder-17 f6eb13b
fixes textToModel docs
perminder-17 337e47e
some fixes
perminder-17 f761680
Merge branch 'dev-2.0' into textToModel-docs
perminder-17 277c533
resolving merge conflicts
perminder-17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -127,10 +127,194 @@ class Font { | |
|
||
return cmdContours.map((commands) => pathToPoints(commands, options, this)); | ||
} | ||
|
||
/** | ||
* Test | ||
*/ | ||
* | ||
* Converts text into a 3D model that can be rendered in WebGL mode. | ||
* | ||
* This method transforms flat text into extruded 3D geometry, allowing | ||
* for dynamic effects like depth, warping, and custom shading. | ||
* | ||
* It works by taking the outlines (contours) of each character in the | ||
* provided text string and constructing a 3D shape from them. | ||
* | ||
* Once your 3D text is ready, you can rotate it in 3D space using <a href="#/p5/orbitControl">orbitControl()</a> | ||
* — just click and drag with your mouse to see it from all angles! | ||
* | ||
* Use the extrude slider to give your letters depth: slide it up, and your | ||
* flat text turns into a solid, multi-dimensional object. | ||
* | ||
* You can also choose from various fonts such as "Anton", "Montserrat", or "Source Serif", | ||
* much like selecting fancy fonts in a word processor, | ||
* | ||
* The generated model (a Geometry object) can be manipulated further—rotated, scaled, | ||
* or styled with shaders—to create engaging, interactive visual art. | ||
* | ||
* @param {String} str The text string to convert into a 3D model. | ||
* @param {Number} x The x-coordinate for the starting position of the text. | ||
* @param {Number} y The y-coordinate for the starting position of the text. | ||
* @param {Number} width Maximum width of the text block (wraps text if exceeded). | ||
* @param {Number} height Maximum height of the text block. | ||
* @param {Object} [options] Configuration options for the 3D text: | ||
* @param {Number} [options.extrude=0] The depth to extrude the text. A value of 0 produces | ||
* flat text; higher values create thicker, 3D models. | ||
* @param {Number} [options.sampleFactor=1] A factor controlling the level of detail for the text contours. | ||
* Higher values result in smoother curves. | ||
* @return {p5.Geometry} A geometry object representing the 3D model of the text. | ||
* | ||
* @example | ||
* <div modernizr='webgl'> | ||
* <code> | ||
* let font; | ||
* let geom; | ||
* | ||
* async function setup() { | ||
* createCanvas(200, 200, WEBGL); | ||
* fonts = { | ||
* Anton: await loadFont('https://fonts.gstatic.com/s/anton/v25/1Ptgg87LROyAm0K08i4gS7lu.ttf') | ||
* }; | ||
* | ||
* // Create 3D geometry from text using the Anton font | ||
* geom = fonts['Anton'].textToModel("Hello", 50, 0, { sampleFactor: 2 }); | ||
* geom.clearColors(); | ||
* geom.normalize(); | ||
* } | ||
* | ||
* function draw() { | ||
* background(255); | ||
* orbitControl(); // Enables mouse control to rotate the 3D text | ||
* fill("red"); | ||
* strokeWeight(4); | ||
* scale(min(width, height) / 300); | ||
* model(geom); | ||
* | ||
* describe('A red non-extruded "Hello" in Anton on white canvas, rotatable via mouse.'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* @example | ||
* <div modernizr='webgl'> | ||
* <code> | ||
* let font; | ||
* let geom; | ||
* | ||
* async function setup() { | ||
* createCanvas(200, 200, WEBGL); | ||
* fonts = { | ||
* Anton: await loadFont('https://fonts.gstatic.com/s/anton/v25/1Ptgg87LROyAm0K08i4gS7lu.ttf'), | ||
* Montserrat: await loadFont('https://fonts.gstatic.com/s/montserrat/v29/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Ew-Y3tcoqK5.ttf'), | ||
* 'Source Serif': await loadFont('https://fonts.gstatic.com/s/sourceserif4/v8/vEFy2_tTDB4M7-auWDN0ahZJW3IX2ih5nk3AucvUHf6OAVIJmeUDygwjihdqrhxXD-wGvjU.ttf'), | ||
* }; | ||
* | ||
* // You can change fonts from here. | ||
* geom = fonts['Source Serif'].textToModel("Hello", 50, 0, { sampleFactor: 2, extrude: 5 }); | ||
* geom.clearColors(); | ||
* geom.normalize(); | ||
* } | ||
* | ||
* function draw() { | ||
* background(255); | ||
* orbitControl(); // Enables mouse control to rotate the 3D text. | ||
* fill("red"); | ||
* strokeWeight(4); | ||
* scale(min(width, height) / 300); | ||
* model(geom); | ||
* | ||
* describe('3D red extruded "Hello" in Source Serif on white, rotatable via mouse.'); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* @example | ||
* <div modernizr='webgl'> | ||
* <code> | ||
* let geom; | ||
* let fonts; | ||
* let artShader; | ||
* let lineShader; | ||
* | ||
* // Define parameters as simple variables | ||
* let words = 'HELLO'; | ||
* let font = 'Anton'; | ||
* let warp = 1; | ||
* let extrude = 5; | ||
* let palette = ["#ffe03d", "#fe4830", "#d33033", "#6d358a", "#1c509e", "#00953c"]; | ||
* | ||
* async function setup() { | ||
* createCanvas(200, 200, WEBGL); | ||
* fonts = { | ||
* Anton: await loadFont('https://fonts.gstatic.com/s/anton/v25/1Ptgg87LROyAm0K08i4gS7lu.ttf'), | ||
* Montserrat: await loadFont('https://fonts.gstatic.com/s/montserrat/v29/JTUHjIg1_i6t8kCHKm4532VJOt5-QNFgpCtr6Ew-Y3tcoqK5.ttf'), | ||
* 'Source Serif': await loadFont('https://fonts.gstatic.com/s/sourceserif4/v8/vEFy2_tTDB4M7-auWDN0ahZJW3IX2ih5nk3AucvUHf6OAVIJmeUDygwjihdqrhxXD-wGvjU.ttf'), | ||
* }; | ||
* | ||
* artShader = baseMaterialShader().modify({ | ||
* uniforms: { | ||
* 'float time': () => millis(), | ||
* 'float warp': () => warp, | ||
* 'float numColors': () => palette.length, | ||
* 'vec3[6] colors': () => palette.flatMap((c) => [red(c)/255, green(c)/255, blue(c)/255]), | ||
* }, | ||
* vertexDeclarations: 'out vec3 vPos;', | ||
* fragmentDeclarations: 'in vec3 vPos;', | ||
* 'Vertex getObjectInputs': `(Vertex inputs) { | ||
* vPos = inputs.position; | ||
* inputs.position.x += 5. * warp * sin(inputs.position.y * 0.1 + time * 0.001) / (1. + warp); | ||
* inputs.position.y += 5. * warp * sin(inputs.position.x * 0.1 + time * 0.0009) / (1. + warp); | ||
* return inputs; | ||
* }`, | ||
* 'vec4 getFinalColor': `(vec4 _c) { | ||
* float x = vPos.x * 0.005; | ||
* float a = floor(fract(x) * numColors); | ||
* float b = a == numColors - 1. ? 0. : a + 1.; | ||
* float t = fract(x * numColors); | ||
* vec3 c = mix(colors[int(a)], colors[int(b)], t); | ||
* return vec4(c, 1.); | ||
* }` | ||
* }); | ||
* | ||
* lineShader = baseStrokeShader().modify({ | ||
* uniforms: { | ||
* 'float time': () => millis(), | ||
* 'float warp': () => warp, | ||
* }, | ||
* 'StrokeVertex getObjectInputs': `(StrokeVertex inputs) { | ||
* inputs.position.x += 5. * warp * sin(inputs.position.y * 0.1 + time * 0.001) / (1. + warp); | ||
* inputs.position.y += 5. * warp * sin(inputs.position.x * 0.1 + time * 0.0009) / (1. + warp); | ||
* return inputs; | ||
* }`, | ||
* }); | ||
* } | ||
* | ||
* let prevWords = ''; | ||
* let prevFont = ''; | ||
* let prevExtrude = -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example, why in the other cases we need an explicit float and not here? It might be confusing for some users. |
||
* | ||
* function draw() { | ||
* if (words !== prevWords || prevFont !== font || prevExtrude !== extrude) { | ||
* if (geom) freeGeometry(geom); | ||
* | ||
* geom = fonts[font].textToModel(words, 0, 50, { sampleFactor: 2, extrude }); | ||
* geom.clearColors(); | ||
* geom.normalize(); | ||
* | ||
* prevWords = words; | ||
* prevFont = font; | ||
* prevExtrude = extrude; | ||
* } | ||
* | ||
* background(255); | ||
* orbitControl(); | ||
* shader(artShader); | ||
* strokeShader(lineShader); | ||
* strokeWeight(4); | ||
* scale(min(width, height) / 210); | ||
* model(geom); | ||
* describe('3D wavy with different color sets "Hello" in Anton on white canvas, rotatable via mouse.'); | ||
* } | ||
* </code> | ||
* </div> | ||
*/ | ||
textToModel(str, x, y, width, height, options) { | ||
({ width, height, options } = this._parseArgs(width, height, options)); | ||
const extrude = options?.extrude || 0; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this was adressed before, but having the notation
-1.
might not be clear for all users, thinking that why not use just-1
or-1.0
I understand that we might need to explicitly cast it like this to indicate its a float to WEBGL but it might I think it would be useful to add a note as to why we choose to notate it like this. A brief comment would be good enough I believe