webgl_materials_texture_html.html 5.2 KB

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