Code Monkey home page Code Monkey logo

arjs_unity's Introduction

Unity tool for AR.js

This tool is in it's early stages, but is meant to help the user to build an AR.js project using Unity to help make the 3D object placement a breeze. This project is by no means complete, and currently only supports changing the image target, adding basic shapes to the scene, changing the shapes' colors or textures, adding .obj models, simple linear animations, plane videos, and the ability to click an object to trigger an animation or open a webpage.

Setup

Download the zip file from GitHub, extract the folder, then drag and drop it into the Assets folder within your Unity project, and make sure the name is "ARjs_Unity". If the name of the folder is "ARjs_Unity-master", change it to "ARjs_Unity". Currently, the name and placement of the folder is important, or else it won't work. After this has been added to your project, download the AR.js project from GitHub. Take the "AR.js-master" folder and drop that into the Assets folder as well. Again, name and placement of the folder matters. And this folder should be named "AR.js-master".

After that, you can get started. You can seperate the different ARjs projects your're working on by making different scenes. The project will compile to Assets>AR.js-master>aframe>{Active Scene Name}. This means if you have two different scenes in different folders, but with the same name, they will overwrite each other when compiling the ARjs project. It will also (likely) overwrite the animations on your game objects when you export animations if the scenes are the same name.

Usage

Changing the Image Target

There are two menu items for this. "AR.js/Image Target/1. Generate Image Target" and "AR.js/Image Target/2. Apply Generated Image". The first one will open up the website where you can go to generate both the image and the .patt file that you will need. The second one, should only be used after you already have an ImageTarget in scene. It will open another window where you will need drag and drop both the .patt file and the target image you created. Then hit update. If you don't alter anything before hitting update, it will reset the target to the default HIRO image target.

Adding Normal Objects

Right click the Hierarchy to add an image target, then right click the image target to add a shape.

Texturing Normal Objects

You can right click in your assets folder to create a new material, with the new material selected, drag and drop an image under the material's "albedo" option, or simply change the color of the material. Then you can drag and drop this material onto your object.

Adding Custom Model Objects

If you right click the image target, you can add a custom model to the scene. Custom models require .mtl files to be textured. And sometimes .mtl files require textures in order to work. After clicking on "AR.js>Custom Model" a window will appear. Drag and drop the .obj, .mtl, and texture file into the appropriate places in the window. Then click the button and the files will both get copied from wherever it was to the aframe/{scene name}/models folder and appear in the scene that you're working in. If you want to learn how to make simple objects in Unity click here

Button Objects

Select the object in question that you want to make into a button. Then click the "AR.js" menu item, and select "Make Button." You can then change the URL that the button links to in the inspector when the object is selected. NOTE: buttons don't work the best on mobile for some reason. You kinda need to tap the object many time in quick succession, but on the computer using the mouse it always gets it on just one click.

Videos

Videos are very similar to Planes. You can add a video by right clicking the image target, selecting AR.js>Video. The Video object has a child also called "Video". The child called Video has a video player component. Drag and drop your video clip into the Video Players empty spot called "Video Clip". Select the parent Video in order to change the position, scale and rotation of the video. If you're changing the transform of the child, it won't export that information correctly. NOTE: Adding a video will also add a Mute/Unmute button to the webpage. This is mostly for iOS as it is required to have a mute button in order for there to be sound on the video.

Animating Objects (Linear)

Select the object that you want to animate, click the "AR.js" menu item, and select "Make Animation" then select "Linear." This will add two scripts to the object. The one you will need to use for adding keyframes to your animation is the Custom List script (that I modified from this original post).

You can move your object anywhere and record that as a point (change the FrameTime to change when the object is supposed to get to that position). It will then be added to a list of keyframes. Note: you need to have at least one keyframe at time 0. After adding it to the list, each keyframe can be expanded and edited. If you want to check what that keyframe actually looks like, you can click "Set Transform from Frame," and it will set the transform of your object to what was recorded to that frame. If you move your object around a bit, and click "Update Transform" it will change the position, rotation, and scale for that keyframe to the objects current transform. You can change the time of the keyframe and it will be reordered in the list of keyframes to be chronological. After you have all your keyframes, Click "Export Animation." This will create a JSON file of all the keyframes you have made for the animation. If you then click the play button, you can see what your animation looks like.

There is a loop option, and a click option that you can enable. If checked, the "Loop Animation" option will cause the animation to loop. The "On Click" option will make the animation trigger upon your click (though only when you finally compile to HTML, and not when you click play in the Unity Editor).

Animating Objects (Bezier Curve)

There is a lot I need to add here for it to be fully functional. Particularly a way of also rotating and scaling the objects. Right now, it only works with positional animation. And I need to add in several options like execute on click. Right now though, you can use it and get a feel for how this feature works.

