Scene.add(object)
function for the Scene.add(object)
object, adding objects such as THREE.Mesh
(this is the plane you see), THREE.SpotLight
and THREE.AmbientLight
. The THREE.Camera
object is automatically added by the Three.js library itself when you render a scene, but it can also be added manually if you want.
var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); ... var planeGeometry = new THREE.PlaneGeometry(60,40,1,1); var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff}); var plane = new THREE.Mesh(planeGeometry,planeMaterial); ... scene.add(plane); var ambientLight = new THREE.AmbientLight(0x0c0c0c); scene.add(ambientLight); ... var spotLight = new THREE.SpotLight( 0xffffff ); ... scene.add( spotLight );
addCube
() function:
this.addCube = function() { var cubeSize = Math.ceil((Math.random() * 3)); var cubeGeometry = new THREE.CubeGeometry(cubeSize,cubeSize,cubeSize); var cubeMaterial = new THREE.MeshLambertMaterial( {color: Math.random() * 0xffffff }); var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true; cube.name = "cube-" + scene.children.length; cube.position.x=-30 + Math.round((Math.random() * planeGeometry.width)); cube.position.y= Math.round((Math.random() * 5)); cube.position.z=-20 + Math.round((Math.random() * planeGeometry.height)); scene.add(cube); this.numberOfObjects = scene.children.length; };
THREE.CubeGeometry
with a random size from zero to three. In addition to an arbitrary size, the cube also gets a random position on the stage and a color.
name
attribute. Its name is set as cube
- and the number of cubes already on the scene (shown using the scene.children.length
attribute). Thus, the names obtained are cube-1, cube-2, cube-3
and so on. The name can be useful for debugging, but can also be used to directly search for an object on your scene. If you use the Scene.getChildByName(name)
function, then you can directly get the object and, for example, change its location. The variable numberOfObjects
used by our controlling interface as the size of the list of the number of elements on the stage. Therefore, when we add or remove an object, we set this variable to a value equal to the new number of elements in the scene.
removeCube
, and, as the name implies, by pressing this button, the last added cube is removed from the scene. The following shows how this function is implemented:
this.removeCube = function() { var allChildren = scene.children; var lastObject = allChildren[allChildren.length-1]; if (lastObjectinstanceofTHREE.Mesh) { scene.remove(lastObject); this.numberOfObjects = scene.children.length; } }
add
() function. And to remove an object from the scene, as it is not surprising, the function remove
(). In this code snippet, we used the THREE.Scene()
object's children
property to get the last object that was added. We also need to check that the object is a Mesh
object to avoid removing the camera and the light. After we delete an object, we must once again update the control interface element that displays the number of objects on the scene.
console
object, as shown below:
this.outputObjects = function() { console.log(scene.children); }
cube-17
will look like this:
__webglActive: true __webglInit: true _modelViewMatrix: THREE.Matrix4 _normalMatrix: THREE.Matrix3 _vector: THREE.Vector3 castShadow: true children: Array[0] eulerOrder: "XYZ" frustumCulled: true geometry: THREE.CubeGeometry id: 20 material: THREE.MeshLambertMaterial matrix: THREE.Matrix4 matrixAutoUpdate: true matrixRotationWorld: THREE.Matrix4 matrixWorld: THREE.Matrix4 matrixWorldNeedsUpdate: false name: "cube-17" parent: THREE.Scene position: THREE.Vector3 properties: Object quaternion: THREE.Quaternion receiveShadow: false renderDepth: null rotation: THREE.Vector3 rotationAutoUpdate: true scale: THREE.Vector3 up: THREE.Vector3 useQuaternion: false visible: true __proto__: Object
Scene.Add()
: this method adds an object to the sceneScene.Remove(
): this method removes the object from the sceneScene.children()
: this method gets a list of all the children in the scene.Scene.getChildByName()
: this method gets a specific object from the scene by its name attributerender
function to render the scene. Let's take a look at the same code snippet for this example:
function render() { stats.update(); scene.traverse(function(e) { if (e instanceofTHREE.Mesh&& e != plane ) { e.rotation.x+=controls.rotationSpeed; e.rotation.y+=controls.rotationSpeed; e.rotation.z+=controls.rotationSpeed; } }); requestAnimationFrame(render); renderer.render(scene, camera); }
THREE.Scene.traverse()
function is used. We can pass a function as a function argument. This will be passed to the function and invoked in each child of the scene. In the render
() function, we will use the traverse
() function to update the rotation for each cube
instance (we explicitly ignore our main plane). We could do this by iterating over all the elements of the array by the children
property in the for
loop.
Mesh
and Geometry
objects, I would like to show you two interesting properties that can be set on the scene object: fog
and overrideMaterial
.
fog
property allows you to add a fog effect to our scene. The farther the object, the more it will be hidden from view. The following screenshot shows what it looks like:
scene.fog=new THREE.Fog( 0xffffff, 0.015, 100 );
near
, and 100 for the property far
. Using these two properties, you can determine where the fog will start and how quickly it will become more dense. There is also another way to set the fog for the scene: for this we need the following:
scene.fog=new THREE.FogExp2( 0xffffff, 0.015 );
near
and far
, but only the color and density of the fog. In general, it's better to play around yourself to deal with these properties, and you will get the effect you want.
overrideMaterial
property, it is used to set the material of all the objects on the scene. If you use this property, then, as shown below, all objects that you add to the scene will have the same material:
scene.overrideMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
MeshLambertMaterial
object as the base material.
Scene
object.
add(object)
: adds an object to the scene, you can also use this function to create a group of objects.hildren
: returns a list of all the objects that were added to the scene, including the camera and the light.getChildByName(name
): when you create an object, you can give it a name using the name
attribute. The Scene
object has a function that can be used to directly return an object with a specific name.remove(object)
: if you have references to an object in the scene, then you can remove it from the scene using this functiontraverse(function)
: the attribute children
returns a list of all objects on the scene. Using the traverse
function, you can access these objects by passing the callback function as an argument.overrideMaterial
: using this property, you can make sure that all objects on the scene use the same material.Mesh
objects were used. For example, to add a sphere
object to the scene, we did the following:
var sphereGeometry = new THREE.SphereGeometry(4,20,20); var sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff); var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
mesh
and everything is basically done. Let's give a small example:
var vertices = [ new THREE.Vector3(1,3,1), new THREE.Vector3(1,3,-1), new THREE.Vector3(1,-1,1), new THREE.Vector3(1,-1,-1), new THREE.Vector3(-1,3,-1), new THREE.Vector3(-1,3,1), new THREE.Vector3(-1,-1,-1), new THREE.Vector3(-1,-1,1) ]; var faces = [ new THREE.Face3(0,2,1), new THREE.Face3(2,3,1), new THREE.Face3(4,6,5), new THREE.Face3(6,7,5), new THREE.Face3(4,5,1), new THREE.Face3(5,0,1), new THREE.Face3(7,6,2), new THREE.Face3(6,3,2), new THREE.Face3(5,7,0), new THREE.Face3(7,2,0), new THREE.Face3(1,3,4), new THREE.Face3(3,6,4), ]; var geom = new THREE.Geometry(); geom.vertices = vertices; geom.faces = faces; geom.computeCentroids(); geom.mergeVertices();
newTHREE.Face3(0,2,1)
creates a triangular face with the help of points 0, 2 and 1 from the array of vertices.
render
function. Whenever you change something in the drop-down control box, the cube is displayed correctly, based on the position changes of one of its vertices. To improve performance, the Three.js library assumes that the geometry of the grid will not change over time. To make sure that our example works, you need to add the following lines of code inside the loop of the render
function:
mesh.geometry.vertices=vertices; mesh.geometry.verticesNeedUpdate=true; mesh.geometry.computeFaceNormals();
mesh
object. We do not need to reconfigure the faces, as they are still limited to the same points as before. After we update the array of vertices, we must explicitly say that the array of vertices needs to be updated. We can do this by setting the verticesNeedUpdategeometry
property to true
. Finally, we recalculate the faces of a fully updated model using the computeFaceNormals
() function.
geometry
functionality that we will consider is the clone
() function. We mentioned that geometry
determines the shape of an object, and in combination with the material we can create an object that can be added to the scene, and then drawn by the Three.js library. Using the clone
() function, as the name implies, we can make a copy of the geometry and use it to create another mesh
object with a different material. In the previous example, you can see the clone button at the top of the control interface, as shown in the following screenshot:
var materials = [ new THREE.MeshLambertMaterial( { opacity:0.6, color: 0x44ff44,transparent:true } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) ];
mesh
object. To do this, you can use the SceneUtils.createMultiMaterialObject()
function as shown below:
var mesh = THREE.SceneUtils.createMultiMaterialObject(geom,materials);
THREE.Mesh
, but creates one instance for each material you specify, and puts all these objects in a group. This group can be used as well as other objects on the scene. You can add grids, get grids by name, and so on. For example, to add shadows for all children in this group, we do the following:
mesh.children.forEach(function(e) {e.castShadow=true});
this.clone = function() { var cloned = mesh.children[0].geometry.clone(); var materials = [ new THREE.MeshLambertMaterial( { opacity:0.6, color: 0xff44ff, transparent:true } ), new THREE.MeshBasicMaterial({ color: 0x000000, wireframe: true } ) ]; var mesh2 = THREE.SceneUtils.createMultiMaterialObject(cloned,materials); mesh2.children.forEach(function(e) {e.castShadow=true}); mesh2.translateX(5); mesh2.translateZ(5); mesh2.name="clone"; scene.remove(scene.getChildByName("clone")); scene.add(mesh2); }
mesh
variable consists of two children: a mesh that uses MeshLambertMaterial
and a mesh that uses MeshBasicMaterial
. Based on this cloned geometry, we created a new mesh and called it mesh2
.
position
: determines the position of this object in relation to the position of its parent. Most often, the parent of an object is an THREE.Scene()
object THREE.Scene()
rotation
: using this property, you can set the rotation of the object around one of its axes.scale
: this property allows you to scale the object along the x, y and z axestranslateX(amount)
: moves an object a specified value along the x axistranslateY(amount
): moves the object to the specified value along the y axistranslateZ(amount)
: moves an object a specified value along the z axisposition
property. We have already seen this property a couple of times, so let's quickly fix it. Using this property, you can set the x, y, and z coordinates of the object. The position of an object depends on its parent object, which is usually the scene. We can set the position property of an object in three different ways; each of the coordinates can be directly set as follows:
cube.position.x=10; cube.position.y=3; cube.position.z=1;
cube.position.set(10,3,1);
position
property is essentially an THREE.Vector.
object THREE.Vector.
, , :
cube.postion=new THREE.Vector3(10,3,1)
mesh
. , . , THREE.Geometry
THREE.SceneUtils.createMultiMaterialObject
. , , , , . . , , , . , , . :
rotation
. , , . . , position
. , 2pi. , :
cube.rotation.x=0.5*Math.PI; cube.rotation.set(0.5*Math.PI,0,0); cube.rotation = new THREE.Vector3(0.5*Math.PI,0,0);
scale
. . . , , :
sphere
, (1,2,3). translateX
(4). (5,2,3). , , translateX
(-4). , .
07-both-cameras.html
. , - :
this.switchCamera = function() { if (camera instanceof THREE.PerspectiveCamera) { camera = new THREE.OrthographicCamera( window.innerWidth / - 16, window.innerWidth / 16, window.innerHeight / 16, window.innerHeight / - 16, -200, 500 ); camera.position.x = 2; camera.position.y = 1; camera.position.z = 3; camera.lookAt(scene.position); this.perspective = "Orthographic"; } else { camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.x = 120; camera.position.y = 60; camera.position.z = 180; camera.lookAt(scene.position); this.perspective = "Perspective"; } };
THREE.PerspectiveCamera
THREE.OrthographicCamera
. THREE.PerspectiveCamera
. :
fov
: . , . , 180 , 360 . , , , . , , 60 90 . 45 .aspect
: , . , . : window.innerWidth/window.innerHeight
.near
: , Three.js . , , . 0,1far
: . , , , . : 1000.OrthographicCamera
:
left
: Three.js . . , -100, , .right
: , left
, . , .top
:bottom
:near
: , ,far
: , , .position (0,0,0)
. , , :
camera.lookAt(new THREE.Vector3(x,y,z));
children
.translate
.Source: https://habr.com/ru/post/225199/