example.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Proof of Concept VRML loading with ThreeJs and VrmlParser</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. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display: block;
  23. }
  24. #info a, .button {
  25. color: #f00;
  26. font-weight: bold;
  27. text-decoration: underline;
  28. cursor: pointer
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank">three.js / VrmlParser</a> -
  35. vrml format loader test using VrmlParser -
  36. </div>
  37. <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r76/three.js"></script>
  38. <script src="vrml.js"></script>
  39. <script src="Renderer/ThreeJsRenderer.js"></script>
  40. <script type="x-shader/x-vertex" id="vertexShader">
  41. varying vec3 vWorldPosition;
  42. void main() {
  43. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  44. vWorldPosition = worldPosition.xyz;
  45. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  46. }
  47. </script>
  48. <script type="x-shader/x-fragment" id="fragmentShader">
  49. uniform vec3 topColor;
  50. uniform vec3 bottomColor;
  51. uniform float offset;
  52. uniform float exponent;
  53. varying vec3 vWorldPosition;
  54. void main() {
  55. float h = normalize( vWorldPosition + offset ).y;
  56. gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( h, exponent ), 0.0 ) ), 1.0 );
  57. }
  58. </script>
  59. <script>
  60. var container, stats;
  61. var camera, controls, scene, renderer;
  62. var cross;
  63. init();
  64. animate();
  65. function init() {
  66. camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 1e10);
  67. camera.position.z = 6;
  68. scene = new THREE.Scene();
  69. scene.fog = new THREE.Fog(0xffffff, 1, 5000);
  70. scene.fog.color.setHSL(0.6, 0, 1);
  71. scene.add(camera);
  72. // light
  73. var dirLight = new THREE.DirectionalLight(0xffffff);
  74. dirLight.position.set(200, 200, 1000).normalize();
  75. var ambientLight = new THREE.AmbientLight(0x444444);
  76. dirLight.position.set(-200, -200, 1000).normalize();
  77. camera.add(dirLight);
  78. scene.add(ambientLight);
  79. camera.add(dirLight.target);
  80. // VRML parser example:
  81. var xhrLoader = new THREE.XHRLoader();
  82. // onLoad, onProgress, onError
  83. xhrLoader.load('example.wrl', function (data) {
  84. try {
  85. var tree = vrmlParser.parse(data);
  86. } catch ( e ) {
  87. console.log('Exception with message ' + e.message);
  88. if ( undefined !== e.location ) {
  89. console.log('Exception at location start: offset: ' + e.location.start.offset + ' line: ' + e.location.start.line + ' column: ' + e.location.start.column);
  90. console.log('Exception at location end: offset: ' + e.location.end.offset + ' line: ' + e.location.end.line + ' column: ' + e.location.end.column);
  91. }
  92. return;
  93. }
  94. console.log(tree);
  95. var renderer = new VrmlParser.Renderer.ThreeJsRenderer();
  96. renderer.render(tree, scene);
  97. }, function () {
  98. }, function (error) {
  99. var request = error.target;
  100. if ( 404 === request.status ) {
  101. console.log('VRML Document not found at ' + request.responseURL);
  102. }
  103. console.log(error);
  104. });
  105. // renderer
  106. renderer = new THREE.WebGLRenderer({antialias: true});
  107. renderer.setClearColor(0x000000, 1);
  108. renderer.setSize(window.innerWidth, window.innerHeight);
  109. container = document.createElement('div');
  110. document.body.appendChild(container);
  111. container.appendChild(renderer.domElement);
  112. //
  113. window.addEventListener('resize', onWindowResize, false);
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize(window.innerWidth, window.innerHeight);
  119. }
  120. function animate() {
  121. requestAnimationFrame(animate);
  122. renderer.render(scene, camera);
  123. }
  124. </script>
  125. </body>
  126. </html>