webgl_loader_json.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loader -json</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#777;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px;
  18. width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family:Monospace;
  22. font-size:13px;
  23. text-align:center;
  24. }
  25. a {
  26. color: #ffffff;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="container"></div>
  32. <div id="info">
  33. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  34. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank" rel="noopener">3drt</a>
  35. </div>
  36. <script src="../build/three.js"></script>
  37. <script src="js/WebGL.js"></script>
  38. <script src="js/libs/stats.min.js"></script>
  39. <script>
  40. if ( WEBGL.isWebGLAvailable() === false ) {
  41. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  42. }
  43. var container, stats, clock, mixer;
  44. var camera, scene, renderer;
  45. init();
  46. animate();
  47. function init() {
  48. container = document.getElementById( 'container' );
  49. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  50. camera.position.set( 2, 4, 5 );
  51. clock = new THREE.Clock();
  52. scene = new THREE.Scene();
  53. scene.fog = new THREE.FogExp2( 0x000000, 0.035 );
  54. mixer = new THREE.AnimationMixer( scene );
  55. var loader = new THREE.JSONLoader();
  56. loader.load( 'models/json/legacy/monster/monster.js', function ( geometry, materials ) {
  57. // adjust color a bit
  58. var material = materials[ 0 ];
  59. material.morphTargets = true;
  60. material.color.setHex( 0xffaaaa );
  61. for ( var i = 0; i < 729; i ++ ) {
  62. var mesh = new THREE.Mesh( geometry, materials );
  63. // random placement in a grid
  64. var x = ( ( i % 27 ) - 13.5 ) * 2 + THREE.Math.randFloatSpread( 1 );
  65. var z = ( Math.floor( i / 27 ) - 13.5 ) * 2 + THREE.Math.randFloatSpread( 1 );
  66. mesh.position.set( x, 0, z );
  67. var s = THREE.Math.randFloat( 0.00075, 0.001 );
  68. mesh.scale.set( s, s, s );
  69. mesh.rotation.y = THREE.Math.randFloat( - 0.25, 0.25 );
  70. mesh.matrixAutoUpdate = false;
  71. mesh.updateMatrix();
  72. scene.add( mesh );
  73. mixer.clipAction( geometry.animations[ 0 ], mesh )
  74. .setDuration( 1 ) // one second
  75. .startAt( - Math.random() ) // random phase (already running)
  76. .play(); // let's go
  77. }
  78. } );
  79. // lights
  80. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  81. scene.add( ambientLight );
  82. var pointLight = new THREE.PointLight( 0xff4400, 5, 30 );
  83. pointLight.position.set( 5, 0, 0 );
  84. scene.add( pointLight );
  85. // renderer
  86. renderer = new THREE.WebGLRenderer();
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. container.appendChild( renderer.domElement );
  90. // stats
  91. stats = new Stats();
  92. container.appendChild( stats.dom );
  93. // events
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. }
  96. //
  97. function onWindowResize() {
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. }
  102. //
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. stats.update();
  107. }
  108. function render() {
  109. var timer = Date.now() * 0.0005;
  110. camera.position.x = Math.cos( timer ) * 10;
  111. camera.position.y = 4;
  112. camera.position.z = Math.sin( timer ) * 10;
  113. mixer.update( clock.getDelta() );
  114. camera.lookAt( scene.position );
  115. renderer.render( scene, camera );
  116. }
  117. </script>
  118. </body>
  119. </html>