What is the method for generating ocean waves using three.js?

I attempted to create ocean waves using three.js, but when I tried running it on a web browser, all I saw was a blank white screen.

https://i.sstatic.net/dhEsY.png

The code snippet I utilized for generating the ocean waves is:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1b071d0a0a2f5f415e5b5c">[email protected]</a>/build/three.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aeef2e8ffffdaaab4abaea9">[email protected]</a>/examples/js/controls/OrbitControls.js"></script> 
        <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51253923343411617f606562">[email protected]</a>/examples/js/objects/Water.js"></script>   
        <script>


            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
            camera.position.set( 0, 0, 50 );
            camera.lookAt( 0, 0, 0 );
            

            const renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );

            
            const WaterGeometry = new THREE.PlaneGeometry( 45, 45 );
            const texture = new THREE.TextureLoader().load( 'https://i.imgur.com/mK9cHLq.jpeg' );
            const material = new THREE.MeshBasicMaterial( { map: texture, side: THREE.DoubleSide } );           
            const controls = new THREE.OrbitControls( camera, renderer.domElement );
            const params = {
                color: '#ffffff',
                scale: 4,
                flowX: 1,
                flowY: 1
            };


                water = new THREE.Water( WaterGeometry, material, {
                    color: params.color,
                    scale: params.scale,
                    flowDirection: new THREE.Vector2( params.flowX, params.flowY ),
                    textureWidth: 1024,
                    textureHeight: 1024
                } );



            controls.update();

            scene.add( water );

            function animate() 
            {
                requestAnimationFrame( animate );

                water.rotation.x += 0.00;
                water.rotation.y += 0.00;
                water.rotation.z += 0.01;

                renderer.render( scene, camera );
            };

            animate();
            
        </script>
    </body>
</html>

Where did I go wrong? All I see now is a slowly rotating black square.

Thank you in advance for any feedback and advice on resolving this issue!

https://i.sstatic.net/rebim.png

Answer №1

Your imports seem to be causing some issues. You may want to consider the following structure:

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3e7fbf1f1c4d4e4fad5dad9">[email protected]</a>/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0266190b170707520607080b">[email protected]</a>/examples/js/controls/OrbitControls.js"></script> 
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680c101a0606052d131c13040507">[email protected]</a>/examples/js/objects/Water.js"></script> 

Make sure you always import three.js files from the same source and release version.

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

calculating the separation between two specific points on the object, with the assistance of the IFCviewerAPI

My goal is to determine the distance between two points on an object by simply clicking. I am utilizing the IFCviewerAPI for this functionality. ...

Utilize Meteor and Mongo to access a nested array object in a template with spacebars

I am trying to populate the content of a textarea by extracting data from a nested array. In my helper function, I have specified the document id and the element id. The goal is to extract the content of the text field from the findOne result and display i ...

Utilizing AJAX in Django applications

I'm currently working on integrating a basic AJAX function with Django, but I seem to be facing some issues. The request.is_ajax() function is returning False, and using event.preventDefault() doesn't prevent the form from getting submitted. Her ...

Carousel with Bootstrap: Heading positioned over the content

My goal is to have the caption of Bootstrap Carousel displayed above the content, but I'm encountering issues with it. When clicking on the chevrons, you may notice the < Item 1 > bouncing... (This behavior is a bug, and logically speaking, I ex ...

Convince me otherwise: Utilizing a Sinatra API paired with a comprehensive JS/HTML frontend

As I embark on the journey of designing a social website that needs to support a large number of users, I have come up with an interesting approach: Implementing Sinatra on the backend with a comprehensive REST API to handle all operations on the website ...

Three.js: Comparing the Process of Updating Geometries with Replacing Them

In my current project, I have a complex scene filled with objects created using the ExtrudeGeometry method. These objects need to be updated every frame, with the extrusion shape and amount constantly changing. The shapes are generated using d3's voro ...

Use multer-ftp to change the name of a file

I've been working on uploading a file to an FTP server using multer-ftp. The file is successfully uploaded, but I need to change the name of the file. Is there a way to accomplish this? var upload = multer({ storage: new FTPStorage({ basepath: ...

Prevent spaces from being entered in a textbox in asp.net with JavaScript

How can I prevent spaces from being entered into a textbox using JavaScript? My current script works, but fails when the space bar is pressed continuously. Any suggestions on improving this issue would be greatly appreciated. Below is the code I am curren ...

How can you access the URL of a resource action in Angular?

In my Angular application, I have created a resource named 'Files' with the following definition: app.factory('Files', function($resource) { return $resource('/api/accounts/:account_id/sites/:site_id/files/:file_id'); }); ...

What is the best way to ensure the options/choices div reaches its ideal width?

Check out my StackBlitz project that I've set up In the styles.css file, you'll notice that I defined a fixed width of 650px for the option dropdown div, which is currently working fine @import '~bootstrap/dist/css/bootstrap.min.css'; ...

What is the best way to display a date picker from a different page as a pop-up when a button is clicked on the current

Chapter One <button class="btn btn-outline-primary btn-sm btn-block " >Place order</button> Chapter Two <button class="simplepicker-btn" >Show Date Picker</button> > <script> > let simplepicker = new SimpleP ...

Extracting text from an array within Meteor

I am currently utilizing Meteor along with the ajduke:bootstrap-tagsinput plugin for managing tags on my platform. Check out the ajduke:bootstrap-tagsinput example page here For inserting tags as arrays, I am using the True Multiple Value feature mention ...

Script and Ajax loading repeatedly, causing multiple instances of script and Ajax loading

I recently encountered some unexpected issues while debugging and playing around with the code on my website, . Let me walk you through the scenario to understand the problem better. When you click on the "art" tab, two logs appear in the console - one sho ...

The Chrome Extension is unable to recognize a portion of the URL following the # symbol in the contentscript.js code

I am currently developing an extension for a specific website that I don't have ownership of. The URLs on this site only change after a /#/. For instance, starting from the URL .../#/PF/charts/patients/419d3081-5e20-4347-a852-52b2c333ce85/summary W ...

How to deselect a radio button in AngularJS

I have explored various solutions using ng-dblclick, $event, etc, but none of them seem to work for me. Within a table, I have two radio buttons inside an ng-repeat. I have a field that determines which radio button should be active. While using ng-change ...

Steps for importing JQuery into a custom DNN module

I have developed a custom DNN module that requires the use of certain JQuery plugins. I successfully loaded the plugins, but encountered the following error: Uncaught TypeError: undefined is not a function imagesloaded.js?cdv=18:93 Uncaught TypeError: ...

Progressive Web App (PWA) default start URL

I am currently facing an issue with my Vue app, which is also a Progressive Web App (PWA). While the PWA functions correctly as planned, I realized that I am using generic paths for my web application. This means that in order to access the correct page, ...

Having trouble setting cookies with Node.js Express?

Check out this code snippet: const app = express(); const cookieParser = require('cookie-parser'); app.use(cookieParser()); app.post("/pinfo", (req, res) => { var form = new formidable.IncomingForm(); form.parse(req, async functi ...

Disabling the submit button on an MC form - step by step guide

In order to create a multiple-choice question with radio buttons, each question must have only one answer choice. Each question should provide three options for the user to select from. It is necessary to validate whether the user has answered every questi ...

Choosing the Following Date from the Datepicker using Selenium IDE

I need the selenium IDE test case to automatically select a date following the steps outlined below: Start by clicking on the departure date to open the datepicker Loop through the dates starting with the currently selected day until the next available d ...