Guest User

Untitled

a guest
Jan 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. // budo boop.ts --live --dir . -- -p [ tsify --target es6 ]
  2.  
  3. import * as B from 'babylonjs'
  4. import * as parseMagicaVoxel from 'parse-magica-voxel'
  5. import * as createAOMesh from 'ao-mesher'
  6. import * as fill from 'ndarray-fill'
  7. import * as ndarray from 'ndarray'
  8.  
  9. let canvas : any = document.createElement( "canvas" );
  10. document.body.appendChild(canvas)
  11. document.documentElement.style.cssText =
  12. document.body.style.cssText =
  13. 'margin: 0; padding: 0; height: 100%'
  14. canvas.style.cssText = 'width: 100%; height: 100%'
  15.  
  16. let engine = new B.Engine( canvas, true );
  17. let scene = new B.Scene( engine );
  18.  
  19. //
  20. // Camera
  21. //
  22. let camera = new B.UniversalCamera('camera1', new B.Vector3(0, 2, 2), scene);
  23. camera.setTarget( BABYLON.Vector3.Zero() );
  24. camera.attachControl( canvas, true );
  25.  
  26. //
  27. // Lighting
  28. //
  29. let light1 = new B.HemisphericLight('light1', new B.Vector3(0,5,0), scene);
  30. light1.intensity = 0.5
  31.  
  32. var light2 = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-10, -20, -10), scene)
  33. var shadowGenerator = new BABYLON.ShadowGenerator(2048, light2)
  34. shadowGenerator.usePoissonSampling = true;
  35.  
  36. let mat = new B.StandardMaterial( "mat", scene );
  37. mat.diffuseColor = new B.Color3( 0.8, 0, 0 );
  38. // sphere.material = mat;
  39.  
  40. engine.runRenderLoop( () => {
  41. scene.render();
  42. })
  43.  
  44. window.addEventListener( 'resize', () => {
  45. engine.resize();
  46. })
  47.  
  48. fetch('./lab-02.vox')
  49. .then(r => r.arrayBuffer())
  50. .then(buffer => {
  51. let parsed = parseMagicaVoxel(buffer)
  52. console.log(parsed)
  53.  
  54. let size = parsed.SIZE
  55.  
  56. // Oversize because ao-mesher doesn't create faces on the boundaries
  57. size.x += 2
  58. size.y += 2
  59. size.z += 2
  60.  
  61. // Construct an ndarray
  62. let field = ndarray(new Uint16Array(size.x * size.y * size.z), [size.x, size.y, size.z])
  63. fill(field, (x, y, z) => 0)
  64.  
  65. parsed.XYZI.forEach(row => {
  66. let { x, y, z, c } = row
  67. field.set(x, y, z, c + (1 << 15))
  68. })
  69.  
  70. const vertData = createAOMesh(field)
  71.  
  72. // Convert the vertData format into a babylon.js Mesh
  73. let face = 0
  74. let i = 0
  75. let s = 1
  76.  
  77. const hue = 0
  78. const positions = []
  79. const indices = []
  80. const normals = []
  81. const colors = []
  82.  
  83. // Identity function, use these to nudge the mesh as needed
  84. const fx = x => x
  85. const fy = y => y
  86. const fz = z => z
  87.  
  88. while (i < vertData.length) {
  89. const textureIndex = vertData[i + 7]
  90.  
  91. // const color = new B.Color3(1, 1, 0)
  92. var a = new B.Vector3(vertData[i + 0], vertData[i + 1], vertData[i + 2])
  93. positions.push(fx(vertData[i + 0] * s))
  94. positions.push(fy(vertData[i + 1] * s))
  95. positions.push(fz(vertData[i + 2] * s))
  96. i += 8
  97.  
  98. var b = new B.Vector3(vertData[i + 0], vertData[i + 1], vertData[i + 2])
  99. positions.push(fx(vertData[i + 0] * s))
  100. positions.push(fy(vertData[i + 1] * s))
  101. positions.push(fz(vertData[i + 2] * s))
  102. i += 8
  103.  
  104. var c = new B.Vector3(vertData[i + 0], vertData[i + 1], vertData[i + 2])
  105. positions.push(fx(vertData[i + 0] * s))
  106. positions.push(fy(vertData[i + 1] * s))
  107. positions.push(fz(vertData[i + 2] * s))
  108. i += 8
  109.  
  110. // Face index
  111. indices.push(face + 0, face + 2, face + 1)
  112.  
  113. const intensity = 0.5
  114. const offset = 0.4
  115. let color = new B.Color3(parsed.RGBA[textureIndex].r / 255, parsed.RGBA[textureIndex].g / 255, parsed.RGBA[textureIndex].b / 255)
  116.  
  117. colors.push(color.r * (vertData[i - 24 + 3] / 255 * intensity + offset))
  118. colors.push(color.g * (vertData[i - 24 + 3] / 255 * intensity + offset))
  119. colors.push(color.b * (vertData[i - 24 + 3] / 255 * intensity + offset))
  120. colors.push(1)
  121.  
  122. colors.push(color.r * (vertData[i - 16 + 3] / 255 * intensity + offset))
  123. colors.push(color.g * (vertData[i - 16 + 3] / 255 * intensity + offset))
  124. colors.push(color.b * (vertData[i - 16 + 3] / 255 * intensity + offset))
  125. colors.push(1)
  126.  
  127. colors.push(color.r * (vertData[i - 8 + 3] / 255 * intensity + offset))
  128. colors.push(color.g * (vertData[i - 8 + 3] / 255 * intensity + offset))
  129. colors.push(color.b * (vertData[i - 8 + 3] / 255 * intensity + offset))
  130. colors.push(1)
  131.  
  132. face += 3
  133. }
  134.  
  135. // Create transferrable objects
  136. let positionsArray = new Float32Array(positions)
  137. let indicesArray = new Float32Array(indices)
  138. let colorsArray = new Float32Array(colors)
  139.  
  140. let mesh = new B.Mesh('voxel-mesh', scene)
  141. mesh.scaling.set(0.25, 0.25, 0.25)
  142. mesh.rotation.set(-Math.PI / 2, 0, 0)
  143. mesh.position.set(-8, -2, 8)
  144. mesh.receiveShadows = true
  145.  
  146. var mat = new BABYLON.StandardMaterial('voxel-material', scene);
  147. mat.specularColor = new BABYLON.Color3(0, 0, 0)
  148. mesh.material = mat
  149.  
  150. var vertexData = new B.VertexData()
  151. B.VertexData.ComputeNormals(positions, indices, normals)
  152.  
  153. // Assign positions, indices and normals to vertexData
  154. vertexData.positions = positions
  155. vertexData.indices = indices
  156. vertexData.normals = normals
  157. vertexData.colors = colors
  158.  
  159. // Apply vertexData to custom mesh
  160. vertexData.applyToMesh(mesh)
  161.  
  162. shadowGenerator.getShadowMap().renderList.push(mesh)
  163. })
Add Comment
Please, Sign In to add comment