Three.js JavaScript 3D library on a WordPress site

Three.js JavaScript 3D library on a WordPress site

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 youll also have to update your style.css in order to make it look the way you want. Heres a paste of my current (entire) stylesheet: http://pastebin.com/9uUiAZhY

Good luck!

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

Im 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, its 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.

Three.js JavaScript 3D library on a WordPress site

Leave a Reply

Your email address will not be published. Required fields are marked *