Three.js JavaScript 3D library on a WordPress site

I wanted to use Three.js JavaScript 3D library on my WordPress site, so I tried the three.min.js in the:

  1. body of a post

    <script src="/three.min.js"></script>

    Read More
  2. then the footer

    <script type='text/javascript' src='/three.min.js'></script>

  3. and now the Pinboard: Theme Functions (functions.php) file

    function pinboard_enqueue_scripts() {
    wp_register_script('three.min', '/three.min.js', true);
    wp_enqueue_script( 'three.min' );
    }
    add_action( 'wp_enqueue_scripts', 'pinboard_enqueue_scripts' );

Unfortunately the cube from Mr.doob’s page is not showing up on my Cube page.

Your help in running the Three.js on my WordPress site would be greatly appreciated.

Thank you.

Related posts

Leave a Reply

2 comments

  1. Well that has also a lot to do with CSS ^^ I do the same at http://lizkats.com, maybe get some inspiration there.
    In the wordpress theme editor I edited the header file (header.php) to include both the library and the script there. I used an absolute URL (i.e. I cheated, maybe you have more patience and find a better way) and inserted libraries and script like following:

    </head>
    <body <?php body_class(); ?>>
        <!-- Here (after head & body in Heaper.php) starts the stuff I added myself -->
        <div id="3dcontainer" style="position: fixed; z-index: -1;"></div>
        <div id="page" class="hfeed site">
        <header id="masthead" class="site-header" role="banner" style="overflow: visible;">
    
    
        <script src="http://lizkats.com/wp-content/fromGame/three.js"></script>
        <script src="http://lizkats.com/wp-content/fromGame/models/modelMeerkatPoseWeb.js"></script>
    <script>
    
            var container, stats;
    
            var camera, scene, renderer, objects;
    
            var clock = new THREE.Clock();
    
    
            function init() {
    
                container = document.getElementById('3dcontainer');
    
                camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.5, 200 );
                camera.position.set( 0.4, 0.42, 0.9 );
    
                scene = new THREE.Scene();
    
                // ...
    
                // Renderer
    
                renderer = new THREE.WebGLRenderer( { alpha: true } );
                renderer.setClearColor( 0x453326, 1);
                renderer.setSize( window.innerWidth, window.innerHeight);
    
                container.appendChild( renderer.domElement );
    
                animate();
            }
    
            init();
    
            function animate() {
    
                requestAnimationFrame( animate );
    
    
                render();
            }
    
            function render() {
    
                var delta = clock.getDelta();
    
                renderer.render( scene, camera );
            }
    </script>
    

    The 3dcontainer is where I insert the canvas object.

    Further you’ll also have to update your style.css in order to make it look the way you want. Here’s a paste of my current (entire) stylesheet: http://pastebin.com/9uUiAZhY

    Good luck!

  2. This is popular search, so I decided to write little help for feature readers.

    I’m assuming that you have properly installed and working WordPress.

    Installation without bundlers

    The simplest way to make example three.js cube working on your WordPress home page, following three.js docs:

    1. Depending on your WordPress configuration open index.php / page.php

    2. First we need to import library, there are two common ways to do that via : npm or cdn.

    CDN:

    <script type="module">
    // Find the latest version by visiting https://cdn.skypack.dev/three.
    import * as THREE from 'https://cdn.skypack.dev/three@0.137.5';
    
    const scene = new THREE.Scene();
    </script>
    

    NPM (check Downloading and installing Node.js and npm if needed):

    Open your terminal and run npm install three. This will create node_modules folder in your directory, which contains library. Example provided in three.js docs, assumes that you will bundle html. Although we will import our library without bundling for the needs of this question.

    Open functions.php and enqueue library:

    wp_enqueue_script( 'three-min', get_template_directory_uri() . '/node_modules/three/build/three.min.js', array(), null, false );
    

    Make a notice, it’s placed in <head> so it will be run before our script.

    3. Add the basic cube to the script:

    <script type="module">
    
    // Find the latest version by visiting https://cdn.skypack.dev/three.
    
    import * as THREE from 'https://cdn.skypack.dev/three@0.137.5';
    
    const scene = new THREE.Scene();
                const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
    
                const renderer = new THREE.WebGLRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );
    
                const geometry = new THREE.BoxGeometry();
                const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
                const cube = new THREE.Mesh( geometry, material );
                scene.add( cube );
    
                camera.position.z = 5;
    
                function animate() {
                    requestAnimationFrame( animate );
    
                    cube.rotation.x += 0.01;
                    cube.rotation.y += 0.01;
    
                    renderer.render( scene, camera );
                };
    
                animate();
    
    
    </script>
    

    for NPM type=”module” and import * as THREE from 'https://cdn.skypack.dev/three@0.137.5'; is not needed. Because we have imported it already using UMD.

    I might update it in the future for Webpack bundler integration.