webgl_materials_texture_html.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - html texture</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #ffffff;
  11. }
  12. #draw_element {
  13. width: 600px;
  14. background-color: #aaaaaa;
  15. color: #000000;
  16. font-family: sans-serif;
  17. font-size: 30px;
  18. line-height: 1.5;
  19. text-align: center;
  20. padding: 30px;
  21. /* border: 10px solid #cccccc; */
  22. }
  23. #draw_element img {
  24. animation: swing 1s ease-in-out infinite alternate;
  25. }
  26. #draw_element input[type="text"] {
  27. font-size: 24px;
  28. padding: 8px 12px;
  29. border: 2px solid #888;
  30. border-radius: 6px;
  31. width: 80%;
  32. margin-top: 10px;
  33. }
  34. #draw_element button {
  35. font-size: 24px;
  36. padding: 8px 20px;
  37. margin-top: 10px;
  38. border: none;
  39. border-radius: 6px;
  40. background-color: #4CAF50;
  41. color: white;
  42. cursor: pointer;
  43. }
  44. #draw_element button:hover {
  45. background-color: #2196F3;
  46. }
  47. @keyframes swing {
  48. from { transform: rotate(-15deg); }
  49. to { transform: rotate(15deg); }
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div id="info">
  55. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - HTMLTexture
  56. </div>
  57. <script type="importmap">
  58. {
  59. "imports": {
  60. "three": "../build/three.module.js",
  61. "three/addons/": "./jsm/"
  62. }
  63. }
  64. </script>
  65. <script type="module">
  66. import * as THREE from 'three';
  67. import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  68. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  69. import { InteractionManager } from 'three/addons/interaction/InteractionManager.js';
  70. let camera, scene, renderer, mesh, interactions;
  71. init();
  72. function init() {
  73. renderer = new THREE.WebGLRenderer( { antialias: true } );
  74. if ( renderer.getContext().texElementImage2D === undefined ) {
  75. info.innerHTML += '<br>This browser does not support the <a href="https://github.com/WICG/html-in-canvas" target="_blank">HTML-in-Canvas API</a>.';
  76. }
  77. renderer.toneMapping = THREE.NeutralToneMapping;
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.setAnimationLoop( animate );
  81. document.body.appendChild( renderer.domElement );
  82. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  83. camera.position.z = 500;
  84. scene = new THREE.Scene();
  85. scene.background = new THREE.Color( 0xaaaaaa );
  86. scene.environment = new THREE.PMREMGenerator( renderer ).fromScene( new RoomEnvironment(), 0.02 ).texture;
  87. // HTML element
  88. const element = document.createElement( 'div' );
  89. element.id = 'draw_element';
  90. element.innerHTML = `
  91. Hello world!<br>I'm multi-line, <b>formatted</b>,
  92. rotated text with emoji (&#128512;), RTL text
  93. <span dir=rtl>من فارسی صحبت میکنم</span>,
  94. vertical text,
  95. <p style="writing-mode: vertical-rl;">
  96. 这是垂直文本
  97. </p>
  98. an inline image (<img width="150" src="textures/758px-Canestra_di_frutta_(Caravaggio).jpg">), and
  99. <svg width="50" height="50">
  100. <circle cx="25" cy="25" r="20" fill="green" />
  101. <text x="25" y="30" font-size="15" text-anchor="middle" fill="#fff">
  102. SVG
  103. </text>
  104. </svg>!
  105. <br>
  106. <input type="text" placeholder="Type here...">
  107. <button>Click me</button>
  108. `;
  109. const geometry = new RoundedBoxGeometry( 200, 200, 200, 10, 10 );
  110. const material = new THREE.MeshStandardMaterial( { roughness: 0, metalness: 0.5 } );
  111. material.map = new THREE.HTMLTexture( element );
  112. mesh = new THREE.Mesh( geometry, material );
  113. scene.add( mesh );
  114. // Interaction
  115. interactions = new InteractionManager();
  116. interactions.connect( renderer, camera );
  117. interactions.add( mesh );
  118. // Button click handler
  119. element.querySelector( 'button' ).addEventListener( 'click', function () {
  120. this.textContent = 'Clicked!';
  121. } );
  122. window.addEventListener( 'resize', onWindowResize );
  123. }
  124. function onWindowResize() {
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. }
  129. function animate( time ) {
  130. mesh.rotation.x = Math.sin( time * 0.0005 ) * 0.5;
  131. mesh.rotation.y = Math.cos( time * 0.0008 ) * 0.5;
  132. interactions.update();
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>
粤ICP备19079148号