Texture map
- Texture is introduced
- Simple texture mapping
- Canvas texture map
- Video Video texture map
-
- Video as three.js texture map (VideoTexture)
- BumpMap and normalMap
Texture is introduced
Texture mapping is an important part of Threejs, and developers need to process texture mapping while loading models for games, product 720 presentations, 3D visualizations for the Internet of Things, and more.
Simple texture mapping
Loading an image using the load() method of TextureLoader returns the Texture object Texture, which can be used as the value of the model material color map.map property.
The color map attribute of the material. After the map is set, the model will collect pixel values from the texture map. The map is called a color map because the mesh model gets the RGB color value of the color map. Example: Using tiling, attach the grass image to PlaneGeometry as follows:
Code:
<div id="app"></div>
<script type="text/javascript">
var scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45.window.innerWidth / window.innerHeight, 1.1000);
render = new THREE.WebGLRenderer({
antialias: true
});
render.setPixelRatio(window.devicePixelRatio);
render.setSize(window.innerWidth, window.innerHeight)
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
var app = document.getElementById("app");
app.appendChild(render.domElement);
var geometry = new THREE.PlaneGeometry(20.20.32); / / plane
var textureLoader = new THREE.TextureLoader(); // Texture loader
var texture = textureLoader.load('./img/grass/grass.png');
// Set array mode to default ClampToEdgeWrapping RepeatWrapping: Array Mirroring Array: MirroredRepeatWrapping
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
// Uv texture repeats in both directions
texture.repeat.set(10.10);
var material = new THREE.MeshBasicMaterial({
map: texture, // Set the texture map
side: THREE.DoubleSide
});
var plane = new THREE.Mesh(geometry, material);
scene.add(plane);
/ / camera
camera.position.set(20.20.40); // Set the camera position
camera.lookAt(new THREE.Vector3(0.0.0))
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
function animate(){
render.render(scene, camera);
window.requestAnimationFrame(animate);
}
animate();
</script>
Copy the code
Effect:
The texture map is static. If you want a moving texture map, you can set the texture. Offset position
function animate(){
// Set the texture offset
texture.offset.x -= 0.06
render.render(scene, camera);
window.requestAnimationFrame(animate);
}
animate();
Copy the code
Effect:
Canvas texture map
During the process of using a map, different materials can be used on the map
Using a Canvas Canvas as a texture map can be implemented using the Three. Js class CanvasTexture.
Canvas Canvas can draw a variety of geometric shapes through 2D API. You can draw an outline on Canvas and then use it as a texture map for model objects such as Three. Js mesh model, Sprite model, etc.
Core code:
var textureLoader = new THREE.TextureLoader(); // Texture loader
var texture = new THREE.CanvasTexture(canvas);
Copy the code
When using canvas as a map, the content of canvas should be created first, without adding canvas into the DOM tree
var canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
var c = canvas.getContext('2d');
// The rectangular area fills the background
c.fillStyle = "#ff0000";
c.fillRect(0.0.512.128);
c.beginPath();
/ / text
c.beginPath();
c.translate(256.64);
c.fillStyle = "#fff"; // Fill the text with color
c.font = "bold 28px 宋体"; // Font style Settings
c.textBaseline = "middle"; // The vertical coordinate defined by the text and fillText
c.textAlign = "center"; // The text is centered (with the abscissa defined by fillText)
c.fillText("Empty city machine ( ̄ε(# ̄)☆╰╮ O ( ̄ democracy  ̄//)".0.0);
Copy the code
var geometry = new THREE.PlaneGeometry(40.20.32);
var textureLoader = new THREE.TextureLoader(); // Texture loader
var texture = new THREE.CanvasTexture(canvas); // canvas is canvas
// Set array mode to default ClampToEdgeWrapping RepeatWrapping: Array Mirroring Array: MirroredRepeatWrapping
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
// Uv texture repeats in both directions
texture.repeat.set(1.1);
var material = new THREE.MeshBasicMaterial({
map: texture, // Set the texture map
side: THREE.DoubleSide / / double
});
var box = new THREE.Mesh(geometry, material);
scene.add(box);
Copy the code
Effect:
Video Video texture map
Using the video as a texture map can be implemented using the three. js class VideoTexture separately.
Video as three.js texture map (VideoTexture)
Video is essentially composed of frames of picture streams. The video is used as the texture map of Threejs model, which is to extract frame by frame from the video as the texture map of the model, and then continuously update the texture map to produce the effect of video playback.
To use the video as a texture image, first create the content of the video, without adding the video into the DOM tree code:
let video = document.createElement('video'); // Create video object
video.src = ".. /img/ Season 2 1.mp4"; // Set the video address
video.autoplay = "autoplay"; // Set playback
video.loop = "loop"; // Loop
Copy the code
Core code:
var textureLoader = new THREE.TextureLoader(); // Texture loader
var texture = new THREE.VideoTexture(video); // The video is used as a texture
Copy the code
Effect :(due to the size limit of the uploaded image, the frame number of the GIF image was deleted, resulting in the slow display effect below)
BumpMap and normalMap
In order to reduce the size of the model file, normalMap algorithm is naturally generated. 3D art of complex 3D models can be simplified into simple models by subtracting faces. The complex geometry information of the fine surface is then mapped to a normal map. normalMap
In a previous article, we created a rotating earth model using textures:Three. Js Miscellany (9) — Exercise: The Earth
There were no mountains on earth before, just a plane map. Now you can use the Discovery map to create the mountain effect
Normal map:
Code:
var textureLoader = new THREE.TextureLoader(); // Texture loader
var texture = textureLoader.load('./img/earth/css_globe_diffuse.jpg'); // Load the image and return the Texture object
// Load the normal map
var textureNormal = textureLoader.load('./img/earth/earth_normal_2048.jpg');
var material = new THREE.MeshPhongMaterial({
map: texture, // Set the texture map
normalMap: textureNormal, // Normal map
// set the depth, default (1,1).
normalScale: new THREE.Vector2(1.2.1.2)});var sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
Copy the code
Effect: