123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package hrt.prefab;
- class Model extends Object3D {
- var animation : Null<String>;
- var lockAnimation : Bool = false;
- public function new(?parent) {
- super(parent);
- type = "model";
- }
- override function save() {
- var obj : Dynamic = super.save();
- if( animation != null ) obj.animation = animation;
- if( lockAnimation ) obj.lockAnimation = lockAnimation;
- return obj;
- }
- override function load( obj : Dynamic ) {
- super.load(obj);
- animation = obj.animation;
- lockAnimation = obj.lockAnimation;
- }
- function setCullingCollider( o : h3d.scene.Object, cc : h3d.col.Collider ) {
- o.cullingCollider = cc;
- for( c in @:privateAccess o.children )
- setCullingCollider(c, cc);
- }
- override function makeInstance(ctx:Context):Context {
- if( source == null)
- return super.makeInstance(ctx);
- ctx = ctx.clone(this);
- try {
- var obj = ctx.loadModel(source);
- if(obj.defaultTransform != null && children.length > 0) {
- obj.name = "root";
- var root = new h3d.scene.Object();
- root.addChild(obj);
- obj = root;
- }
- obj.name = name;
- ctx.local3d.addChild(obj);
- ctx.local3d = obj;
- updateInstance(ctx);
- if( animation != null )
- obj.playAnimation(ctx.loadAnimation(animation));
- return ctx;
- } catch( e : Dynamic ) {
- e.message = "Could not load model " + source + ": " + e.message;
- ctx.shared.onError(e);
- }
- ctx.local3d = new h3d.scene.Object(ctx.local3d);
- ctx.local3d.name = name;
- updateInstance(ctx);
- return ctx;
- }
- var tmpBounds = new h3d.col.Bounds();
- override function updateInstance( ctx: Context, ?propName : String ) {
- super.updateInstance(ctx, propName);
- var o = ctx.local3d;
- #if editor
- var cc = o.getBounds(tmpBounds).toSphere();
- setCullingCollider(o, cc);
- #end
- }
- #if editor
- override function edit( ctx : EditContext ) {
- super.edit(ctx);
- var props = ctx.properties.add(new hide.Element('
- <div class="group" name="Animation">
- <dl>
- <dt>Model</dt><dd><input type="model" field="source"/></dd>
- <dt>Animation</dt><dd><select><option value="">-- Choose --</option></select>
- <dt title="Don\'t save animation changes">Lock</dt><dd><input type="checkbox" field="lockAnimation"></select>
- </dl>
- </div>
- '),this, function(pname) {
- ctx.onChange(this, pname);
- });
- var select = props.find("select");
- var anims = try ctx.scene.listAnims(source) catch(e: Dynamic) [];
- for( a in anims )
- new hide.Element('<option>').attr("value", ctx.ide.makeRelative(a)).text(ctx.scene.animationName(a)).appendTo(select);
- if( animation != null )
- select.val(animation);
- select.change(function(_) {
- var v = select.val();
- var prev = animation;
- var obj = ctx.getContext(this).local3d;
- ctx.scene.setCurrent();
- if( v == "" ) {
- animation = null;
- obj.stopAnimation();
- } else {
- obj.playAnimation(ctx.rootContext.loadAnimation(v)).loop = true;
- if( lockAnimation ) return;
- animation = v;
- }
- var newValue = animation;
- ctx.properties.undo.change(Custom(function(undo) {
- var obj = ctx.getContext(this).local3d;
- animation = undo ? prev : newValue;
- if( animation == null ) {
- obj.stopAnimation();
- select.val("");
- } else {
- obj.playAnimation(ctx.rootContext.loadAnimation(animation)).loop = true;
- select.val(v);
- }
- }));
- });
- }
- override function getHideProps() : HideProps {
- return {
- icon : "cube", name : "Model", fileSource : ["fbx","hmd"],
- allowChildren : function(t) return Library.isOfType(t,Object3D) || ["material", "shader"].indexOf(t) >= 0,
- onResourceRenamed : function(f) animation = f(animation),
- };
- }
- #end
- static var _ = Library.register("model", Model);
- }
|