Plugin Directory

Changeset 1563556


Ignore:
Timestamp:
12/28/2016 03:41:07 PM (9 years ago)
Author:
goldsounds
Message:

Added gltf_scene post type

Location:
gltf-media-type/trunk
Files:
5 added
9 edited

Legend:

Unmodified
Added
Removed
  • gltf-media-type/trunk/README.txt

    r1562420 r1563556  
    44Requires at least: 4.5
    55Tested up to: 4.7
    6 Stable tag: 1.0.1
     6Stable tag: 1.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1818== Installation ==
    1919
    20 1. Upload `gltf.php` to the `/wp-content/plugins/` directory
    21 1. Activate the plugin through the 'Plugins' menu in WordPress
     201. Unzip this plugin in the `/wp-content/plugins/` directory
     211. Activate the "glTF Media Type" plugin through the 'Plugins' menu in WordPress
    22221. Upload .gltf or .glb files via the Media browser, or via the Add Media button for posts/pages.
    23231. Embed the 3D object in posts using a shortcode, like `[gltf_model scale="1.0" url="http://mywordpresssite.com/wp-content/uploads/2016/12/model.gltf"]. Note: You may get that full URL from the Media browser by selecting the file and copying the link.
     
    3333== Changelog ==
    3434
     35= 1.1 =
     36* Add gltf_scene post type
     37
    3538= 1.0 =
    3639* First version, just basic rendering
  • gltf-media-type/trunk/admin/class-gltf-admin.php

    r1562406 r1563556  
    5252        $this->plugin_name = $plugin_name;
    5353        $this->version = $version;
    54 
    5554    }
    5655
     
    8382     * @since    1.0.0
    8483     */
    85     public function enqueue_scripts() {
    86         wp_enqueue_script( $this->plugin_name, plugin_dir_url( dirname( __FILE__ ) ) . 'js/three.min.js', null, $this->version, false );
     84    public function enqueue_scripts( $hook ) {
     85        wp_enqueue_script( 'threejs', plugin_dir_url( dirname( __FILE__ ) ) . 'js/three.min.js', null, $this->version, false );
    8786        wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/gltf-admin.js', array( 'jquery' ), $this->version, false );
    8887
     88        global $post;
     89
     90        // if we're editing a gltf_scene, load the metabox js
     91        if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && 'gltf_scene' === $post->post_type ) {
     92            wp_enqueue_script( 'gltf-admin-select-model-metabox', plugin_dir_url( __FILE__ ) . 'js/gltf-admin-select-model-metabox.js', array( 'jquery' ), $this->version, false );
     93
     94            require_once dirname( dirname( __FILE__ ) ) . '/includes/class-gltf-model-utils.php';
     95            Gltf_Model_Utils::enqueue_scripts( $this->version );
     96        }
    8997    }
    9098
  • gltf-media-type/trunk/gltf.php

    r1562420 r1563556  
    1212 * Plugin URI:        http://goldsounds.com/plugins/gltf
    1313 * Description:       A plugin to upload and render glTF 3D models in WordPress.
    14  * Version:           1.0.1
     14 * Version:           1.1
    1515 * Author:            Daniel Walmsley
    1616 * Author URI:        http://goldsounds.com
     
    5151 * admin-specific hooks, and public-facing site hooks.
    5252 */
    53 require plugin_dir_path( __FILE__ ) . 'includes/class-gltf.php';
     53require_once plugin_dir_path( __FILE__ ) . 'includes/class-gltf.php';
    5454
    5555/**
  • gltf-media-type/trunk/includes/class-gltf-activator.php

    r1562406 r1563556  
    3131     */
    3232    public static function activate() {
    33 
     33        require_once plugin_dir_path( __FILE__ ) . 'class-gltf.php';
     34        $plugin = new Gltf();
     35        $plugin->register_scene_post_type();
     36        flush_rewrite_rules();
    3437    }
    3538
  • gltf-media-type/trunk/includes/class-gltf-deactivator.php

    r1562406 r1563556  
    3131     */
    3232    public static function deactivate() {
    33 
     33        flush_rewrite_rules();
    3434    }
    3535
  • gltf-media-type/trunk/includes/class-gltf.php

    r1562406 r1563556  
    140140        // add three.js if the current post has our shortcode
    141141        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_model_render_script' ) );
     142        add_action( 'init', array( $this, 'register_scene_post_type' ) );
     143        add_action( 'add_meta_boxes_gltf_scene', array( $this, 'add_scene_metaboxes' ) );
     144        add_action( 'save_post_gltf_scene', array( $this, 'save_scene_metaboxes' ), 10, 3 );
     145        add_filter( 'single_template', array( $this, 'register_scene_template' ) );
     146
     147        // remove the sidebars before rendering a scene
     148        add_filter( 'body_class', function( $classes ) {
     149            global $post;
     150           
     151            if ( $post && $post->post_type == 'gltf_scene' && in_array( 'has-sidebar', $classes ) ) {
     152                $classes = array_diff( $classes, array('has-sidebar') );
     153            }
     154           
     155            return $classes;
     156        }, 99, 1 );
    142157    }
    143158
     
    165180    }
    166181
     182    public function register_scene_post_type() {
     183        register_post_type( 'gltf_scene',
     184            array(
     185                'labels' => array(
     186                    'name' => __( 'Scenes' ),
     187                    'singular_name' => __( 'Scene' )
     188                ),
     189                'public' => true,
     190                'has_archive' => true,
     191                'rewrite' => array('slug' => 'scenes'),
     192            )
     193        );
     194    }
     195
     196    function add_scene_metaboxes() {
     197        add_meta_box( 'gltf_select_scene_model', __( 'Select Scene Model', 'gltf-media-type' ), array( $this, 'select_scene_model_callback' ), 'gltf_scene' );
     198        // add_meta_box( 'scene_select_model', 'Select Model', 'swp_file_upload', 'podcasts', 'normal', 'default' );
     199    }
     200
     201    function select_scene_model_callback( $post ) {
     202        wp_nonce_field( basename( __FILE__ ), 'gltf_nonce' );
     203
     204        // global $post;
     205        // Get WordPress' media upload URL
     206        $upload_link = esc_url( get_upload_iframe_src( 'image', $post->ID ) );
     207
     208        // See if there's a media id already saved as post meta
     209        $main_model_id = get_post_meta( $post->ID, '_gltf_main_model', true );
     210        $main_model_scale = get_post_meta( $post->ID, '_gltf_main_model_scale', true );
     211
     212        // Get the image src
     213        $main_model_url = wp_get_attachment_url( $main_model_id );
     214
     215        // For convenience, see if the array is valid
     216        $main_model_is_set = !! $main_model_url;
     217        ?>
     218
     219        <!-- Your image container, which can be manipulated with js -->
     220        <div class="gltf-main-model-container">
     221            <?php if ( $main_model_is_set ) : ?>
     222                <div class="gltf-model" data-model="<?php echo $main_model_url ?>" data-scale="1.0" style="width: 300px; height: 300px;"></div>
     223            <?php endif; ?>
     224        </div>
     225
     226        <!-- Your add & remove image links -->
     227        <p class="hide-if-no-js">
     228            <a class="upload-main-model <?php if ( $main_model_is_set  ) { echo 'hidden'; } ?>"
     229               href="<?php echo $upload_link ?>">
     230                <?php _e('Set main 3D model') ?>
     231            </a>
     232            <a class="delete-main-model <?php if ( ! $main_model_is_set  ) { echo 'hidden'; } ?>"
     233              href="#">
     234                <?php _e('Remove this 3D model') ?>
     235            </a>
     236        </p>
     237
     238        <!-- A hidden input to set and post the chosen image id -->
     239        <input class="main-model-id" name="main-model-id" type="hidden" value="<?php echo esc_attr( $main_model_id ); ?>" />
     240
     241        <p>
     242            <label for="main-model-scale">
     243                <?php _e( 'Model Scale', 'gltf-media-type '); ?>
     244                <input class="main-model-scale" name="main-model-scale" value="<?php echo esc_attr( $main_model_scale ? $main_model_scale : 1.0 ); ?>" />
     245            </label>
     246        </p>
     247        <?php
     248        /*
     249       
     250        $gltf_stored_meta = get_post_meta( $post->ID );
     251        ?>
     252        <p>
     253            <span class="gltf-row-title"><?php _e( 'Example Radio Buttons', 'gltf-media-type' )?></span>
     254            <div class="gltf-row-content">
     255                <label for="meta-radio-one">
     256                    <input type="radio" name="meta-radio" id="meta-radio-one" value="radio-one" <?php if ( isset ( $gltf_stored_meta['meta-radio'] ) ) checked( $gltf_stored_meta['meta-radio'][0], 'radio-one' ); ?>>
     257                    <?php _e( 'Radio Option #1', 'gltf-media-type' )?>
     258                </label>
     259                <label for="meta-radio-two">
     260                    <input type="radio" name="meta-radio" id="meta-radio-two" value="radio-two" <?php if ( isset ( $gltf_stored_meta['meta-radio'] ) ) checked( $gltf_stored_meta['meta-radio'][0], 'radio-two' ); ?>>
     261                    <?php _e( 'Radio Option #2', 'gltf-media-type' )?>
     262                </label>
     263            </div>
     264        </p><?php
     265        */
     266    }
     267
     268    public function save_scene_metaboxes( $post_id, $post, $update ) {
     269        $is_autosave = wp_is_post_autosave( $post_id );
     270        $is_revision = wp_is_post_revision( $post_id );
     271        $is_valid_nonce = ( isset( $_POST[ 'gltf_nonce' ] ) && wp_verify_nonce( $_POST[ 'gltf_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
     272     
     273        // Exits script depending on save status
     274        if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
     275            return;
     276        }
     277     
     278        // Checks for input and sanitizes/saves if needed
     279        // if( isset( $_POST[ 'meta-text' ] ) ) {
     280        //  update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
     281        // }
     282        if( isset( $_POST[ 'main-model-id' ] ) ) {
     283            update_post_meta( $post_id, '_gltf_main_model', $_POST[ 'main-model-id' ] );
     284        }
     285        if( isset( $_POST[ 'main-model-scale' ] ) ) {
     286            update_post_meta( $post_id, '_gltf_main_model_scale', $_POST[ 'main-model-scale' ] );
     287        }
     288        if( isset( $_POST[ 'meta-radio' ] ) ) {
     289            update_post_meta( $post_id, 'meta-radio', $_POST[ 'meta-radio' ] );
     290        }
     291    }
     292
     293    public function register_scene_template( $single ) {
     294        global $wp_query, $post;
     295
     296        /* Checks for single template by post type */
     297        if ($post->post_type == 'gltf_scene'){
     298            return dirname( dirname( __FILE__ ) ) . '/public/templates/single-gltf_scene.php';
     299        }
     300
     301        return $single;
     302    }
     303
    167304    public function enqueue_model_render_script() {
    168305        global $post;
    169         if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'gltf_model') ) {
    170             wp_enqueue_script( 'threejs', plugin_dir_url( dirname( __FILE__ ) ) . 'js/three.min.js', null, $this->version, false );
    171             wp_enqueue_script( 'gltf-loader', plugin_dir_url( dirname( __FILE__ ) ) . 'js/loaders/GLTFLoader.js', array( 'threejs' ), $this->version, false );
    172             wp_enqueue_script( 'orbitcontrols', plugin_dir_url( dirname( __FILE__ ) ) . 'js/OrbitControls.js', array( 'threejs' ), $this->version, false );
    173             wp_enqueue_script( 'gltf-model-preview', plugin_dir_url( dirname( __FILE__ ) ) . 'js/gltf-model-preview.js', array( 'jquery', 'gltf-loader', 'orbitcontrols' ), $this->version, false );
    174         }
     306        if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'gltf_model') ) {
     307            require_once dirname( __FILE__ ) . '/class-gltf-model-utils.php';
     308            Gltf_Model_Utils::enqueue_scripts( $this->version );
     309        }
    175310    }
    176311
  • gltf-media-type/trunk/js/gltf-model-preview.js

    r1562406 r1563556  
    1212
    1313    function addLights() {
    14         var ambient = new THREE.AmbientLight( 0x101030 );
     14        // var ambient = new THREE.AmbientLight( 0x101030 );
     15        var ambient = new THREE.AmbientLight( 0xFFFFFF, 1 );
    1516        scene.add( ambient );
    1617
  • gltf-media-type/trunk/public/class-gltf-public.php

    r1562406 r1563556  
    9696         * class.
    9797         */
    98 
    99         wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/gltf-public.js', array( 'jquery' ), $this->version, false );
     98        require_once dirname( dirname( __FILE__ ) ) . '/includes/class-gltf-model-utils.php';
     99        Gltf_Model_Utils::enqueue_common_gltf_scripts( $this->version );
     100        wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/gltf-public.js', array( 'jquery', 'orbitcontrols' ), $this->version, false );
    100101
    101102    }
  • gltf-media-type/trunk/public/js/gltf-public.js

    r1562406 r1563556  
    22    'use strict';
    33
    4     /**
    5      * All of the code for your public-facing JavaScript source
    6      * should reside in this file.
    7      *
    8      * Note: It has been assumed you will write jQuery code here, so the
    9      * $ function reference has been prepared for usage within the scope
    10      * of this function.
    11      *
    12      * This enables you to define handlers, for when the DOM is ready:
    13      *
    14      * $(function() {
    15      *
    16      * });
    17      *
    18      * When the window is loaded:
    19      *
    20      * $( window ).load(function() {
    21      *
    22      * });
    23      *
    24      * ...and/or other possibilities.
    25      *
    26      * Ideally, it is not considered best practise to attach more than a
    27      * single DOM-ready or window-load handler for a particular page.
    28      * Although scripts in the WordPress core, Plugins and Themes may be
    29      * practising this, we should strive to set a better example in our own work.
    30      */
     4     window.GltfScene = function() {
     5
     6        var container, camera, scene, renderer, controls;
     7
     8        function addCamera() {
     9            camera = new THREE.PerspectiveCamera( 75, container.offsetWidth / container.offsetHeight, 1, 2000 );
     10            camera.position.set(0, 2, 3);
     11            scene.add( camera );
     12        }
     13
     14        function addLights() {
     15            // var ambient = new THREE.AmbientLight( 0x101030 );
     16            var ambient = new THREE.AmbientLight( 0xFFFFFF, 1 );
     17            scene.add( ambient );
     18
     19            var directionalLight = new THREE.DirectionalLight( 0xffeedd );
     20            directionalLight.position.set( 0, 0, 1 );
     21            scene.add( directionalLight );
     22        }
     23
     24        function addControls() {
     25            controls = new THREE.OrbitControls( camera, renderer.domElement );
     26            controls.userPan = false;
     27            controls.userPanSpeed = 0.0;
     28            controls.maxDistance = 5000.0;
     29            controls.maxPolarAngle = Math.PI * 0.495;
     30            controls.autoRotate = false;
     31            // controls.autoRotateSpeed = -10.0;
     32        }
     33
     34        function addRenderer() {
     35            renderer = new THREE.WebGLRenderer({antialias:true});
     36            renderer.setClearColor( 0x222222 );
     37            renderer.setPixelRatio( window.devicePixelRatio );
     38            renderer.setSize( container.offsetWidth, container.offsetHeight );
     39        }
     40
     41        function addLoadingLogger() {
     42            var manager = new THREE.LoadingManager();
     43            manager.onProgress = function ( item, loaded, total ) {
     44                console.log( item, loaded, total );
     45            };
     46        }
     47
     48        function loadModel( modelUrl, modelScale ) {
     49            THREE.GLTFLoader.Shaders.removeAll(); // remove all previous shaders
     50            var loader = new THREE.GLTFLoader;
     51            loader.load( modelUrl, function( data ) {
     52                var object = data.scene;
     53                object.scale.set(modelScale, modelScale, modelScale);
     54
     55                var animations = data.animations;
     56                if ( animations && animations.length ) {
     57                    mixer = new THREE.AnimationMixer( object );
     58                    for ( var i = 0; i < animations.length; i ++ ) {
     59                        var animation = animations[ i ];
     60                        mixer.clipAction( animation ).play();
     61                    }
     62                }
     63
     64                scene.add( object );
     65            } );
     66        }
     67
     68        function render() {
     69            renderer.render( scene, camera );
     70        }
     71
     72        function animate() {
     73            requestAnimationFrame( animate );
     74            THREE.GLTFLoader.Animations.update();
     75            THREE.GLTFLoader.Shaders.update(scene, camera);
     76            render();
     77            controls.update();
     78        }
     79
     80        function initScene( id ) {
     81            container = jQuery( '#'+id ).get(0);
     82
     83            scene = new THREE.Scene();
     84
     85            addLights(); // LIGHTS!
     86            addCamera(); // CAMERA!
     87            addRenderer(); // ACTION! :)
     88            addControls();
     89            addLoadingLogger();
     90
     91            container.appendChild( renderer.domElement );
     92        }
     93
     94        return {
     95            render: function( id, scene ) {
     96                console.log("rendering "+id+": "+JSON.stringify( scene ));
     97
     98                initScene( id );
     99                loadModel( scene.main_model, scene.main_model_scale );
     100                animate();
     101            }
     102        }
     103     }();
    31104
    32105})( jQuery );
Note: See TracChangeset for help on using the changeset viewer.