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 QUICKSORT · May 01, 2013 at 06:03 PM · texturemaskrendertotextureself-illumination

how to create illumination mask texture loading script? (self-illuminated diffuse)

Hi all, I'm new to Unity and am countering a lot issues since I don't don't know a lot about unity. I've searched a lot and couldn't manage to load a illumination mask texture through a script on a procedurally generated mesh.

I managed to create a mesh through a script, I can also place a texture on it given the correct UV coordinates. There are 3 meshes that are supposed to be projected on a 270° cloth from 3 directions. These meshes will overlap each other, so I had to put a mask on it. I created an alpha mask (tif file so that unity recognizes it). But I cannot manage to put it on the mesh through my script.

I even had to write a custom self-illuminated diffuse shader, so that I can give separate uv coordinates for the mask. I managed to do that too and it also works correct. But somehow the mask is not recognized when loading it through the script. So How am I supposed to do it?

This is the result when I run the script, there is a question mark where the mask should be: alt text

this is how it should be. And you can also see on the mesh that the mask is loaded correctly like this. alt text

And this is the script where everything for this happens. As you can see I'm using 2 WWW objects in order to load the textures.

Though this is only a test, later on one will be removed because I'm going to replace the maintexture with a render to texture. Do you guys also know how I can set the main texture to a previously created renderToTexture from a different camera?

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System;
 
 
 public class Mesh0 : MonoBehaviour {
 
     /*
      * This is the script for dynamically loading the mesh that will 
      * contain the render to texture. 
      * (for now only one mesh will be laoded. 2 more will be added later if necessary)
      */
 
 
     //Flag indicating if the mesh is loaded or not (prevents loading multiple times)
     bool meshLoaded = false;
     //vertices of the mesh
     List<Vector3> vertexList = new List<Vector3>();
     //triangles of the mesh
     List<int> triangleList = new List<int>();
     // uv coordinates of the mesh
     List<Vector2> uvList = new List<Vector2>();
     // uv coordinates of the mask
     List<Vector2> uv2List = new List<Vector2>();
     //offset of triangle coordinates for each mesh (3 of them atm)
     //int triangleOffset;
 
     Mesh mpMesh;
     //Material mat;
 
     // Use this for initialization
     IEnumerator Start()
     {
         meshLoaded = true;
         //string filePath = "C:\\Users\\ISA\\Desktop\\testMesh.txt";
         //string texturePath = "C:\\Users\\ISA\\Desktop\\gridTexture.jpg";
         string filePath = "Assets\\Resources\\MultiprojectorMesh0.txt";
         //string filePath = "Assets\\Resources\\MultiprojectorMesh1.txt";
         //string filePath = "Assets\\Resources\\MultiprojectorMesh2.txt";
         string texturePath = "file:C:/Users/ISA/Documents/School_Documenten/2012-2013/Bachelorproef/Coding/First/Assets/ketnet.png";
         string maskPath = "file:C:/Users/ISA/Documents/School_Documenten/2012-2013/Bachelorproef/Coding/First/Assets/Resources/mask1.tif";
 
         WWW wwwMain = new WWW(texturePath);
         yield return wwwMain;
         WWW wwwMask = new WWW(maskPath);
         yield return wwwMask;
 
         //Loading Mesh
         if (!readData(filePath, 0))
         {
             Debug.Log("Error: Failed to create mesh.\n");
             meshLoaded = false;
         }
         else
             Debug.Log("\"" + filePath + "\" successfully loaded.\n");
 
         //Creating Mesh
         if (meshLoaded)
         {
             MeshFilter mf = GetComponent<MeshFilter>();
             mpMesh = new Mesh();
             mf.mesh = mpMesh;
             mpMesh.vertices = vertexList.ToArray();
             mpMesh.triangles = triangleList.ToArray();
             mpMesh.uv = uvList.ToArray();
             mpMesh.uv2 = uv2List.ToArray();
             mpMesh.RecalculateNormals();
             renderer.material.mainTexture = wwwMain.texture;
             renderer.material.SetTexture("_Illum",wwwMask.texture);
         }
         Debug.Log("Finished Creating Mesh 0\n");
 
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         //if (meshLoaded)
             //Graphics.DrawMesh(mpMesh, transform.localToWorldMatrix, mat, 0);
     }
 
     //Reads the mesh data from the file
     bool readData(string filePath, int meshIndex) 
     {
         string line;
         bool vertices = false;
         bool triangles = false;
         bool success = true;
 
         if (File.Exists(filePath))
         {
             StreamReader file = null;
             try
             {
                 file = new StreamReader(filePath);
                 while ((line = file.ReadLine()) != null)
                 {
                     if (line.Equals("VERT_XY_UV") && (line = file.ReadLine()) != null)
                     {
                         vertices = true;
                         triangles = false;
                     }
                     else if (line.Equals("TRI_XXX") && (line = file.ReadLine()) != null)
                     {
                         vertices = false;
                         triangles = true;
                     }
                     if (vertices)
                         parseVertex(line, meshIndex);
                     else if (triangles)
                         parseTriangle(line);
                 }
             }
             finally
             {
                 //Closing stream
                 if (file != null)
                     file.Close();
             }
         }
         else 
         {
             Debug.Log("Error: The file" + filePath + " does not exist");
             success = false;
         }
         return success;
     }
 
     //Parses the given line from the file stream
     //line: line of string to be parsed
     //meshFile: int from 0 to 2. Indicating th index of the projector
     void parseVertex(string line, int meshIndex)
     {
         // Prototype function, needs to be updated when I get legit values
         int x, y;
         float u, v;
         int projectorOffset = 1920+10; //width of the meshes seperately (needs to be confirmed)
         //TODO: Test values only for ketnet.png texture, TO be cahnged for render to Texture
         float uScale = 0.0003472222f; //1 / 3072
         float vScale = 0.0006924444f; // 1 / 768
         float u2Scale = 0.0005208333f; // 1 / 1920
         float v2scale = 0.0009259259f;// 1 / 1080
         // xxxxx yyyyy vertices
         // zzzzz not added because it's a  2d mesh, z is constant
         x = Convert.ToInt32(line.Substring(0, 5));
         y = Convert.ToInt32(line.Substring(6, 5));
         vertexList.Add(new Vector3(x + (projectorOffset)*meshIndex, y, 0));
         //Adding illumination mask UV coordinates (same as vertixes)
         uv2List.Add(new Vector3(x*u2Scale, y*v2scale));
         //xxxxx.xxxxx yyyyy.yyyyy uv-coordinates
         u = Convert.ToInt32(float.Parse(line.Substring(12, 11)));
         v = Convert.ToInt32(float.Parse(line.Substring(24, 11)));
         uvList.Add(new Vector2((float)(u*uScale), (float)(v*vScale)));
     }
 
     void parseTriangle(string line)
     {
         // xxxxx xxxxx xxxxx
         int triangleP1 = Convert.ToInt32(line.Substring(0, 5));
         int triangleP2 = Convert.ToInt32(line.Substring(6, 5));
         int triangleP3 = Convert.ToInt32(line.Substring(12, 5));
         Vector3 p1 = vertexList[triangleP1];
         Vector3 p2 = vertexList[triangleP2];
         Vector3 p3 = vertexList[triangleP3];
         
         Vector3 U, V, normal;
         U.x = p2.x - p1.x;
         U.y = p2.y - p1.y;
         U.z = p2.z - p1.z;
 
         V.x = p3.x - p1.x;
         V.y = p3.y - p1.y;
         V.z = p3.z - p1.z;
 
         normal.x = (U.y * V.z) - (U.z * V.y);
         normal.y = (U.z * V.x) - (U.x * V.z);
         normal.z = (U.x * V.y) - (U.y * V.x);
 
         //Debug.Log("Normal: x:" +normal.x+ " y:" +normal.y+ " z:" +normal.z+ "\n");
 
         triangleList.Add(triangleP1);
         if (normal.z < 0)
         {
             triangleList.Add(triangleP2);
             triangleList.Add(triangleP3);
         }
         else
         {
             triangleList.Add(triangleP3);
             triangleList.Add(triangleP2);
         }
     }
 }
 

wrongmask.jpg (371.8 kB)
maskcorrect.jpg (368.5 kB)
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
0
Best Answer

Answer by QUICKSORT · May 01, 2013 at 08:47 PM

Never mind.

I used:`renderer.material.SetTexture("_Illum", (Texture2D)Resources.Load("mask1"));`

Instead of: renderer.material.SetTexture("_Illum",wwwMask.texture);

And somehow it did work. Thanks anyone for taking your and reading my post

Comment
Add comment · 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

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

12 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

Related Questions

Shader light fix? 1 Answer

GUI Texture on Button Swap 3 Answers

How can I make a scratch card type of effect in unity 1 Answer

Reading a masked texture? 0 Answers

Creating a component from a button click 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