UNPKG

awv3

Version:
1 lines 4.5 kB
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}exports.__esModule=!0;var _extends2=require("babel-runtime/helpers/extends"),_extends3=_interopRequireDefault(_extends2),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_possibleConstructorReturn2=require("babel-runtime/helpers/possibleConstructorReturn"),_possibleConstructorReturn3=_interopRequireDefault(_possibleConstructorReturn2),_inherits2=require("babel-runtime/helpers/inherits"),_inherits3=_interopRequireDefault(_inherits2),_three=require("three"),_three2=_interopRequireDefault(_three),_object=require("../three/object3"),_object2=_interopRequireDefault(_object),canvas2d=document.createElement("canvas");canvas2d.width=128,canvas2d.height=128;var context=canvas2d.getContext("2d"),gradient=context.createRadialGradient(canvas2d.width/2,canvas2d.height/2,0,canvas2d.width/2,canvas2d.height/2,canvas2d.width/2);gradient.addColorStop(.1,"rgba(0,0,0,0.15)"),gradient.addColorStop(1,"rgba(0,0,0,0)"),context.fillStyle=gradient,context.fillRect(0,0,canvas2d.width,canvas2d.height);var shadowTexture=new _three2["default"].CanvasTexture(canvas2d),shadowMaterial=new _three2["default"].MeshBasicMaterial({map:shadowTexture,transparent:!0}),Presentation=function(e){function t(a){var o,i=arguments.length<=1||void 0===arguments[1]?{}:arguments[1],s=arguments.length<=2||void 0===arguments[2]?void 0:arguments[2];(0,_classCallCheck3["default"])(this,t);var r=(0,_possibleConstructorReturn3["default"])(this,e.call(this));return r.criteria=s,r.options=(0,_extends3["default"])({shadow:!0,lights:!0,edges:.2,ambient:1},i),r._objects=new _object2["default"],r._stage=new _object2["default"],r._lights=new _object2["default"],e.prototype.add.call(r,r._stage),e.prototype.add.call(r,r._lights),e.prototype.add.call(r,r._objects),r._objects.on((o={},o[_object2["default"].Events.Lifecycle.ChildAdded]=function(e){},o)),a&&r.add(a),r}return(0,_inherits3["default"])(t,e),t.prototype.add=function(e){var t=this;this._objects.add(e),this._objects.centerGeometry().updateMaterials(),this._objects.traverse(function(e){if("Mesh"===e.type){var a={color:e.material.color,emissive:new _three2["default"].Color(0),metalness:.5,roughness:1,clearCoat:0,clearCoatRoughness:1};t.criteria&&(a=(0,_extends3["default"])({},a,t.criteria({item:e,radius:e.getRadius(),hsl:e.material.color.getHSL()},a))),e.castShadow=!0,e.receiveShadow=!0,e.material=new _three2["default"].MeshPhysicalMaterial((0,_extends3["default"])({reflectivity:1,fog:!1,polygonOffset:!0,polygonOffsetFactor:1,polygonOffsetUnits:1},a))}else"LineSegments"===e.type&&(t.options.edges?e.material.opacity=t.options.edges:e.destroy())}),this.update()},t.prototype.update=function(){var e=this;this.viewFound().then(function(){e._objects.updateBounds();var t=e._objects.bounds.box.min,a=e._objects.bounds.box.max,o=a.distanceTo(new _three2["default"].Vector3(t.x,a.y,a.z)),i=a.distanceTo(new _three2["default"].Vector3(a.x,t.y,a.z)),s=a.distanceTo(new _three2["default"].Vector3(a.x,a.y,t.z));if(e._stage.position.copy(e._objects.position),e._stage.position.y-=s/1.5,e._stage.rotation.x=-Math.PI/2,e.options.ambient){var r=new _three2["default"].AmbientLight(16777215);r.intensity=e.options.ambient,e._lights.add(r)}var n=6*e._objects.updateBounds().getRadius();e._lights.add(new _three2["default"].PointLight(16777215,.5).setPosition(-n,0,0)),e._lights.add(new _three2["default"].SpotLight(16777215,.5).setPosition(0,-n,2*-n)),e._lights.add(new _three2["default"].PointLight(16777215,.5).setPosition(n,0,0)),e._lights.add(new _three2["default"].PointLight(16777215,.5).setPosition(0,n,0)),e._lights.add(new _three2["default"].PointLight(16777215,1).setPosition(0,0,n));var d=e.parent.worldToLocal(new _three2["default"].Vector3(n/2,n/2,n)),l=new _three2["default"].SpotLight(16777215,3,1.5*n,Math.PI/6);l.position.copy(d),l.target=e._objects,l.castShadow=!0,l.penumbra=1,l.shadow=new _three2["default"].LightShadow(new _three2["default"].PerspectiveCamera(10,1,100,1.5*n)),l.shadow.mapSize.width=2048,l.shadow.mapSize.height=2048,e._lights.add(l);var h=new _three2["default"].PlaneBufferGeometry(1.2*o,1.2*i,1,1),u=new _three2["default"].Mesh(h,shadowMaterial);e._stage.add(u)})},t.prototype.getModels=function(){return this._objects},t.prototype.getLights=function(){return this._lights},t.prototype.getStage=function(){return this._stage},t}(_object2["default"]);exports["default"]=Presentation;