Book Image

Three.js Cookbook

By : Jos Dirksen
Book Image

Three.js Cookbook

By: Jos Dirksen

Overview of this book

Table of Contents (15 chapters)
Three.js Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Selecting an object in the scene


A common requirement for Three.js applications is to interact with the scene. You might create a shooter where you want to use the mouse for aiming or an RPG where you need to interact with your environment. In this recipe, we'll show you how you can use the mouse to select objects that are rendered on screen.

Getting ready

To apply this effect, we'll need a scene where we can select some objects. For this recipe, we've provided an example, which is 03.10-select-an-object-in-the-scene.html. If you open this file in your browser, you'll see a number of objects moving around the scene.

You can use your mouse to select any of the objects on screen. The first time you click on them, they'll become transparent, and the next time, they'll become solid again.

How to do it...

We'll need to work through a couple of steps for this recipe:

  1. The first thing we need to do is set up the mouse listener. We want to fire a JavaScript function each time the mouse button is clicked...