Search Unity

webcam capture and use it as texture

Discussion in 'Getting Started' started by sinafa, Jan 9, 2017.

  1. sinafa

    sinafa

    Joined:
    Jan 9, 2017
    Posts:
    5
    hi my dear friends
    i want to have a button which when clicked webcamera run and can shot and capture picture ... then use that picture as texture of some of my models ...
    it means that i want to give my customers this ability to change the model texture via their mobile camera

    is it possible ?
    im very poor in programing ... please explain it very clearly and simple
    thanks alot
     
  2. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Hello,

    For the usage of a webcam:
    You have to use a WebCamTexture
    Here is a ready to use script example.
    You just have to copy the content of the example in a new C# script (the new script file have to be called "ExampleClass").
    Then attach the script on a plane or a quad you can create within Unity.

    Now about your models texture shot thing, it's easy, but it get way more complicated when you want your models to actually look good, depending on what you want to do.

    To achieve that you just have to take a picture and apply the picture texture on the material you want.

    Here is how.
     
    Last edited: Jan 9, 2017
    izanette likes this.
  3. sinafa

    sinafa

    Joined:
    Jan 9, 2017
    Posts:
    5

    thank you very much dear fabian

    i used this code and every thing is good ...but i have some other questions ... it takes a shot and saved it in drive C ...
    i want to publish my app for android and ios ...
    which path should i assign in this part of code that i can load it for texture !
    Code (CSharp):
    1. private string _SavePath = "C:/WebcamSnaps/"; //Change the path here!
    and how can i load that shot to my texture ?

    thanks a lot
     
  4. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
  5. sinafa

    sinafa

    Joined:
    Jan 9, 2017
    Posts:
    5
    it means that i have to write code like this:
    Code (CSharp):
    1. private string _SavePath =Application.persistentDataPath;
    i did that but i faced this error:

    and for loading image it means that i have to use this code:
    Code (CSharp):
    1. // Add this script to a GameObject. The Start() function fetches an
    2. // image from the documentation site.  It is then applied as the
    3. // texture on the GameObject.
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7.  
    8. public class ExampleClass : MonoBehaviour {
    9.  
    10.     public string url = "https://docs.unity3d.com/uploads/Main/ShadowIntro.png";
    11.  
    12.     IEnumerator Start() {
    13.  
    14.         Texture2D tex;
    15.         tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
    16.         WWW www = new WWW(url);
    17.         yield return www;
    18.         www.LoadImageIntoTexture(tex);
    19.         GetComponent<Renderer>().material.mainTexture = tex;
    20.     }
    21. }
    and change the url to my path ?
    something like this:

    Code (CSharp):
    1. public string url = Application.persistentDataPath+"/0.png";
    ??
     
    Last edited: Jan 9, 2017
  6. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    If you want to code you should learn the basis of programming before trying to do this or ask a programmer to do it for you against a proper exchange.

    https://unity3d.com/fr/learn/tutorials/topics/scripting

    I have the feeling this thread will be a "I will do all the work for you".

    Good luck,
     
    Psyco92 likes this.
  7. sinafa

    sinafa

    Joined:
    Jan 9, 2017
    Posts:
    5
    thanks dear fabian

    is there anyone else to help me more ?
     
  8. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You already got help: You were pointed to the tutorials!

    Understand that this issue you're asking us to solve is not the only one you're going to run into. And you can't just come ask someone to give you code each time something doesn't work. Well, you can, but don't expect people to do it.

    If you want to be a game developer, you need to learn how to develop. That means either learning how to write code, or learning how to use a visual scripting tool (and accepting the things you sacrifice when you do so). It's hard work, and no one can do it for you.

    When you run into specific issues and show us that you're working to solve it but need help, or you need further direction after you've followed the steps advised, we'll be here and will be happy to assist.
     
    Psyco92, Ryiah and Fabian-Haquin like this.