Breaking apart a plane geometry using Three.js shatter/explode effects

Struggling to achieve a glass shattering effect in Three.js using Tween and plane geometry. The issue arises when the mesh/geometry fails to update with the tween after the initial rendering. It seems that calling the function "shatter()" before the first render demonstrates that the tween is functional, but only for one cycle. Subsequently, post-render, the mesh/geometry ceases to update.

The current code looks like this:

<html> 
<head> 
    <title>My first Three.js app</title> 
    <style> 
        body { margin: 0; } canvas { width: 100%; height: 100% } 
    </style> 
</head> 
    <body> 
        <script src="js/three.min.js"></script> 
        <script src="js/ExplodeModifier.js"></script> 
        <script src="js/tween.min.js"></script> 
        <script> 
            var scene = new THREE.Scene(); 
            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); 
            var renderer = new THREE.WebGLRenderer(); 
            renderer.setSize( window.innerWidth, window.innerHeight ); 
            document.body.appendChild( renderer.domElement ); 

            var geometry = new THREE.PlaneGeometry(5, 5, 8,8); // create plane
            var material = new THREE.MeshBasicMaterial( { color: 0xB20000, wireframe: true});

            var mesh = new THREE.Mesh( geometry, material );
            scene.add( mesh );

            camera.position.z = 5;

            function shatter()
            {
                TWEEN.removeAll(); 

                var explodeModifier = new THREE.ExplodeModifier();
                explodeModifier.modify( geometry );

                var verticeA = 0;
                var verticeB = 1;
                var verticeC = 2;

                var test = geometry.vertices.length;

                geometry.vertices[verticeA].translateX(1);

                for(var i = 0; i < (geometry.vertices.length / 256); i++)
                {
                    TWEEN.removeAll();

                    var pos = new THREE.Vector3();

                    var final = Math.random();

                    pos.x = final;
                    pos.y = final;
                    pos.z = final;

                    new TWEEN.Tween(geometry.vertices[verticeA])
                    .to( { x: pos.x, y: pos.y, z: pos.z }, 2000 )
                    .easing( TWEEN.Easing.Exponential.InOut )
                    //.onUpdate( function() { })
                    .start();

                    new TWEEN.Tween(geometry.vertices[verticeB])
                    .to( {  x: pos.x, y: pos.y, z: pos.z }, 2000 )
                    .easing( TWEEN.Easing.Exponential.InOut )
                    .start();

                    new TWEEN.Tween(geometry.vertices[verticeC])
                    .to( { x: pos.x, y: pos.y, z: pos.z }, 2000 )
                    .easing( TWEEN.Easing.Exponential.InOut )
                    .start();

                    verticeA += 3;
                    verticeB += 3;
                    verticeC += 3;
                }
            };

            var render = function () {
                TWEEN.update();

                requestAnimationFrame( render );

                renderer.render(scene, camera);
            };

            render();
            shatter();
        </script> 
    </body> 
</html>

Answer №1

Ensure to indicate any modifications made to vertices coordinates by activating geometry.verticesNeedUpdate=true.

For smooth animation, remember to set this parameter as true following each tween update, whether in the render or update callback function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating through File URLs to access desired folders or directories

I have a group of users connected to an internal network, each with a mapped drive to a server (E:). All users are running Windows 7 and use Firefox as their browser. To allow access to MySQL files using PHP, I have set up XAMPP on the server. Currently, ...

Having difficulty displaying elements from two arrays at particular intervals

I am currently working with two arrays that have the same number of items in each. My goal is to print these items in intervals within the console. The desired output would look something like this: 1 Bruce Wayne 45 Then, after a one-second interval, it s ...

What is the method for passing an element object instead of an id in JSXGraph?

I'm attempting to modify var brd2 = JXG.JSXGraph.initBoard('box', {boundingbox: [-8.75, 2.5, 8.75, -2.5]}); to read as var brd2 = JXG.JSXGraph.initBoard(this.$('#box'), {boundingbox: [-8.75, 2.5, 8.75, -2.5]}); Due to the creat ...

Combining NodeJS with PHP: A Seamless Integration

They say NodeJS is perfect for creating real-time chat applications and I'm eager to add a chat feature to my website. Right now, I have the design ready and need to work on the back-end code. However, when I use socket.io + express, things don' ...

