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 xorap · Jun 10, 2018 at 10:26 PM · shadersshader programmingshader writing

How to calculate UV coordinates of the outer edge of a texture mask?

alt text I'm trying to write a shader, that will increase the normal weight of the UV along an array of rays, V = mU + B. From (0,0) to (0,128) or rather between the max upper and lower corners of my texture file (128 x 128). I also want U to increase at the rate: _momentum, and the active (U,V) of each ray to have a normal weight of .2, for the active moment and all other points to have a weight of 0. I'm not familiar with manipulating normals in shaders, but perhaps in c# by using an alpha gradient prefab alt text Could my shader instantiate this alpha mask prefab at the active endpoint of each ray? At this point the texture will appear to have a wave rolling across. But I want to also include a mask, so that if any of my rays meet the green mask, they will halt and even perform other behaviors, originating from that location. alt text Point A represents the greatest value of b from my equation, and point B the lowest value of b. By pulling these coordinates I could know the range of (blue) rays that would collide, and then for each point along those rays that collide with the mask, I would be able to send a backwards casting ray with a _degrademomentum and a negative normal weight/inverted alpha prefab. Possibly even increasing the weight value of the normals as those specific rays approach their endpoint as well.


What I am most importantly seeking is if I can send raycasts on a 2D plane/texture and store the UVpoints where they meet the mask, and if they can alter the texture's normals along each line in relation to time.

The reason I don't just tile the prefab and translate it across, is because I want to use the information on pulling the edge coordinates to develop other equations for the remainder of the rays to perform. For instance upon reaching U = 99, the ray would carry momentum in another direction. Such as backwards, while the other rays remain unchanged.

Comment
Add comment · Show 3
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 xorap · Jun 10, 2018 at 11:23 PM 0
Share

Looking at this shader, I might be able to achieve what I want, only I would somehow need to replace the box with my mask, and still somehow find the coordinates of the border between empty space and filled space. https://github.com/gillesferrand/Unity-RayTracing/blob/master/Assets/RayCaster.shader

avatar image xorap · Jun 13, 2018 at 08:52 PM 0
Share

using UnityEngine; using System.Collections;

