webgl_geometry_colors_json.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - json - vertex colors</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. color: #eee;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. padding: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. a {
  24. color: #0080ff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - json - vertex colors</div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var container, stats;
  37. var camera, scene, renderer;
  38. var mesh, mesh2, mesh3, light;
  39. var mouseX = 0, mouseY = 0;
  40. var windowHalfX = window.innerWidth / 2;
  41. var windowHalfY = window.innerHeight / 2;
  42. init();
  43. animate();
  44. function init() {
  45. container = document.getElementById( 'container' );
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 1800;
  48. scene = new THREE.Scene();
  49. light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 0, 0, 1 ).normalize();
  51. scene.add( light );
  52. var loader = new THREE.JSONLoader();
  53. loader.load( "models/json/cubecolors/cubecolors.json", createScene1 );
  54. loader.load( "models/json/cubecolors/cube_fvc.json", createScene2 );
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  62. //
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. windowHalfX = window.innerWidth / 2;
  67. windowHalfY = window.innerHeight / 2;
  68. camera.aspect = window.innerWidth / window.innerHeight;
  69. camera.updateProjectionMatrix();
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. }
  72. function createScene1( geometry, materials ) {
  73. mesh = new THREE.Mesh( geometry, materials );
  74. mesh.position.x = 400;
  75. mesh.scale.x = mesh.scale.y = mesh.scale.z = 250;
  76. scene.add( mesh );
  77. }
  78. function createScene2( geometry, materials ) {
  79. mesh2 = new THREE.Mesh( geometry, materials );
  80. mesh2.position.x = - 400;
  81. mesh2.scale.x = mesh2.scale.y = mesh2.scale.z = 250;
  82. scene.add( mesh2 );
  83. }
  84. function onDocumentMouseMove( event ) {
  85. mouseX = ( event.clientX - windowHalfX );
  86. mouseY = ( event.clientY - windowHalfY );
  87. }
  88. //
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. function render() {
  95. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  96. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  97. camera.lookAt( scene.position );
  98. if ( mesh ) {
  99. mesh.rotation.x += 0.01;
  100. mesh.rotation.y += 0.01;
  101. }
  102. if ( mesh2 ) {
  103. mesh2.rotation.x += 0.01;
  104. mesh2.rotation.y += 0.01;
  105. }
  106. renderer.render( scene, camera );
  107. }
  108. </script>
  109. </body>
  110. </html>