When WebGL loads the model, the debugging result of javascript Parsing is converted to JSON to view and confirm the array structure.

Source: Internet
Author: User

When WebGL loads the model, the debugging result of javascript Parsing is converted to JSON to view and confirm the array structure.

Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.


So long as a large title is actually a subject, and arrays in javascript are converted into JSON strings.

Why? I read it from a text file and want to convert it into text. What is the bend ?!


There is a problem. javascript, as a language, its array is also a memory organizational structure. It can be printed out, but it is a pile of puttles)

If you want to see the data and the organizational structure, you can find a plaintext format that can describe the array structure. In javascript, JSON must be the first choice! As for other languages, I think JSON is also the first choice. Do not believe you have to move the definition of JSON to see if it is true!


Have you confirmed it?

Okay. Now you agree with this idea like me. Let's take a look at how to convert arrays in javascript into JSON strings.

The following example uses WebGL to process a cube. The following part may be helpful to my friends studying WebGL, at least because I am still studying it, what is recorded here can only be regarded as a stage result. It can be called a milestone in cmme, but it can be called a sprint in Agile development scrum.

Model file,

mtllib changfangti.mtl## object Box001#v  -3000.0000 0.0000 2000.0000v  -3000.0000 0.0000 -2000.0000v  3000.0000 0.0000 -2000.0000v  3000.0000 0.0000 2000.0000v  -3000.0000 2800.0000 2000.0000v  3000.0000 2800.0000 2000.0000v  3000.0000 2800.0000 -2000.0000v  -3000.0000 2800.0000 -2000.0000# 8 verticesvn 5.0000 -1.0000 -0.0000vn 0.0000 1.0000 -0.0000vn 0.0000 0.0000 1.0000vn 1.0000 0.0000 0.0006vn 0.0000 0.0000 -1.0000vn -1.0000 0.0000 -0.0006# 6 vertex normalsvt 0.9995 0.0005 0.0005vt 0.9989 0.9995 0.0005vt 0.0005 0.9995 0.0005vt 0.0011 0.0005 0.0005vt 0.0005 0.0005 0.9995vt 0.9989 0.0005 0.9995vt 0.9995 0.9995 0.9995vt 0.0011 0.9995 0.9995vt 0.0005 0.0005 0.0005vt 0.9989 0.0005 0.0005vt 0.0005 0.0005 0.9989vt 0.9995 0.0005 0.9995vt 0.0005 0.9995 0.9989vt 0.9989 0.9995 0.9995vt 0.0005 0.9995 0.9995vt 0.0005 0.0005 0.0011vt 0.9995 0.9995 0.0005vt 0.0005 0.9995 0.0011# 18 texture coordsg Box001usemtl 07___Default11s 2f 1/1/1 2/2/1 3/3/1 f 3/3/1 4/4/1 1/1/1 usemtl 07___Defaults 4f 5/5/2 6/6/2 7/7/2 f 7/7/2 8/8/2 5/5/2 usemtl 09___Defaults 8f 1/9/3 4/10/3 6/2/3 f 6/2/3 5/3/3 1/9/3 usemtl 10___Defaults 16f 4/11/4 3/12/4 7/7/4 f 7/7/4 6/13/4 4/11/4 usemtl 11___Defaults 32f 3/5/5 2/6/5 8/14/5 f 8/14/5 7/15/5 3/5/5 usemtl 12___Defaults 64f 2/16/6 1/1/6 5/17/6 f 5/17/6 8/18/6 2/16/6 # 12 faces
The above model file is exported from 3 dmax. the obj format model has four parts that we care about. The parts starting with v, vt, vn, and f can also be concerned about usemtl, but it has not been considered yet, only one material is specified.

There is a very good algorithm here, but with a general idea that has not been put into practice. Maybe it will be done in the near future when in-depth details are done, this is also a tough guy to think about. Let's look forward to it.


Next, the main character of this article is coming to the market.

The JSON class is supported only in Versions later than ECMAScript 5.

    var lines = data.split("\n");    alert(JSON.stringify(lines));

[    "mtllib 11.mtl",    "",    "#",    "# object 006",    "#",    "",    "v  34.1000 161.8999 104.3000",    "v  0.0000 162.5000 108.2000",    "v  0.0000 156.6999 113.3000",    "v  35.3000 157.0000 107.6999",    "v  55.5000 157.0000 97.2999",    "v  66.6000 157.1000 91.5999",    "v  64.6999 161.9999 88.4999",    "v  53.3999 161.9999 94.2999",    "v  91.7998 157.0000 66.6000",    "v  89.2999 161.6999 64.1000",    "v  107.8999 157.1000 34.6000",    "v  104.6999 161.6999 33.8999",    "v  109.3999 161.5000 0.3999",    "v  112.5999 156.2999 0.3999",    "v  0.0000 152.5999 112.9999",    "v  56.2999 152.9000 97.9000",    "v  97.9999 152.7000 54.6999",    "v  112.4000 152.9000 0.3999",    "v  0.0000 144.5999 119.1999",    "v  59.8000 142.9000 103.5000",    "v  103.4000 143.1998 58.4999",    "v  0.0000 139.0999 124.9000",    "v  62.9999 138.8999 108.2000",    "v  108.2000 138.9999 62.0999",    "v  118.6999 143.0999 0.3999",    "v  124.7000 138.6999 0.3999",    "v  210.6000 9.8997 271.2999",    "v  215.2999 42.7000 270.1999",    "v  0.0000 42.8000 270.9000",    "v  0.0000 10.7000 271.1999",    "v  0.0000 85.7999 127.4999",    "v  65.1000 85.6999 110.6999",    "v  0.0000 272.6000 67.9000",    "v  32.6000 271.1999 57.9999",    "v  36.8999 292.2999 64.7999",    "v  0.0000 293.0000 74.2999",    "v  56.7999 270.1999 32.2000",    "v  63.1000 290.6000 38.1999",    "v  65.0000 270.0000 0.3999",    "v  73.9000 291.2000 0.3999",    "v  61.5000 308.2999 35.3000",    "v  69.5999 309.7000 0.3999",    "v  35.1999 308.8999 61.7999",    "v  0.0000 311.3999 70.2999",    "v  51.3000 351.7000 0.3999",    "v  45.2999 350.3999 25.4998",    "v  25.0000 350.7999 45.6000",    "v  0.0000 352.6000 51.6000",    "v  77.6999 172...2 997/2497/4042 ",    "f 769/2284/4043 997/2497/4043 996/2498/4043 ",    "f 769/2284/4027 996/2498/4027 998/2499/4027 ",    "f 769/2284/4046 998/2499/4046 999/2500/4046 ",    "f 769/2284/4029 999/2500/4029 1000/2501/4029 ",    "f 769/2284/4027 1000/2501/4027 1001/2502/4027 ",    "f 769/2284/4029 1001/2502/4029 1002/2503/4029 ",    "f 769/2284/4029 1002/2503/4029 1004/2504/4029 ",    "f 769/2284/4046 1004/2504/4046 1003/2505/4046 ",    "f 769/2284/4027 1003/2505/4027 1010/2506/4027 ",    "f 769/2284/4027 1010/2506/4027…
The above is the result of formatting with the json formatting online tool. It looks very clear!

For json, see the following description:

JSON is a data structure that replaces XML. Compared with xml, JSON is smaller but not less descriptive. because of its small size, data transmission over the network will reduce more traffic and speed up the process,

So what is JSON?

JSON is a string, but the elements are marked with specific symbols.

{} Double brackets indicate objects

[] Brackets indicate Arrays

"" Double quotation marks are attributes or values.

: Colon indicates that the latter is the value of the former (this value can be a string, number, or another array or object)

So {"name": "Michael"} can be understood as an object containing name Michael.

[{"Name": "Michael" },{ "name": "Jerry"}] indicates an array containing two objects.

Of course, you can also use {"name": ["Michael", "Jerry"]} to simplify the previous section. This is an object with a name array.

Ps: there are still some misunderstandings. Why can't {name: 'json'} pass the test,

That's because the latest regulations on the JSON Official Website

If it is a string, it is best to use double quotation marks for both the key and value, so the above Code is {"name": "json "}

Do not refute the definition of the official website.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.