[RequireComponent (typeof($$anonymous$$eshFilter), typeof($$anonymous$$eshRenderer))] public class ExampleVerts : $$anonymous$$onoBehaviour { public int xSize, ySize; private $$anonymous$$esh mesh;

 private void Awake ()
 {
     Generate ();
 }

 private Vector3[] vertices;

 private void Generate ()
 {
     //Building the $$anonymous$$esh
     GetComponent<$$anonymous$$eshFilter> ().mesh = mesh = new $$anonymous$$esh ();
     mesh.name = "Procedural Grid";

     vertices = new Vector3[(xSize + 1) * (ySize + 1)];
     Vector2[] uv = new Vector2[vertices.Length]; //this gives vertices UV information
     Vector4[] tangents = new Vector4[vertices.Length]; //Tangents for bump mapping
     Vector4 tangent = new Vector4 (1f, 0f, 0f, -1f); //^
     //double loop to position vertices
     for (int i = 0, y = 0; y <= ySize; y++) {
         for (int x = 0; x <= xSize; x++, i++) {
             vertices [i] = new Vector3 (x, y);
             uv [i] = new Vector2 ((float)x / xSize, (float)y / ySize);
             tangents [i] = tangent;
         }
     }
     mesh.vertices = vertices;
     mesh.uv = uv;

     //giving the meshfilter triangles
     int[] triangles = new int[xSize * ySize * 6];
     for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++) {
         for (int x = 0; x < xSize; x++, ti += 6, vi++) {
             triangles [ti] = vi;
             //triangles[1] = 1; //xSize + 1 to flip normal  // these were transfered, since the triangles share vertices
             //triangles[2] = xSize + 1; //1 ^
             triangles [ti + 3] = triangles [ti + 2] = vi + 1;
             triangles [ti + 4] = triangles [ti + 1] = vi + xSize + 1;
             triangles [ti + 5] = vi + xSize + 2;
         }
     }
     mesh.triangles = triangles;
     mesh.RecalculateNormals ();
     StartCoroutine (OceanWake ());
 }
 public Layer$$anonymous$$ask layermask;

 //gizmos visualize vertices
 private void OnDrawGizmos ()
 {
     if (vertices == null) {
         return;
     }
     Gizmos.color = Color.white;
     for (int i = 0; i < vertices.Length; i++) { 
         Vector3 _i = new Vector3 (0, vertices [i].y);
         Gizmos.DrawSphere (_i, 0.1f);
         RaycastHit hit; 
         if(Physics.Raycast (vertices [i], Vector3.right, out hit, ySize + 1, layermask)){
                 Gizmos.DrawSphere (hit.point, 0.1f);
             //Debug.Log (hit.point);
             }
     }
 }


 private int _b;
 public float _rate;
 [Range (0, 20)]
 public int _rate2;
 private bool shoreline_stored = false;
 private Vector2[] pixels;
 private Vector2 shoreline;

 IEnumerator OceanWake ()
 {
     while (true) {//replace 'true' with future raycast boolean
         Texture2D texture = new Texture2D (128, 128);

         GetComponent<Renderer> ().material.SetTexture ("_DispTex", texture);

         int _b = $$anonymous$$athf.RoundToInt (_rate * Time.time);

         pixels = new Vector2[(texture.width) * (texture.height)];

         for (int i = 0, y = 0; y < texture.height; y++) { //y<texture.height is the max value for int y in this case 128
             for (int x = 0; x < texture.width; x++, i++) {
                 pixels [i] = new Vector2 (x, y);
                 Color color = ((x & y) != 0 ? Color.white : Color.black);
                 //texture.SetPixel (_b, y, color); //for single line
                 if (!shoreline_stored) {
                     RaycastHit2D hit = Physics2D.Raycast (new Vector2 (0, pixels [i].y), Vector2.right, texture.width, layermask, -0.5f, 0.5f);
                     if (hit.collider != null) {
                         hit.point = shoreline;
                         Debug.Log (shoreline);
                         shoreline_stored = true;
                     }
                 }
                 for (int _mb = _b - _rate2; _mb < _b + _rate2; _mb++) {
                     if (y == shoreline.y) {
                         for (int shorelim = _b + _mb; shorelim < shoreline.x; shorelim++) {
                             texture.SetPixel (shorelim, $$anonymous$$athf.RoundToInt(shoreline.y), color);
                         }
                     } else {
                         texture.SetPixel ((_b + _mb), y, color); //for a scalable range

                         if (x != _b + _mb) {
                             int _p = x;
                             texture.SetPixel (_p, y, Color.black);
                         }
                     }
                 }
             }
         }
         texture.Apply ();
         yield return null;
     }
 }

}

avatar image xorap · Jun 13, 2018 at 09:01 PM 0
Share

This is what I have so far.

Ins$$anonymous$$d of tracking (u,v)'s, I use a black distortion map and update each pixel with white, for my equations. It works with step one, sending the horizontal wave across the plane, but it's pooping at the collider phase.

Using both a 3D collider (to visualize the gizmos) and a 2D circle collider for the actual OceanWake() alt text Despite using the same technique to store the hitpoints of my 3D raycasts, my 2D raycasts do not store. But my console still logs (0,0) (the bottom left of the plane) as the only shoreline hitpoint. 1) there are no colliders in that spot 2) If I remove the shoreline_stored boolean, it does the same except it will overload my system with (0,0) logs.

2018-06-13-16-54-28.png (327.9 kB)

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

90 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

Related Questions

Adding a clip() to the default shader? 0 Answers

How i get unity linear fog start and end values in shaderlab. 1 Answer

Addition to Standard Shader stopping it from batching? 0 Answers

Make shader outer glow wider 0 Answers

Shadow distance rendering in a custom shader 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