[英]Forge function generateTexture()
In the following example, there is a function called generateTexture()
. 在以下示例中,有一个名为generateTexture()
的函数。
Is it possible to draw text (numbers) into the pixel array? 是否可以在像素阵列中绘制文本(数字)? Or is it possible to draw text (numbers) on top of that shader? 或者是否可以在该着色器上绘制文本(数字)?
Our goal is to draw a circle with a number inside of it. 我们的目标是绘制一个带有数字的圆圈。
https://forge.autodesk.com/blog/using-dynamic-texture-inside-custom-shaders https://forge.autodesk.com/blog/using-dynamic-texture-inside-custom-shaders
UPDATE: We noticed that each circle can't use a unique generateTexture()
. 更新:我们注意到每个圆圈都不能使用唯一的generateTexture()
。 The generateTexture()
result is used by every single one of them. generateTexture()
结果由它们中的每一个使用。 The only thing that can be customized per object is the color, plus what texture is used. 每个对象唯一可以自定义的是颜色,以及使用的纹理。
We could create a workaround for this, which is to generate every texture from 0 to 99, and to then have each object choose the correct texture based on the number we want to display. 我们可以为此创建一个解决方法,即生成从0到99的每个纹理,然后让每个对象根据我们想要显示的数字选择正确的纹理。 We don't know if this will be efficient enough to work properly though. 我们不知道这是否足够有效以便正常工作。 Otherwise, it might have to be 0 to 9+ or something in that direction. 否则,它可能必须是0到9+或那个方向的东西。 Any guides on our updated question would be really appreciated. 任何关于我们更新的问题的指南都将非常感激。 Thanks. 谢谢。
I am able to successfully display text with the following code, simply replace generateTexture() by generateCanvasTexture() in the sample and you should get the result below: 我能够使用以下代码成功显示文本,只需在示例中用generateCanvasTexture()替换generateTexture() ,您应该得到以下结果:
const generateCanvasTexture = () => {
const canvas = document.createElement("canvas")
const ctx = canvas.getContext('2d')
ctx.font = '20pt Arial'
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillText(new Date().toLocaleString(),
canvas.width / 2, canvas.height / 2)
const canvasTexture = new THREE.Texture(canvas)
canvasTexture.needsUpdate = true
canvasTexture.flipX = false
canvasTexture.flipY = false
return canvasTexture
}
It is possible but you would need to implement it yourself. 这是可能的,但你需要自己实现它。 Shaders are a pretty low level feature so there is no way to directly draw a number or a text, but you can convert a given character into its representation as a 2d pixel array. 着色器是一个相当低级别的功能,因此无法直接绘制数字或文本,但您可以将给定字符转换为其表示为2d像素数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.