Discovering the Javascript Code Executing on a <span> element with the Help of Chrome Inspector or Firebug

I have encountered a situation where a website is dynamically generating information into <span></span> tags using jQuery. The span has a class of "Price" and an id corresponding to a Product Code, with the data being pulled from a JSON data sh ...

Tips on dividing a div into two separate pages when converting to PDF

I am working on an asp.net MVC project and I need to convert a HTML div into a PDF with two separate pages. Here is an example of my code: HTML Code <div class="row" id="mycanvas"> <p>This is the first part of my content.</p> <p ...

reviewing the content of an element upon mouse hover

I'm having trouble setting up a mouse enter event that should trigger if the element contains specific text. However, for some reason, it seems like the :contains parameter is not being recognized. Here is the HTML: <div class="sample">red< ...

What causes the discrepancy in the expiresIn value of my JWT when it is transmitted from the server to the front-end?

After setting the token expiry date on the server, I decided to log out the value to double-check it. However, upon checking the value on my React front-end, I noticed a significant change in the value received compared to what was sent from the server. Is ...

Is incrementing x by 1 equivalent to x + 1?

I have a very basic angular application. It simply increases the value by 1 when clicked on using ng-click. Take a look at JSFiddle <div ng-app="app" ng-controller="ctrl"> <div ng-click="hello=hello+1">THIS WORKS ON CLICK: {{hello}}</d ...

What is the best way for Cucumber to move on to the next annotation only after ensuring all async requests from the previous one have finished processing?

I am looking to set up a basic test using Selenium and Cucumber for logging into my web application and confirming that the main page is displayed correctly. Currently, all three tests are returning true even before the page is fully loaded. The issue ar ...

Performing queries using the ORM Sequelize to fetch data from two separate tables simultaneously during a single page

I have encountered a challenge while working on my project. I need to display data from two different tables on one page (page.hbs). My tech stack includes Node.js ORM Sequelize, MySQL database, and Handlebars for templating. However, I am facing difficult ...

Can React-Select be utilized in the browser directly from the CDN?

Is it possible to utilize react-select directly in the browser without using bundlers nowadays? The most recent version that I could find which supports this is 2.1.2: How to import from React-Select CDN with React and Babel? In the past, they provided r ...

pg-promise received an error due to an incorrect parameter being passed in for options

I have been working on transitioning my files from utilizing the pg package to the pg-promise package. Initially, everything was functioning correctly with the original pg solution I had in place. However, upon switching to pg-promise and referencing the d ...

Struggling to display AJAX GET results on my webpage, although they are visible in the Google Element Inspector

I'm working on a basic invoice page where I need to populate a dropdown box from my MySQL database. The issue I'm facing is that when I select an item, the description box doesn't get prepopulated as expected. I've checked in the networ ...

Vue - Display components next to sidebar

Having an issue with the vue-sidebar-menu. The sidebar is appearing over the components instead of beside them. Here is a screenshot of the problem: https://i.sstatic.net/cVgI6.jpg <template> <div id="app"> <sidebar-menu :menu="menu" ...

Issue with Angular failing to identify jQuery after transferring the dependency from package.json to bower.json

Initially, my project included angular, angular-bootstrap, and jquery in the package.json file, with everything being compiled using browserify. // package "dependencies": { "angular": "~1.4.6", "angular-bootstrap": "~0.12.2", "jquery": "~2.1. ...

How to make an input blur in Angular 2 when a button is clicked?

Is there a way to blur an input field by pressing the return button on a mobile native keyboard? Here is an example: <input type="text" #search> this.search.blur() //-- unfocus and hide keyboard ...

Here's a unique version: "Strategies for effectively closing a modal when moving to a

I'm currently working with react-router-dom and material UI modal, and I am looking for a way to automatically hide the modal whenever the user navigates to another page. Here is my App component: const App = () => ( <BrowserRouter> &l ...

Struggling to get the knockout js remove function to function properly within a nested table structure

I've been encountering issues while trying to eliminate the formulation elements with the 'Delete comp' link. I can't seem to figure out why it's not functioning as expected. Moreover, the 'Add another composition' link o ...

Avoid displaying hover effects on touch-enabled devices

Is there a way to prevent touch devices from triggering the CSS hover state without converting all hover styles into mouseover listeners? I am working on an application that needs to support both touch and pointer input. However, certain hover styles don& ...