javascript - Textgeometry as object attribute -


i'm working on project create web components 3 js. in order have create objects different components. have several problems create objects have text attribute. add mesh of text in three.group() object in order make more accessible , usable if need add modifications.

var button = {     init : function(text, textsize, textfont, textmaterial, other attributes ...) {         this._geo = new three.group();         this.createtextdesign(text, textsize, textfont, textmaterial);         this._textmesh = this.createtext()         this._geo.add(this._textmesh);          ...     },      createtextdesign : function() {        this._text = text;        this._textmaterial = textmaterial;        this._textfont = textfont;         if (textsize == "lg") {             this._textsize = 2.5;        } else if (textsize == "sm"){             this._textsize = 1.5;        } else if (textsize == "xs"){             this._textsize = 1;        } else {             this._textsize = 2;     },      createtext : function(){         var text = null;         var fontloader = new three.fontloader();         fontloader.load('fonts/' + this._textfont + '.typeface.json', function(font) {         var textgeometry = new three.textgeometry(this._text, {             font: font,             size: this._textsize,             height: this._textsize         });         fontloader.load();          text = new three.mesh(textgeometry, this._textmaterial);         });         return text;     },      getgroup : function(){         return this._geo;     },      ... }; 

the problem when try instantiate object following generic code (instantiate + display object) :

<!doctype html> <html lang="en">     <head>         <title>three.js - ascii effect</title>         <meta charset="utf-8">         <style>             body {                 font-family: monospace;                 background-color: #f0f0f0;                 margin: 0px;                 overflow: hidden;             }         </style>     </head>     <body>         <script src="js/build/three.js"></script>         <script src="components/button.js"></script>         <script>             var scene = new three.scene();             var camera = new three.perspectivecamera( 75, window.innerwidth/window.innerheight, 0.1, 1000 );              var renderer = new three.webglrenderer();             renderer.setsize( window.innerwidth, window.innerheight );             document.body.appendchild( renderer.domelement );              var mat2 = new three.meshbasicmaterial( {color: 0xff0000} );             var mat3 = new three.meshbasicmaterial( {color: 0x00ff00} );             var mat4 = new three.meshbasicmaterial( {color: 0x0000ff} );              var button = object.create(button);             button.init("test", "lg", "optimer_regular", mat2, mat3, mat4);              scene.add(button.getgroup());              camera.position.z = 50;              var render = function () {                 requestanimationframe( render );                 renderer.render(scene, camera);             };              render();         </script>     </body> </html> 

i following error :

three.object3d.add: object not instance of three.object3d.         nullthree.js:10826:5 .add()                                                                three.js:10826 button.init()                                                         button.js:10 <anonymous>                                                           test4.html:31 

with button.js:10 being line add this._textmesh this._geo.

i have tried different techniques create text meshes without using textloader (for example : http://blog.andrewray.me/creating-a-3d-font-in-three-js/) text not display, , don't have error message in console...

does have idea on how solve problem ? in advance !

the error thrown because in fact not returning mesh, null createtext()-method.

a simplified version of code looks this:

function createtext() {   var text = null;    fontloader.load(fonturl, function(font) {     text = createtextmesh(font);   })    return text; } 

now, need aware how async functions in javascript work. in example, 3 things happen when call createtext():

  1. variable text initialized null
  2. the request load font started
  3. the value of text (is null @ point) returned.

why that? http-request takes (in terms of speed of code) ages return result. callback-function receives loaded font called long time after value has been returned. still creates mesh then, result isn't used anymore return did happen.

there options around problem, in case need wait until font loaded before adding object group.

you try this:

function addtextmesh(parent) {   fontloader.load(fonturl, function(font) {     parent.add(createtextmesh(font));   }) } 

or make sure font loaded before of happens.

one more note: don't know use-case, if need render lot of text, using text-meshes waste of resources (because of huge number of vertices , computing involved in rendering them).

it might idea @ other alternatives rendering text:


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -