当前位置:网站首页>Three. JS introductory learning notes 15: threejs frame animation module
Three. JS introductory learning notes 15: threejs frame animation module
2022-07-07 15:49:00 【Jiang Duoduo_ Mostly Harmless 】
Reference learning
http://www.webgl3d.cn/Three.js/
Edit keys and parse playback
Threejs It provides a series of methods for users to edit and play key frame animation API, For example, keyframes KeyframeTrack、 clip AnimationClip、 operation AnimationAction、 Mixer AnimationMixer.
Edit keyframe :
Keyframe animation is through keyframes KeyframeTrack And editing AnimationClip Two API To complete , In actual development, if you need to make a frame animation of a complex 3D model , For example, walking alone 、 Running and other actions , Generally, art passes 3dmax、blender Wait for the software to edit , There is no need for programmers to implement .
Play keys
By manipulating the AnimationAction And mixers AnimationMixer Two API Play the existing frame animation data .
Mixer THREE.AnimationMixer() The parameter of is the parent object of the two grid models written in the case code group, Parameters in actual development Group You can also load the model object returned by the external model .
When playing keyframe animation , Notice in the rendering function render() In the implementation of mixer.update( Render interval ) Tell the frame animation system Threejs Time interval between two renderings , The time interval can be obtained by Threejs A clock class provided Clock Realization .
summary
1. Need a parent group , Add mesh objects to it , Name the mesh object
2. Edit keyframe
(1) Application KeyframeTrack And editing AnimationClip Two API
(2) new THREE.KeyframeTrack
You can set the position in the key frame , Color , Zoom and other properties , Use time points and changing values
(3) Set playback time duration
(4) Create clip clip object , name , The duration of the , Keyframes .
3. Play keys
operation AnimationAction And mixers AnimationMixer Two API Play
(1) new THREE.AnimationMixer(group);
Play the frame animation of all sub objects in the Group
(2)clip As a parameter , Through the mixer clipAction Method returns an operation object AnimationAction
(3) By manipulating the Action Set playback mode , Adjust the playback speed , Whether to circulate
(4) Start playing
4. Rendering
(1) Create a clock object
(2) Perform the render operation , Request rendering again , Render the next frame , Update mixer related time .
effect
KeyframeTrack
http://www.webgl3d.cn/threejs/docs/#api/zh/animation/KeyframeTrack
Complete code
<!DOCTYPE html>
<html lang="en">
<head>
<title>3D</title>
<meta charset="utf-8">
<!-- The adaptive -->
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script type="text/javascript" src="js/three.js"></script>
<script>
init();
function init() {
//renderer
var renderer = new THREE.WebGLRenderer({
antialias:true,
// alpha:true
});
renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementsByTagName("body")[0].appendChild(renderer.domElement);
// init scene
var scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf0f0f0 );
// init camera
var camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(15,15,15);
// camera.lookAt(new THREE.Vector3(0,0,0));
camera.lookAt( scene.position );
scene.add(camera);
//light
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 3, 1, 1 ).normalize();// Vector attribute vector is converted to unit vector , Set the direction to be the same as the original vector , The length is 1
// light.intensity=1.5;// Strength
scene.add(light);
// Animated mesh model
var material1 = new THREE.MeshLambertMaterial({
color:0x0000ff
});
var geometry1 = new THREE.CubeGeometry(5,2,3);
var mesh1 = new THREE.Mesh(geometry1, material1);
mesh1.name="Box";
mesh1.position.set(0,2,0);
var material2 = new THREE.MeshLambertMaterial({
color:0x00ff00
});
var geometry2 = new THREE.SphereGeometry(1, 50, 50);
var mesh2 = new THREE.Mesh(geometry2, material2);
mesh2.name="Sphere";
mesh2.position.set(0,5,0);
var group = new THREE.Group();
group.add(mesh1,mesh2);
scene.add(group);
scene.add(group);
renderer.render(scene, camera);
// edit group Sub object mesh model mesh1 and mesh2 Frame animation data
// Create a Box Key frame data of the object
var times = [0, 10];// Keyframe event array , Discrete time point sequence
var values = [0,0,0,10,0,0]// An array of values corresponding to the point in time
// Create a position keyframe object :0 Time corresponding position 0,0,0,10 Time corresponding position 150,0,0
var posTrack = new THREE.KeyframeTrack('Box.position', times, values);
// Create yan'e keyframe object :10 Time corresponds to color 1,0,0 20 Time corresponds to color 0,0, 1
var colorKF = new THREE.KeyframeTrack('Box.material.color', [10, 20], [1, 0, 0, 0, 0, 1]);
// Create a Sphere Key frame data of the object , from 0-20 Period of time , Size scale The zoom 3 times
var scaleTrack = new THREE.KeyframeTrack('Sphere.scale', [0, 20], [1, 1, 1, 3, 3, 3]);
//duration Determines the default playback time , Generally, the largest event of all frame animations
//duration Partial , Frame animation data cannot be played out , Larger , After playing the frame animation, it will continue to play empty
var duration = 20;
// Multiple frame animation seat elements create a clip clip object , name “default”, The duration of the 20
var clip = new THREE.AnimationClip("default", duration, [posTrack, colorKF, scaleTrack]);
// Play the edited key frame
//group As a parameter of mixer , It can play group Frame animation of all sub objects in
var mixer = new THREE.AnimationMixer(group);
// clip clip As a parameter , Through the mixer clipAction Method returns an operation object AnimationAction
var AnimationAction = mixer.clipAction(clip);
// By manipulating the Action Set playback mode
AnimationAction.timeScale = 20;// Default 1, You can adjust the playback speed
// AnimationAction.loop = THREE.LoopOnce; // No looping
AnimationAction.play();// Start playing
// Create a clock object Clock
var clock = new THREE.Clock();
// Rendering function
function render(){
renderer.render(scene, camera); // Perform the render operation
requestAnimationFrame(render); // Request to execute render function again render, Render the next frame
//clock.getDelta() Method to obtain the time interval between two frames
// Update mixer related time
mixer.update(clock.getDelta());
}
render();
}
</script>
</body>
</html>
Set up ( Pause 、 Fast forward 、 Scroll bar )
https://blog.csdn.net/u014291990/article/details/103350524
problem : How to use numbers to represent colors in keyframes ?
边栏推荐
- HPDC smart base Talent Development Summit essay
- Briefly describe the working principle of kept
- 【原创】一切不谈考核的管理都是扯淡!
- 【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
- Whether runnable can be interrupted
- Spin animation of Cocos performance optimization
- Pit avoidance: description of null values in in and not in SQL
- Monthly observation of internet medical field in May 2022
- Function: JS Click to copy content function
- numpy---基础学习笔记
猜你喜欢
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
Zhongang Mining: Fluorite continues to lead the growth of new energy market
【数字IC验证快速入门】22、SystemVerilog项目实践之AHB-SRAMC(2)(AMBA总线介绍)
【數字IC驗證快速入門】26、SystemVerilog項目實踐之AHB-SRAMC(6)(APB協議基本要點)
Implementation of crawling web pages and saving them to MySQL using the scrapy framework
Super signature principle (fully automated super signature) [Yun Xiaoduo]
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
Cocos creator collision and collision callback do not take effect
LeetCode2_ Add two numbers
Asynchronous application of generator function
随机推荐
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
2.Golang基础知识
Whole process analysis of unity3d rendering pipeline
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
The significance of XOR in embedded C language
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
How to understand that binary complement represents negative numbers
Monthly observation of internet medical field in May 2022
Starting from 1.5, build a microservice framework link tracking traceid
LeetCode2_ Add two numbers
MongoD管理数据库的方法介绍
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
Iterator and for of.. loop
MongoDB数据库基础知识整理
Syntax of generator function (state machine)
How to build your own super signature system (yunxiaoduo)?
The download button and debug button in keil are grayed out