最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I include gl-matrix? - Stack Overflow

programmeradmin1浏览0评论

What I did:

Downloaded master branch from here: Put src folder into my project's folder. Included gl-matrix-manifest.js from there. Tried this:

var mvMatrix = mat4.create();

Result:

var mvMatrix = mat4.create();
ReferenceError: mat4 is not defined

Okay, let's indlude mat4 directly. Included. Result:

var out = new GLMAT_ARRAY_TYPE(16);
ReferenceError: GLMAT_ARRAY_TYPE is not defined

Okay, maybe it needs common.js:

var x = axis[0], y = axis[1], z = axis[2],
TypeError: axis is undefined

WTF, included all other files from src folder (common, vectors,matrices,quat):

var x = axis[0], y = axis[1], z = axis[2],
TypeError: axis is undefined

(same)

How do I include propertly? In particular I need mat4 and vec4.

What I did:

Downloaded master branch from here: https://github.com/toji/gl-matrix Put src folder into my project's folder. Included gl-matrix-manifest.js from there. Tried this:

var mvMatrix = mat4.create();

Result:

var mvMatrix = mat4.create();
ReferenceError: mat4 is not defined

Okay, let's indlude mat4 directly. Included. Result:

var out = new GLMAT_ARRAY_TYPE(16);
ReferenceError: GLMAT_ARRAY_TYPE is not defined

Okay, maybe it needs common.js:

var x = axis[0], y = axis[1], z = axis[2],
TypeError: axis is undefined

WTF, included all other files from src folder (common, vectors,matrices,quat):

var x = axis[0], y = axis[1], z = axis[2],
TypeError: axis is undefined

(same)

How do I include propertly? In particular I need mat4 and vec4.

Share Improve this question edited Apr 12, 2013 at 20:13 Dmitry asked Apr 10, 2013 at 16:14 DmitryDmitry 5632 gold badges5 silver badges20 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 20

Once the source is specified correctly, I still couldn't directly call mat4.

I had to type "glMatrix.mat4" instead.

Just include /dist/gl-matrix-min.js or /dist/gl-matrix.js :)

Add this cdn link in head

<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.8.1/gl-matrix-min.js"
        integrity="sha512-zhHQR0/H5SEBL3Wn6yYSaTTZej12z0hVZKOv3TwCUXT1z5qeqGcXJLLrbERYRScEDDpYIJhPC1fk31gqR783iQ=="
        crossorigin="anonymous" defer>
</script>
  1. Download from here
  2. Extract and take gl-matrix-min.js
  3. In your index.html file <script src="gl-matrix-min.js"></script>
  4. Now try var mvMatrix = glMatrix.mat4.create();

The next two examples shows a modern way how to include glMatrix and Box2D-WASM as ES6 modules using import maps.

Playground that shows how to initialize Box2D-WASM to create and print a Box2D vector: https://plnkr.co/edit/BGNYcIJRiJXpd9N4?preview

The next playground shows how to use glMatrix only:

    <!-- Since import maps are not yet supported by all browsers, its is
        necessary to add the polyfill es-module-shims.js -->
    <script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/es-module-shims.min.js"></script>

    <script type="importmap">
        {
            "imports": {
                "box2d-wasm": "https://unpkg.com/[email protected]/dist/es/Box2D.js",
                "gl-matrix": "https://cdn.jsdelivr.net/npm/[email protected]/+esm"
            }
        }
    </script>

    <script type="module">
        import { glMatrix, mat4, vec3 } from "gl-matrix";

        console.log(glMatrix.toRadian(45));

        const v = vec3.fromValues(1, 2, 3);
        console.log(v);

        const m = mat4.create();
        console.log(m);
    </script>

发布评论

评论列表(0)

  1. 暂无评论