Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by usman_metadort · Mar 04, 2020 at 09:36 AM · shadertexturescript.

How to Apply a Texture Over Another Texture in unity at runtime in unity

I wanted to replace the part of texture with another texture using raycast hit ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MarshCZA · Mar 04, 2020 at 02:45 PM

Raycast Hit can be used to get the targeted pixel coordinate of your texture, you could then set up a grid centred around that pixel which is the size of your new texture (in pixels) and set the old texture's pixels to be the pixels of your new texture. The code here is a good starting point: https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html To use that code you would then need to look at nearby pixels on the old texture and update their colours by referencing the pixels in your new texture.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image usman_metadort · Mar 06, 2020 at 06:41 AM 0
Share

Firt Of All Thank You For Your Help :). I get Want I Needed but i wanted to Increase The Size Of the Brush. This Is $$anonymous$$y Code. But In This Code The Size of the Brush is Too Small. How I Can Increase The Size Of The Brush.

public Camera cam; public Texture2D mappingTexture;

 void Start()
 {
     cam = GetComponent<Camera>();
 }

 void Update()
 {
     if (!Input.Get$$anonymous$$ouseButton(0))
         return;

     RaycastHit hit;
     if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
         return;

     Renderer rend = hit.transform.GetComponent<Renderer>();
     $$anonymous$$eshCollider meshCollider = hit.collider as $$anonymous$$eshCollider;

     if (rend == null || rend.shared$$anonymous$$aterial == null || rend.shared$$anonymous$$aterial.mainTexture == null || meshCollider == null)
         return;

     // Getting The Coordinates of the $$anonymous$$ain Texture.
     Texture2D tex = rend.material.mainTexture as Texture2D;
     Vector2 pixelUV = hit.textureCoord;
     pixelUV.x *= tex.width;
     pixelUV.y *= tex.height;

     // Getting The Colour Of new Texture.
     Color color = mappingTexture.GetPixel((int)pixelUV.x, (int)pixelUV.y);
     Debug.Log(color);

     // Setting The Colour Of the Texture
     tex.SetPixel((int)pixelUV.x, (int)pixelUV.y, color);
     tex.Apply();
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Awesome Bump shader in Unty? 0 Answers

Shader Texture Change 3 Answers

CustomRenderTexture (RFloat) wont initialize in the same frame it is created. 0 Answers

how to solve shader/texture problem: putting white icons on colored planes 1 Answer

Assign Texture To Material Unity 4.6 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges