customMaterial.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import { TilesRenderer } from '../src/index.js';
  2. import {
  3. Scene,
  4. DirectionalLight,
  5. AmbientLight,
  6. WebGLRenderer,
  7. PerspectiveCamera,
  8. Box3,
  9. OrthographicCamera,
  10. sRGBEncoding,
  11. Group,
  12. ShaderMaterial,
  13. MeshStandardMaterial,
  14. PCFSoftShadowMap,
  15. } from 'three';
  16. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  17. import * as dat from 'three/examples/jsm/libs/dat.gui.module.js';
  18. import Stats from 'three/examples/jsm/libs/stats.module.js';
  19. let camera, controls, scene, renderer, tiles, orthoCamera;
  20. let offsetParent, box, dirLight, statsContainer;
  21. let stats;
  22. const DEFAULT = 0;
  23. const GRADIENT = 1;
  24. const TOPOGRAPHIC_LINES = 2;
  25. const LIGHTING = 3;
  26. const params = {
  27. 'material': DEFAULT,
  28. 'orthographic': false,
  29. 'rebuild': initTiles,
  30. };
  31. const gradientShader = {
  32. vertexShader: /* glsl */`
  33. varying vec3 wPosition;
  34. void main() {
  35. #include <begin_vertex>
  36. #include <project_vertex>
  37. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  38. }
  39. `,
  40. fragmentShader: /* glsl */`
  41. varying vec3 wPosition;
  42. void main() {
  43. float minVal = - 30.0;
  44. float maxVal = 30.0;
  45. float val = ( wPosition.y - minVal ) / ( maxVal - minVal );
  46. vec4 color1 = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  47. vec4 color2 = vec4( 1.0 );
  48. gl_FragColor = mix( color1, color2, val );
  49. }
  50. `,
  51. };
  52. const topoShader = {
  53. extensions: {
  54. derivatives: true,
  55. },
  56. vertexShader: /* glsl */`
  57. varying vec3 wPosition;
  58. varying vec3 vViewPosition;
  59. void main() {
  60. #include <begin_vertex>
  61. #include <project_vertex>
  62. wPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;
  63. vViewPosition = - mvPosition.xyz;
  64. }
  65. `,
  66. fragmentShader: /* glsl */`
  67. varying vec3 wPosition;
  68. varying vec3 vViewPosition;
  69. void main() {
  70. // lighting
  71. vec3 fdx = vec3( dFdx( wPosition.x ), dFdx( wPosition.y ), dFdx( wPosition.z ) );
  72. vec3 fdy = vec3( dFdy( wPosition.x ), dFdy( wPosition.y ), dFdy( wPosition.z ) );
  73. vec3 worldNormal = normalize( cross( fdx, fdy ) );
  74. float lighting =
  75. 0.4 +
  76. clamp( dot( worldNormal, vec3( 1.0, 1.0, 1.0 ) ), 0.0, 1.0 ) * 0.5 +
  77. clamp( dot( worldNormal, vec3( - 1.0, 1.0, - 1.0 ) ), 0.0, 1.0 ) * 0.3;
  78. // thickness scale
  79. float upwardness = dot( worldNormal, vec3( 0.0, 1.0, 0.0 ) );
  80. float yInv = saturate( 1.0 - abs( upwardness ) );
  81. float thicknessScale = pow( yInv, 0.4 );
  82. thicknessScale *= 0.25 + 0.5 * ( vViewPosition.z + 1.0 ) / 2.0;
  83. // thickness
  84. float thickness = 0.01 * thicknessScale;
  85. float thickness2 = thickness / 2.0;
  86. float m = mod( wPosition.y, 3.0 );
  87. // soften edge
  88. float center = thickness2;
  89. float dist = clamp( abs( m - thickness2 ) / thickness2, 0.0, 1.0 );
  90. vec4 topoColor = vec4( 0.149, 0.196, 0.219, 1.0 ) * 0.5;
  91. gl_FragColor = mix( topoColor * lighting, vec4( lighting ), dist );
  92. }
  93. `,
  94. };
  95. init();
  96. animate();
  97. function updateMaterial( scene ) {
  98. const materialIndex = parseFloat( params.material );
  99. scene.traverse( c => {
  100. if ( c.isMesh ) {
  101. c.material.dispose();
  102. switch( materialIndex ) {
  103. case DEFAULT:
  104. c.material = c.originalMaterial;
  105. c.material.side = 2;
  106. c.receiveShadow = false;
  107. c.castShadow = false;
  108. break;
  109. case GRADIENT:
  110. c.material = new ShaderMaterial( gradientShader );
  111. c.material.side = 2;
  112. c.receiveShadow = false;
  113. c.castShadow = false;
  114. break;
  115. case TOPOGRAPHIC_LINES:
  116. c.material = new ShaderMaterial( topoShader );
  117. c.material.side = 2;
  118. c.material.flatShading = true;
  119. c.receiveShadow = false;
  120. c.castShadow = false;
  121. break;
  122. case LIGHTING:
  123. c.material = new MeshStandardMaterial();
  124. c.material.side = 2;
  125. c.receiveShadow = true;
  126. c.castShadow = true;
  127. }
  128. }
  129. } );
  130. }
  131. function onLoadModel( scene ) {
  132. scene.traverse( c => {
  133. if ( c.isMesh ) {
  134. c.originalMaterial = c.material;
  135. }
  136. } );
  137. updateMaterial( scene );
  138. }
  139. function onDisposeModel( scene ) {
  140. scene.traverse( c => {
  141. if ( c.isMesh ) {
  142. c.material.dispose();
  143. }
  144. } );
  145. }
  146. function initTiles() {
  147. if ( tiles ) {
  148. tiles.group.parent.remove( tiles.group );
  149. tiles.dispose();
  150. }
  151. const url = window.location.hash.replace( /^#/, '' ) || '../data/tileset.json';
  152. tiles = new TilesRenderer( url );
  153. tiles.errorTarget = 2;
  154. tiles.onLoadModel = onLoadModel;
  155. tiles.onDisposeModel = onDisposeModel;
  156. offsetParent.add( tiles.group );
  157. }
  158. function init() {
  159. scene = new Scene();
  160. // primary camera view
  161. renderer = new WebGLRenderer( { antialias: true } );
  162. renderer.setPixelRatio( window.devicePixelRatio );
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. renderer.setClearColor( 0x151c1f );
  165. renderer.shadowMap.enabled = true;
  166. renderer.shadowMap.type = PCFSoftShadowMap;
  167. renderer.outputEncoding = sRGBEncoding;
  168. document.body.appendChild( renderer.domElement );
  169. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
  170. camera.position.set( 400, 400, 400 );
  171. orthoCamera = new OrthographicCamera();
  172. // controls
  173. controls = new OrbitControls( camera, renderer.domElement );
  174. controls.screenSpacePanning = false;
  175. controls.minDistance = 1;
  176. controls.maxDistance = 2000;
  177. // lights
  178. dirLight = new DirectionalLight( 0xffffff, 1.25 );
  179. dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
  180. dirLight.castShadow = true;
  181. dirLight.shadow.bias = - 0.01;
  182. dirLight.shadow.mapSize.setScalar( 2048 );
  183. const shadowCam = dirLight.shadow.camera;
  184. shadowCam.left = - 200;
  185. shadowCam.bottom = - 200;
  186. shadowCam.right = 200;
  187. shadowCam.top = 200;
  188. shadowCam.updateProjectionMatrix();
  189. scene.add( dirLight );
  190. const ambLight = new AmbientLight( 0xffffff, 0.05 );
  191. scene.add( ambLight );
  192. box = new Box3();
  193. offsetParent = new Group();
  194. scene.add( offsetParent );
  195. initTiles();
  196. onWindowResize();
  197. window.addEventListener( 'resize', onWindowResize, false );
  198. // GUI
  199. const gui = new dat.GUI();
  200. gui.width = 300;
  201. gui.add( params, 'orthographic' );
  202. gui.add( params, 'material', { DEFAULT, GRADIENT, TOPOGRAPHIC_LINES, LIGHTING } )
  203. .onChange( () => {
  204. tiles.forEachLoadedModel( updateMaterial )
  205. } );
  206. gui.add( params, 'rebuild' );
  207. gui.open();
  208. // Stats
  209. stats = new Stats();
  210. stats.showPanel( 0 );
  211. document.body.appendChild( stats.dom );
  212. statsContainer = document.createElement( 'div' );
  213. statsContainer.style.position = 'absolute';
  214. statsContainer.style.top = 0;
  215. statsContainer.style.left = 0;
  216. statsContainer.style.color = 'white';
  217. statsContainer.style.width = '100%';
  218. statsContainer.style.textAlign = 'center';
  219. statsContainer.style.padding = '5px';
  220. statsContainer.style.pointerEvents = 'none';
  221. statsContainer.style.lineHeight = '1.5em';
  222. document.body.appendChild( statsContainer );
  223. }
  224. function onWindowResize() {
  225. camera.aspect = window.innerWidth / window.innerHeight;
  226. renderer.setPixelRatio( window.devicePixelRatio );
  227. renderer.setSize( window.innerWidth, window.innerHeight );
  228. camera.updateProjectionMatrix();
  229. updateOrthoCamera();
  230. }
  231. function updateOrthoCamera() {
  232. orthoCamera.position.copy( camera.position );
  233. orthoCamera.rotation.copy( camera.rotation );
  234. const scale = camera.position.distanceTo( controls.target ) / 2.0;
  235. let aspect = window.innerWidth / window.innerHeight;
  236. orthoCamera.left = - aspect * scale;
  237. orthoCamera.right = aspect * scale;
  238. orthoCamera.bottom = - scale;
  239. orthoCamera.top = scale;
  240. orthoCamera.near = camera.near;
  241. orthoCamera.far = camera.far;
  242. orthoCamera.updateProjectionMatrix();
  243. }
  244. function animate() {
  245. requestAnimationFrame( animate );
  246. if ( params.orthographic ) {
  247. tiles.deleteCamera( camera );
  248. tiles.setCamera( orthoCamera );
  249. tiles.setResolutionFromRenderer( orthoCamera, renderer );
  250. } else {
  251. tiles.deleteCamera( orthoCamera );
  252. tiles.setCamera( camera );
  253. tiles.setResolutionFromRenderer( camera, renderer );
  254. }
  255. offsetParent.rotation.set( 0, 0, 0 );
  256. if ( params.up === '-Z' ) {
  257. offsetParent.rotation.x = Math.PI / 2;
  258. }
  259. offsetParent.updateMatrixWorld( true );
  260. // update tiles center
  261. if ( tiles.getBounds( box ) ) {
  262. box.getCenter( tiles.group.position );
  263. tiles.group.position.multiplyScalar( - 1 );
  264. }
  265. // update tiles
  266. window.tiles = tiles;
  267. camera.updateMatrixWorld();
  268. orthoCamera.updateMatrixWorld();
  269. tiles.update();
  270. render();
  271. stats.update();
  272. }
  273. function render() {
  274. updateOrthoCamera();
  275. statsContainer.innerText =
  276. `Geometries: ${ renderer.info.memory.geometries } ` +
  277. `Textures: ${ renderer.info.memory.textures } ` +
  278. `Programs: ${ renderer.info.programs.length } `;
  279. renderer.render( scene, params.orthographic ? orthoCamera : camera );
  280. }