Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 berrtus · May 26, 2015 at 06:29 AM · texture2dsetpixels

How to change texture2d

I am trying to ray trace. I saw a few tutorials, but I do not need physics or anything really except the ability to change the color of a pixel in a given rectangular area. Quite simple actually, but the solution of simply changing the color of a pixel seems to elude me. My code makes a rectangle with green and magenta colors and is attached to the camera. I have tried many ways to force the pixels to change color and nothing works. Never mind what is the purpose of this code it evolved mostly around various attempts to change pixel colors. It is attached to the camera.

 using UnityEngine;
 using System.Collections;
 
 public class RayTracer2 : MonoBehaviour
 {
     public Texture2D tex;
     public int width;
     public int height;
     public float rayDistance;
     public float t;
 
     
     void Start ()  
     {
         t = 1.0f;
         width = 100;
         height = 100;
         //if (outTex) {
         //    Destroy(outTex);
         //}
         tex = new Texture2D(width, height);
         Raytrace();
     }
 
     void update ()
     {
         int i;
         t = t + 0.1f;
         //Raytrace();
         Color[] colors = new Color[width*height];
         for(i = 0; i < width*height; i++)
         {
             colors[i] = Color.white;
         }
         tex.GetPixels(20,20,100,100);
         tex.SetPixels(20,20,100,100, colors);
         tex.Apply();
     }
 
     void OnGUI ()
     {
         //GL.PushMatrix();
         //GL.LoadPixelMatrix();
         GUI.DrawTexture(new Rect(20, 20, 200, 200),  tex);
         GUILayout.Label("fps: " + Mathf.Round(1/Time.smoothDeltaTime));
         //GL.PopMatrix();
     }
     
     void Raytrace () 
     {
         int x, y;
         Ray ray;
         Vector3 RayPoint;
         if (tex)
         {
             Destroy(tex);
         }
         tex = new Texture2D(width,height);
         for (y = 0; y < 100; y += 1) 
         {
             for (x = 0; x < 100; x += 1) 
             {
                 tex.SetPixel(x, y, Color.green);
                 ray = camera.ScreenPointToRay(new Vector3(x, y, 0.0f));
                 RayPoint = ray.GetPoint(6.0f*t);
                 if ((-5.0f < RayPoint.x) && (RayPoint.x < 5.0f))
                 {
                     if ((-7.0f < RayPoint.y) && (RayPoint.y < 7.0f))
                     {
                         if ((-6.0f < RayPoint.z) && (RayPoint.z < 6.0f))
                         {
                             tex.SetPixel(x, y, Color.magenta);
                         }
                     }
                 }
             }
             tex.Apply();
         }
     }
 }
Comment
Add comment · Show 1
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 cjdev · May 26, 2015 at 07:48 PM 0
Share

What exactly is your question?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

SetPixels32 called with invalid number if pixels in the array 1 Answer

How to make a custom GUITexture at runtime? 2 Answers

Painting stencil on a surface. 6 Answers

Convert a byte array to color data 0 Answers

Can't copy skybox faces to a cubemap sized texture without huge quality loss 0 Answers


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