Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. AR - Planting Trees (Difficulty: 2)

AR - Planting Trees (Difficulty: 2)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Key Topics Covered

    • Initializing 3D scenes
    • Using models
    • Using planes
    • AR world camera
    • Picking 3D objects
    • Broadcasting messages

     

    Introduction

    In this tutorial, you will build an AR game. The player can click anywhere on the ground/floor to plant a tree.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d2c5c564-f203-45fd-8324-a1641db32c12.gif" width="450" style="border-radius: 5px; border: 1px solid #29622d;">
     
    Note that for the first 10 steps, you can use any computer to build the project. But for the last 2 steps, you will need to run the project in the browser of a mobile device with a back camera, such as a touchpad or a smartphone.

     
     

    Step 1 - An “Empty” Scene with 3D Axes

    Please create a new project, remove the dog sprite, and add these blocks in the “Empty1” sprite. Please rename this sprite to “ground”, since it will contain the ground object.

    You can find the “initialize 3D scene” and the “show 3D axis” blocks in the “3D Scene” category.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/43a6f24e-b195-4762-a219-822a46559d96.png" width="400" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
     
    As shown, the X-axis points to the right, the Y-axis points forward (into the screen), and the Z-axis points up. You can drag your mouse on the stage to rotate the camera around it:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dcebc38b-cc87-4eb8-a3f1-6252211315eb.gif" width="460" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 2 - Add a huge plane

    In the “ground” sprite, let’s add a huge plane, with a size of 1000000 in each direction. Its color is not important, as we will make it transparent in a later step.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/454b54c5-f060-4b82-854f-2a741d8a2e16.png" width="500" style="">
     
    This is what you’ll get:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2cd894bc-b684-430b-96eb-67ad41c80c30.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 3 - Turn on Pointer Picking

    Next, we will turn on pointer picking, so that the player can pick anywhere on the plane.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8a5c25db-efdd-4572-98e3-7b6a135bf321.png" width="600" style="">
     
    Note that for the last input of the new block, we are leaving it empty, so any object in the current sprite will become pickable, including the plane we have added in step 2. You can also write “ground” here, which will give us the same result.

     
     

    Step 4 - Store the Picked Positions

    Now let’s handle the picking event. As the first step, let’s add 2 new variables “target x” and “target y”. They will be used to store the x and y positions of the newly picked point:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f701e9c0-a1f5-4c1c-bce5-13728704bd02.png" width="600" style="">
     
    If you turn on the monitors of these 2 variables, you will see their values are changing whenever you click on the ground:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3ce68063-09f5-405c-9b76-08f652da98c7.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 5 - Add a new sprite named “trees”

    To add new trees into the 3D world, we will first need to add another sprite named “trees”. This is a critical step. If we add new trees in the current “ground” sprite, then those trees will become pickable as well, which will be confusing to the player.

    In general, for such world-tracking AR projects, the ground plane is very important. We only want to make the ground plane to be pickable, so we will add all other 3D objects in other sprites.

    Now we have 2 sprites: “ground” and “trees”.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/22342a10-76fc-4e47-a698-eefc5a0684d4.png" width="200" style="">

     
     

    Step 6 - Send and Receive the “New Point” Message

    We will use messages to communicate between these 2 sprites.

    In the “ground” sprite, we broadcast a new message “new point” every time the player picks a new point on the ground:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/27bae32f-c7f6-406a-8b8f-e4bb3b9277f0.png" width="400" style="">
     
    In the “trees” sprite, we will receive this message so we can add trees later:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8ab1cfcc-3b78-47e2-9b7e-39c5649cd10b.png" width="300" style="">

     
     

    Step 7 - Add a Tree Model When Message Received

    In the “trees” sprite, let’s add a new tree whenever the “new point” message is received. By default the new tree is always added at the center of the scene, so we also need to move it to the point that’s picked by the player. That point’s positions are stored in the “target x” and “target y” variables.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/16f80fc8-cab8-497e-8107-efa975d145fc.png" width="1000" style="">
     
    Now you will plant a new tree every time you click on the ground:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8fb0d012-7096-43a9-acac-8a46861d84a1.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 8 - Hide and Show

    There is a minor issue with our solution. The new tree would always show up at the origin point, then move to the picked point. Here is a slow-motion view:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6cc828b2-dc99-4f8e-856c-0161f8d32930.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
     
    To make the tree appear at the picked point directly, we can use a simple technique:

    • When we add the tree model, we will set it to be hidden
    • After moving the tree to the target point, we change it to visible using the “show” block

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8d2ae739-5f3b-4a72-a653-4eba4267da28.png" width="1000" style="">
     
    After this change, the tree will show up wherever we click:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cd23088c-529e-44d9-8300-991797262cad.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 9 - Make the Tree Grow

    To make it more fun, let’s add an animation that makes the tree grow up. This can be done with 2 changes:

    • When we add the new tree, we can make it very small, with a target height of 20.
    • After the tree becomes visible, we scale it up by 10 times within a very short time.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4726601d-bf55-4ef7-8318-36424963a2d1.png" width="1000" style="">
     
    Now we get an animation of the tree growing up:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/56d9c4a2-1cd3-4403-8fa1-b9007e1b8751.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 10 - Switch to the AR world tracking emulation

    So far we have not done anything related to AR yet. It’s finally time to switch to AR mode. Back in the “ground” sprite, please add the “switch to AR world camera” block. We will start with the emulation mode, and check if everything works fine.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3331c30c-9264-4545-a2dd-fb87705c5dc1.png" width="700" style="">
     
    This is what we get. The camera view is a bit different, but the picking event is working the same way:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9d5901a0-3f7f-4706-b5ff-ebe63434790f.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 11 - Connect to the Real Camera

    Now we are ready to connect to the real camera. You will need to open this project on a mobile device, such as a touchpad or smartphone.

    You will just need to change the emulation mode to “No” to turn on the real camera. In addition, let’s set the ground’s color to be 50% transparent, so you can see through it.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9290ed52-edcb-42a9-86d4-d3f02113e225.png" width="600" style="">
     
    When you run the project on the mobile device, the images from the back camera will replace the blue background of the scene, and you can still click on the ground to plant trees:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/519942a7-1a49-4056-955b-cf6473de3288.gif" width="416" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 12 - Clean Up

    The ground and the 3D axis are helpful in building the project, but they are clearly destroying the illustration of adding objects to the real scene. Before releasing the project, we need to set the ground plane to be 100% transparent. Note that the picking event will still work on the transparent ground, although we can not see it anymore. Also, let’s set the 3D axis to hidden as well.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/044e3cfe-2bb3-4179-bf97-f7e797c71899.png" width="600" style="">
     
    And here is the final result:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d2c5c564-f203-45fd-8324-a1641db32c12.gif" width="450" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Creative Ideas

    There are many fun projects you can create using the same techniques. Here are some ideas you might want to explore:

    • Other Object Types: you can add any other types of objects, like shapes, models and avatars. You can also use buttons or dropdowns to allow the player to choose which type of object to add.

    • Randomize it: Each time a new object is added, you can set its size or color randomly

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on
    • JaecadeJnightJ JaecadeJnight referenced this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode