|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 N9 Y7 D$ f: F% o8 P
% w5 a6 [9 z" M
废话不多说,官网自己百度下, threejs.org ,入门很简单。
) u; q) O$ q# h3 @6 q& G9 B6 P0 W( \) A( \, o) q" f
创建的javascript脚本如下
0 c( Q* S( P5 W: {( @) G& b9 }5 _8 @
/ Y h# b% K4 p6 S* [$ U( \$ q9 a[mw_shl_code=javascript,true]9 @" A* I, a) s
var scene = new THREE.Scene();
1 l3 o9 }" O1 b" R5 I7 Jvar camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
+ {$ M2 D, u& Hvar renderer = new THREE.WebGLRenderer();5 q: F$ S/ V- y" S" k/ D* j' y
renderer.setSize(window.innerWidth, window.innerHeight);5 o1 p9 n" z) R/ U- J. F
document.body.appendChild(renderer.domElement);
5 f: E& F) I* Z8 f. z- e3 ]
; g/ s! g, p/ U2 [# c8 |// Refresh Issue
. Y1 D2 N t2 X1 Ewindow.addEventListener('resize', function () {
6 C/ k. e9 O. z" w1 \; q var width = this.window.innerWidth;
6 C! b- F" n2 ?! K- S: ]5 t var height = this.window.innerHeight;+ _2 G, `5 m- z2 f
render.setSize(width, height);2 ?; X& g6 V6 i7 u% R* C8 q' k
camera.aspect = width / height;; r4 b F# n6 S* J+ `, \( v
camera.updateProjectionMatrix();7 j& P) O1 ~ f( E4 Y
})9 L) c% t! T. t9 J! [' b! U
4 q2 u" I9 B0 ]7 t. y2 W//orbit contorls# u3 s# y; Z ?! X7 g
controls = new THREE.OrbiTControls(camera,renderer.domElement);3 Q' s; H9 X0 Z" _" x* `( W% u
; ^4 m& V. g: R3 V! [
//create the shape" H1 x3 K. j- W5 E* z% J
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);% e, ]3 s" h' s: o5 E
//create a meterial- \# r# r4 g7 N9 J) w6 @
var material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });9 v) E; z4 R$ k J6 n" j+ o% l
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];5 p( h8 W4 z$ v/ S- i: y
//var material = new THREE.MeshFaceMaterial(myMaterial);
8 F2 A0 q8 V' `9 v8 E9 i9 x) y
) F3 ~; D4 |6 ?; @- }var cube = new THREE.Mesh(geometry, material);
, u9 d/ E1 W) G0 N6 L- j. h, N! lscene.add(cube);
$ h* [' W# M4 w% ycamera.position.z = 5;
7 @; l! {) H2 j& N) c; G2 t3 a5 _. m# E" {- y3 e
* Z' F: I; h1 t4 r" ]8 L
5 T3 J& U* Y3 ]- T7 B# F% m6 i* Z4 g5 S" E6 L
& T" X4 \! S: `! ?% Q) Y//rotate `( ~/ f8 V* C7 d# `, d0 \
var update = function () {- m5 m+ @8 `. l3 z# {
cube.rotation.x += .01;: J9 P3 ?( G) D2 ~3 f# _: z
cube.rotation.y += .02;
0 S( a1 t3 \9 x0 L cube.rotation.z += .02;
+ I5 G# z9 d" Q& i}
: j: a& S/ i) o1 e) x
& e; a7 N: z A) R" M" x) E+ W9 kvar render = function () {8 @, B2 U: [4 r3 `" _5 k
renderer.render(scene, camera);
. ^& g# C& @' u2 y5 J, V" b}0 B( w' U/ K1 P) ^6 X/ w
4 \2 ^' B2 D: r% ]& l( h* @
l/ t4 M H e& L5 p* B7 f* ~- e+ m1 e" s# I @
var Gameloop = function () {
5 L- n' m% w" u+ I3 l requestAnimationFrame(Gameloop);
5 S- @1 ? v+ _7 x; V update();
! e5 ?5 [9 T/ P4 e+ d0 L) a2 R! f render();0 p5 a4 Q3 W- t) y* l2 K
}+ S, @1 P$ a% e3 G2 J
7 c* J" g! H( O$ c: MGameloop();9 c( V& C* s$ E1 S
[/mw_shl_code]
# w c6 ~8 R' V/ e& d* _4 Y" j+ ?+ B# I/ f) B( z
2 [7 t, ]0 @1 q6 `" I
引入争取的html,$ X: l# a" J9 q/ O
$ \6 u' Z0 u. _6 V. Y5 d8 K& E: |
[mw_shl_code=html,true]<!DOCTYPE html>) K5 E8 Y# V9 u8 C. L- U/ M
<html>7 u2 N" Q" J* |; n2 \& R+ W# \9 ?$ J/ R
4 I$ ^! e. n: s c' n6 W3 Y q
<head>6 E- g& K- {5 ?. ] X: i: G& c
<title>hello ThreeJS</title>7 j1 w1 }* ?' y* G
<style>
" Q& r, [8 D( C. ^) X# _ body {) [% Z+ L+ ~% |: p: n2 G: k) S
margin: 0;
( b+ v" d* H, ]% k- j; c, L! c }- v) a# ~7 M+ }; o- `) S
1 H+ i& |' o2 E- [; N! a' @ canvas {
3 B. i4 x. p. T4 l1 f" y' v width: 100%;( I# Z+ d; o) B/ Q/ L4 y1 w
height: 100%;( F' M) \, Y+ F1 [+ R$ p
}
o' P' N8 J- o/ g3 Z) A0 _3 O+ g
h1 {
8 g) i: s/ c' h% W4 l! b color: aqua;
, O3 P) Q, L: x9 { }
8 u) Z5 k9 r! c) l2 i0 P" m </style>
* p6 q+ S5 {$ n( N</head> `! n; }& m! q) f6 S
# q. ]& b* d% ?; ?<body>( h" q* Z% V3 }
<script src="js/three.js"></script>
: u( h3 Y) V( _0 N/ V0 G <script src="js/OrbitControls.js"></script>
5 D0 Q" P& F2 B0 z+ f5 f/ R n <script src="myjs.js"></script>
( R; Y4 M9 f" b. w 8 x- H8 |7 ~- a$ k5 w! z4 x; C8 ^
% ?. I" f% L" O- K& i" I; w* X0 a7 a( I* E+ ~3 I
/ @6 |1 C9 F( _3 H+ y) T4 N
</body>
. K3 {1 E- @. G" e. b; S2 E9 h: ~* G0 }! ?% T) y
</html>[/mw_shl_code]
" o* K8 D: e! u1 _/ e
5 R! I, {3 I) e p, Q; p( ]$ w2 r/ d0 s' f效果点击这里:
B0 t- b/ L* J5 D) B
7 a/ p! b/ x5 F- E2 J( p5 K4 Uhttp://plmhome.com/doteam/lesson1.html9 D' s6 R ]9 \9 @2 ?
9 ^5 z/ |# f9 V# b7 u4 d$ ?. E8 @, O$ T0 M9 ]! N. q
|
|