On an AR.js object, click AR.js > Make Animation > Nonlinear Positional. This adds the Bezier Manager as a child to the object with two points. When the "BezierManager(Clone)" is selected in the inspector, you can create new points, loop/unloop the path (connect or disconnect the last point to the start point), or show/hide all the points and lines. When one of the points is selected, you can delete it. When a control point is selected, you can select "locked" and it will make the rotation of the opposite side control point mirror the control point you're moving. This locked feature allows you to more easily keep your animation curve smooth.

Compiling to HTML

For Testing locally on your computer, click the "AR.js" menu item, then click "Compile Files>Testing" (this will prevent an error that causes textures not to render). For publishing to a website, click "CompileFiles>Final" (this will improve the clickablity of objects on mobile devices. Both of these options will create a file located in Assets>AR.js-master>aframe>{Active Scene Name}>index.html

This is the final file and can be opened in a browser to view the AR experience using either the default "Hiro" marker provided from Jerome's AR.js GitHub, or the custom marker that you've created and applied yourself. If you open it in FireFox, you don't have to worry about running it on a localhost server for it to work properly.

How it Works

The CompileFile.cs script is where the majority of the work happens. It's just a lot of loops and conditional statements that goes through ever object attached to the ImageTarget in scene, and adds HTML text to a StringBuilder accordingly then saves the giant string as a file called index.html

Acknowledgments

A huge shout out to Jerome Etienne for enabling Augmented Reality through any web browser. GitHub again

Thank you to ForceX for the Custom List template I'm using to make the KeyFrame list easily editable.

Thank you to Amit Kapdi from The App Guruz for his fantastic article and code for making Bezier Curves in Unity.

Thank you to Lee Stemkoski for his fantastic examples that helped me figure out how to implement animating curved paths in JavaScript.

Also thank you to Or-Aviram for his Draw Field on condition. Which I'm not actively using, but the code is added to my project and I anticipate that I will be using it eventually.

Demonstration

Using AR.js Unity Converter

arjs_unity's People

Contributors

taylordigital13 avatar weldonla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

arjs_unity's Issues

Works with Apple chip and World tracking?

Hi there, I'm new to webAR, and I would like to know if this extension for unity can work with the unity version for the Apple chip and if I can place an object in the 3D world.

Could anyone tell me this?

I appreciate any help you can provide.

Custom scripts for targeted objects.

Hi.
I'm working on a web-based AR app where I procedurally create objects as the image marker is recognized. Then I can touch on these virtual objects and they do different things.
Question is: does this workflow compile standard Unity C# scripts and make them work on web?

Unable to add image target

Hi all,
I'm new to AR.js. Unity kept giving me error when I tried to click update on my custom image target files downloaded from the marker trainer. I'm using Unity Version 2019.4.f21. Am I doing something wrong or is there something I need to change in the C# file in Unity ? Kindly help.
Thank you in advance.

Full error listed below the screen cap.

Screen Shot 2021-03-03 at 11 27 51 PM

UnityException: Texture 'pattern-stealing stars tracking' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
UnityEngine.Texture2D.GetPixel (System.Int32 x, System.Int32 y) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Graphics/Texture.cs:587)
ChangeImageTarget.RemoveWhiteBorder (UnityEngine.Texture2D texture) (at Assets/ARjs_Unity/Editor/ChangeImageTarget.cs:89)
ChangeImageTarget.OnWizardCreate () (at Assets/ARjs_Unity/Editor/ChangeImageTarget.cs:67)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at :0)
UnityEditor.ScriptableWizard.OnGUI () (at /Users/bokken/buildslave/unity/build/Editor/Mono/ScriptableWizard.cs:79)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at :0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:381)
UnityEditor.HostView.Invoke (System.String methodName) (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:375)
UnityEditor.HostView.OldOnGUI () (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:151)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:293)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:508)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:491)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:484)
UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUI (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:477)
UnityEngine.UIElements.IMGUIContainer.HandleEvent (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:449)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/Events/EventHandler.cs:77)
UnityEngine.UIElements.MouseCaptureDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/Events/MouseCaptureDispatchingStrategy.cs:92)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:310)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:275)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:238)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:203)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:44)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:301)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:159)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/Panel.cs:257)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/UIElementsUtility.bindings.cs:404)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at /Users/bokken/buildslave/unity/build/Modules/UIElements/UIElementsUtility.bindings.cs:194)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at /Users/bokken/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:197)

Touch event

Hey, great project.!Could you please let me know could I somehow register click/pointermove/swipe event on object or screen?

Question : Build as self contained WebGL application

Hi,
Was looking though the stuff thats done here and was wondering if there is a way to export as a self contained webgl application, rather than an export to go into an already existing site.

Im looking to build a web gl application that has a load of normal (non-ar) behaviour, but if you boot it up on a mobile device then you can enter/exit the AR mode.

Just wondering do you think this would be possiable?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.