|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
# U. ?5 ]* g# ?3 U, U( u6 o3 q2 M
. V+ i1 x. ^; Q; o废话不多说,官网自己百度下, threejs.org ,入门很简单。& w- h' b5 X' q3 A/ `
+ R$ R* F% `# T, c5 q6 T( `& L创建的javascript脚本如下
$ j2 P3 `2 b% m+ j/ m% G0 z+ G( R" J& T6 L5 q: M! b. `" q. ]8 H- P! Q4 a
: `; z W2 m6 q( z! c! \- ]3 d! o
[mw_shl_code=javascript,true]$ P# ^# c* v) r" {( S" I
var scene = new THREE.Scene();; r6 N, r- v; d: w- ?1 I
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);- Y3 m- V' o& O( r. Z J
var renderer = new THREE.WebGLRenderer();
$ ^" ^- N: ~) N7 c `$ d. T: Drenderer.setSize(window.innerWidth, window.innerHeight);8 u, `/ q/ Q; g7 A# U
document.body.appendChild(renderer.domElement);6 e& D) g) @* H/ U i
4 V3 {3 s: D3 M: D// Refresh Issue 1 U% L' I6 L" \3 O' W+ @8 F/ U+ a
window.addEventListener('resize', function () {6 S* N$ v, k' ]3 A3 v
var width = this.window.innerWidth;
( m& y1 w) P! }; e ] var height = this.window.innerHeight;
3 X, k4 P. M y9 o# S$ I render.setSize(width, height);( | f9 M( q, |. J0 L+ E; ~
camera.aspect = width / height;
) x9 O- H+ Y* f7 D) D camera.updateProjectionMatrix();
1 c3 Y0 y" i, b9 ^( l! l})
5 K. _2 K8 ~0 D9 [
6 O* W" m. H9 q# v' s( I//orbit contorls
8 z3 @4 l, J$ K, V* C# ^7 Vcontrols = new THREE.OrbiTControls(camera,renderer.domElement);. A+ ]; ]1 p( A, M$ |0 s! d: H0 Q2 R
- R6 p, i% o4 d* H5 d* U% ^0 E//create the shape
$ T/ L7 O2 n- ?% c5 F+ O; Kvar geometry = new THREE.TorusGeometry(2, 1, 16, 100);. Y: u% X X) l: W
//create a meterial
: \1 A+ l( }8 T; n! v- _var material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });
/ w. i/ J `) O- y; T6 h( T//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];2 `/ o( ]* p7 @9 R& s- ^ B
//var material = new THREE.MeshFaceMaterial(myMaterial);
0 {( R; r! f4 H# _, \, G
5 }) _9 t) }$ Tvar cube = new THREE.Mesh(geometry, material);; U8 z, j/ t L
scene.add(cube);
2 n1 ^# d8 I8 h5 D; vcamera.position.z = 5;- k5 a" l+ O$ q2 P) p' ~
X; c2 ^! k( S# n+ y9 W
' g1 Q; M2 ~/ n0 b
0 B+ c* _. e, y& S8 K
& G% w3 a& x& ?) K' k, j( i: P& o7 Y+ w1 Z. I* u7 u/ r
//rotate 2 V1 |" i& X7 @7 y- X- b
var update = function () {
5 }- `/ @. W4 @0 L cube.rotation.x += .01;
5 s3 V& @( t. ^8 q! P: W* @* | cube.rotation.y += .02;
- E* p" b8 Q6 B: P. e! }# I- `* G cube.rotation.z += .02;! M: s$ b; f, {! Q& t; V. D
}
8 ~! O/ m; S" O: `% a3 V& J4 h9 W* u% _% a
var render = function () {/ x8 O, `* R- H
renderer.render(scene, camera);; ?3 c! }5 W- |( F' Y
}
1 g* `) ]6 h, g! H9 x H* J
3 K8 e% [8 R; m( T: i3 g j" p. C+ x, d9 ^( H9 ^$ f
/ L* A; U' w: {+ G" i( A# v8 e5 zvar Gameloop = function () {
% E, R6 r' c4 {$ W requestAnimationFrame(Gameloop);
. j$ L6 U4 ]5 V+ g; j" | update();
1 k* J5 K: Y$ [' Y* G0 i- Q render();4 r! L4 _5 N w: ?4 o! y- V# T
}, r3 F7 W" t; t8 B w: b z) j1 |' b
1 L5 P$ a2 P5 X$ O, P
Gameloop();
( \1 e& s# c& m1 F2 |# U6 A[/mw_shl_code]& F' L- c& q% ~4 k- U F% L
# U9 {0 b% ?& U) g- S
# l4 P: S3 ], N4 T# O引入争取的html,
: R2 d& C; t$ G6 t2 ~$ B+ e" O6 K4 n5 K/ h; f
[mw_shl_code=html,true]<!DOCTYPE html>2 s# b7 [$ \9 d% t' ]$ _. H8 e
<html>
5 o- P! j7 ~. H, Z |! O
7 f$ Y. t+ J* T2 _$ W& q4 r<head>. c0 Z: e0 C s7 I9 O! x
<title>hello ThreeJS</title>
. ?& d9 U/ A0 V4 u <style>
- l" M, X- d% l1 k! R body {
* n8 o* t3 O& T! r- F: A5 u margin: 0;/ Q/ S, R9 b* f# {# s+ y- b* a
}
7 L" h+ D Q( |8 k* `
8 m! k) k7 ^ |+ ?& Z canvas {
5 A5 d% I1 r8 n width: 100%;
. v1 A& y& a8 a0 e3 p1 d height: 100%;2 w. ~; U' p& k( e
}
% Y, ?$ A: j4 h
7 ~$ l1 a2 w9 {$ O1 C& H- B h1 {
5 x4 P( E8 J: e) t J4 i color: aqua;
8 ?( |/ E% \$ i }2 J" P' a6 {# P7 {' W4 j
</style>9 M1 s! e1 Q1 d" o. H( w% Z
</head>4 m8 P4 o9 t! z" v, H: E# J
& F5 {- e4 O5 U- p0 u0 s# E<body>
7 p1 b9 V- F% N$ Y* F# r- c7 D; e <script src="js/three.js"></script>! l6 V' y+ \7 @' k* ?8 X
<script src="js/OrbitControls.js"></script>7 i" r! }6 ^0 j
<script src="myjs.js"></script>
9 ~' E7 a! u1 t2 x7 H2 A3 t8 e @
/ X: y) m% [" [; F. M4 b
: A4 j! {* g& w; }% ?0 _" B" B" }# j
: z0 d. n. g( R- i; x( F
</body>
9 d" s- r6 n7 w, S2 w
" F. A. C, b: Q</html>[/mw_shl_code]
" ?2 K+ h2 Z5 W4 _; J) K2 T' |2 v; W; q! P- U7 h* L0 ?
效果点击这里:; B0 H' \& i# a) ~
! L/ G; E' f& u/ G% Ahttp://plmhome.com/doteam/lesson1.html
1 s) \( ^* @0 M# Y- y, y: v b' v; j5 S+ l- ~ \
! p( u' }; s- L% G
|
|