Skip to content

Commit

Permalink
use babylon as a peer dependency, fixes fenomas#58
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuserik committed Jul 24, 2019
1 parent 4186018 commit 69e936d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"webpack": "^4.35.3",
"webpack-cli": "^3.3.5",
"webpack-dev-server": "^3.7.2"
},
"peerDependencies": {
"@babylonjs/core": "4"
}
}
4 changes: 3 additions & 1 deletion src/lib/objectMesher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict'
import { SolidParticleSystem } from "@babylonjs/core/Particles/solidParticleSystem";



var removeUnorderedListItem = require('./util').removeUnorderedListItem
Expand Down Expand Up @@ -152,7 +154,7 @@ function ObjectMesher() {
function buildSPSforMaterialIndex(chunk, scene, meshHash, x0, y0, z0) {
var blockHash = chunk._objectBlocks
// base sps
var sps = new BABYLON.SolidParticleSystem('object_sps_' + chunk.id, scene, {
var sps = new SolidParticleSystem('object_sps_' + chunk.id, scene, {
updatable: false,
})

Expand Down
49 changes: 28 additions & 21 deletions src/lib/rendering.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
'use strict'
import { Scene } from "@babylonjs/core/scene";
import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
import { Octree } from "@babylonjs/core/Culling/Octrees/octree";
import { OctreeBlock } from "@babylonjs/core/Culling/Octrees/octreeBlock";
import { Engine } from "@babylonjs/core/Engines/engine";
import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight";
import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";
import { Vector3, Axis, Color3 } from "@babylonjs/core/Maths/math";
import { Mesh } from "@babylonjs/core/Meshes/mesh";
import "@babylonjs/core/Meshes/meshBuilder";




var glvec3 = require('gl-vec3')
var aabb = require('aabb-3d')
var sweep = require('voxel-aabb-sweep')
var removeUnorderedListItem = require('./util').removeUnorderedListItem


// For now, assume Babylon.js has been imported into the global space already
if (!BABYLON) {
throw new Error('Babylon.js reference not found! Abort! Abort!')
}

module.exports = function (noa, opts, canvas) {
return new Rendering(noa, opts, canvas)
}

var vec3 = BABYLON.Vector3 // not a gl-vec3, in this module only!!
var col3 = BABYLON.Color3
var vec3 = Vector3 // not a gl-vec3, in this module only!!
var col3 = Color3



Expand Down Expand Up @@ -78,33 +86,32 @@ function Rendering(noa, opts, canvas) {

// Constructor helper - set up the Babylon.js scene and basic components
function initScene(self, canvas, opts) {
if (!BABYLON) throw new Error('BABYLON.js engine not found!')

// init internal properties
self._engine = new BABYLON.Engine(canvas, opts.antiAlias, {
self._engine = new Engine(canvas, opts.antiAlias, {
preserveDrawingBuffer: opts.preserveDrawingBuffer,
})
self._scene = new BABYLON.Scene(self._engine)
self._scene = new Scene(self._engine)
var scene = self._scene
// remove built-in listeners
scene.detachControl()

// octree setup
self._octree = new BABYLON.Octree($ => {})
self._octree = new Octree($ => {})
self._octree.blocks = []
scene._selectionOctree = self._octree

// camera, and empty mesh to hold it, and one to accumulate rotations
self._rotationHolder = new BABYLON.Mesh('rotHolder', scene)
self._cameraHolder = new BABYLON.Mesh('camHolder', scene)
self._camera = new BABYLON.FreeCamera('camera', new vec3(0, 0, 0), scene)
self._rotationHolder = new Mesh('rotHolder', scene)
self._cameraHolder = new Mesh('camHolder', scene)
self._camera = new FreeCamera('camera', new vec3(0, 0, 0), scene)
self._camera.parent = self._cameraHolder
self._camera.minZ = .01
self._cameraHolder.visibility = false
self._rotationHolder.visibility = false

// plane obscuring the camera - for overlaying an effect on the whole view
self._camScreen = BABYLON.Mesh.CreatePlane('camScreen', 10, scene)
self._camScreen = Mesh.CreatePlane('camScreen', 10, scene)
self.addMeshToScene(self._camScreen)
self._camScreen.position.z = .1
self._camScreen.parent = self._camera
Expand All @@ -114,7 +121,7 @@ function initScene(self, canvas, opts) {
self._camLocBlock = 0

// apply some defaults
self._light = new BABYLON.HemisphericLight('light', new vec3(0.1, 1, 0.3), scene)
self._light = new HemisphericLight('light', new vec3(0.1, 1, 0.3), scene)

function arrToColor(a) { return new col3(a[0], a[1], a[2]) }
scene.clearColor = arrToColor(opts.clearColor)
Expand Down Expand Up @@ -210,7 +217,7 @@ var _highlightPos = glvec3.create()

/** @method */
Rendering.prototype.getCameraVector = function () {
return vec3.TransformCoordinates(BABYLON.Axis.Z, this._rotationHolder.getWorldMatrix())
return vec3.TransformCoordinates(Axis.Z, this._rotationHolder.getWorldMatrix())
}
var zero = vec3.Zero()
/** @method */
Expand Down Expand Up @@ -324,7 +331,7 @@ Rendering.prototype.makeMeshInstance = function (mesh, isStatic) {
// Create a default standardMaterial:
// flat, nonspecular, fully reflects diffuse and ambient light
Rendering.prototype.makeStandardMaterial = function (name) {
var mat = new BABYLON.StandardMaterial(name, this._scene)
var mat = new StandardMaterial(name, this._scene)
mat.specularColor.copyFromFloats(0, 0, 0)
mat.ambientColor.copyFromFloats(1, 1, 1)
mat.diffuseColor.copyFromFloats(1, 1, 1)
Expand All @@ -349,7 +356,7 @@ Rendering.prototype.prepareChunkForRendering = function (chunk) {
var cs = chunk.size
var min = new vec3(chunk.x, chunk.y, chunk.z)
var max = new vec3(chunk.x + cs, chunk.y + cs, chunk.z + cs)
chunk.octreeBlock = new BABYLON.OctreeBlock(min, max, undefined, undefined, undefined, $ => {})
chunk.octreeBlock = new OctreeBlock(min, max, undefined, undefined, undefined, $ => {})
this._octree.blocks.push(chunk.octreeBlock)
}

Expand Down Expand Up @@ -486,7 +493,7 @@ function checkCameraEffect(self, id) {
function getHighlightMesh(rendering) {
var m = rendering._highlightMesh
if (!m) {
var mesh = BABYLON.Mesh.CreatePlane("highlight", 1.0, rendering._scene)
var mesh = Mesh.CreatePlane("highlight", 1.0, rendering._scene)
var hlm = rendering.makeStandardMaterial('highlightMat')
hlm.backFaceCulling = false
hlm.emissiveColor = new col3(1, 1, 1)
Expand All @@ -495,7 +502,7 @@ function getHighlightMesh(rendering) {
m = rendering._highlightMesh = mesh
// outline
var s = 0.5
var lines = BABYLON.Mesh.CreateLines("hightlightLines", [
var lines = Mesh.CreateLines("hightlightLines", [
new vec3(s, s, 0),
new vec3(s, -s, 0),
new vec3(-s, -s, 0),
Expand Down
17 changes: 12 additions & 5 deletions src/lib/terrainMesher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
'use strict'
import { Mesh } from "@babylonjs/core/Meshes/mesh";
import "@babylonjs/core/Meshes/meshBuilder";
import { MultiMaterial } from "@babylonjs/core/Materials/multiMaterial";
import { VertexData } from "@babylonjs/core/Meshes/mesh.vertexData";
import { Texture } from "@babylonjs/core/Materials/Textures/texture";
import { SubMesh } from "@babylonjs/core/Meshes/subMesh";




Expand Down Expand Up @@ -249,8 +256,8 @@ function MeshBuilder() {

// base mesh and vertexData object
var scene = noa.rendering.getScene()
var mesh = new BABYLON.Mesh(name, scene)
var vdat = new BABYLON.VertexData()
var mesh = new Mesh(name, scene)
var vdat = new VertexData()
vdat.positions = submesh.positions
vdat.indices = submesh.indices
vdat.normals = submesh.normals
Expand All @@ -265,15 +272,15 @@ function MeshBuilder() {

} else {
// else we need to make a multimaterial and define (babylon) submeshes
var multiMat = new BABYLON.MultiMaterial('multimat ' + name, scene)
var multiMat = new MultiMaterial('multimat ' + name, scene)
mesh.subMeshes = []
// var totalVerts = vdat.positions.length
// var totalInds = vdat.indices.length
var vertStart = 0
var indStart = 0
for (var i = 0; i < mats.length; i++) {
multiMat.subMaterials[i] = mats[i]
var sub = new BABYLON.SubMesh(i, vertStart, verts[i], indStart, inds[i], mesh)
var sub = new SubMesh(i, vertStart, verts[i], indStart, inds[i], mesh)
mesh.subMeshes[i] = sub
vertStart += verts[i]
indStart += inds[i]
Expand Down Expand Up @@ -316,7 +323,7 @@ function MeshBuilder() {
var mat = noa.rendering.flatMaterial.clone('terrain' + id)
if (url) {
var scene = noa.rendering.getScene()
var tex = new BABYLON.Texture(url, scene, true, false, BABYLON.Texture.NEAREST_SAMPLINGMODE)
var tex = new Texture(url, scene, true, false, Texture.NEAREST_SAMPLINGMODE)
if (matData.textureAlpha) tex.hasAlpha = true
mat.diffuseTexture = tex
}
Expand Down

0 comments on commit 69e936d

Please sign in to comment.