• 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 Lann · Oct 12, 2014 at 05:27 AM · shader

How do I use PSIZE in a Unity 4.5.4 shader?

I'm told that Unity overhauled the shader compile system in version 4.5. I am unable to get any examples of a shader that sets the size of vertices to work in Unity 4.5.

I am trying to build a point cloud editor that will allow me to scale the visible point size as the user scales a model. I have seen several posts claiming this approach works but they are all a couple years old..

Here is the shader I have been trying:

 Shader "Custom/VertexColor" {
     SubShader {
     Pass {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         LOD 200
               
                  
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
   
         struct VertexInput {
             float4 v : POSITION;
             float4 color: COLOR;
         };
          
         struct VertexOutput {
             float4 pos : SV_POSITION;
             float4 col : COLOR;
             float4 size : PSIZE;
         };
          
         VertexOutput vert(VertexInput v) {
          
             VertexOutput o;
             o.pos = mul(UNITY_MATRIX_MVP, v.v);
             o.col = v.color;
             o.size = 10.0;
             return o;
         }
          
         float4 frag(VertexOutput o) : COLOR {
             return o.col;
         }
  
         ENDCG
         } 
     }
  
 }
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 Thaggis · Dec 29, 2014 at 05:56 PM 0
Share

What you have above looks like it should work, or at least worked for me, with one exception. When I use Direct3D 11, I'm no longer able to adjust the point size. Try disabling it (Edit->Project Settings->Player) by unchecking the box next to 'Use Direct3D 11.'

I'm still looking for how to make this work in D3D 11:

http://answers.unity3d.com/questions/861891/how-do-you-change-point-size-in-direct3d-11-shader.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovelock · Oct 12, 2014 at 10:22 AM

The only way I've been able to get PSIZE to work is by forcing OpenGL. There may be a workaround for Direct3D but this might get you started.

First, run Unity with OpenGL like so:

 "C:\Program Files (x86)\Unity\Editor\Unity.exe" -force-opengl

Attach the following script to your main camera. This enables point size in OpenGL. (Source)

 #if UNITY_STANDALONE
 #define IMPORT_GLENABLE
 #endif
  
 using UnityEngine;
 using System;
 using System.Collections;
 using System.Runtime.InteropServices;
  
 public class EnablePointSize : MonoBehaviour 
 {
     const UInt32 GL_VERTEX_PROGRAM_POINT_SIZE = 0x8642;
     const UInt32 GL_POINT_SMOOTH = 0x0B10;
      
     const string LibGLPath =
         #if UNITY_STANDALONE_WIN
         "opengl32.dll";
     #elif UNITY_STANDALONE_OSX
     "/System/Library/Frameworks/OpenGL.framework/OpenGL";
     #elif UNITY_STANDALONE_LINUX
     "libGL";  // Untested on Linux, this may not be correct
     #else
     null;   // OpenGL ES platforms don't require this feature
     #endif
      
     #if IMPORT_GLENABLE
     [DllImport(LibGLPath)]
     public static extern void glEnable(UInt32 cap);
      
     private bool mIsOpenGL;
      
     void Start()
     {
         mIsOpenGL = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
     }
      
     void OnPreRender()
     {
         if (mIsOpenGL)
             glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
             glEnable(GL_POINT_SMOOTH);
     }
     #endif
 }

Attach the following script to a GameObject to create a point mesh. (Source)

 using UnityEngine;
 using System.Collections;
  
  
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class PointCloud : MonoBehaviour {
  
     private Mesh mesh;
     int numPoints = 60000;
  
     // Use this for initialization
     void Start () {
         mesh = new Mesh();
  
         GetComponent<MeshFilter>().mesh = mesh;
         CreateMesh();
     }
  
     void CreateMesh() {
         Vector3[] points = new Vector3[numPoints];
         int[] indecies = new int[numPoints];
         Color[] colors = new Color[numPoints];
         for(int i=0;i<points.Length;++i) {
             points[i] = new Vector3(Random.Range(-10,10), Random.Range (-10,10), Random.Range (-10,10));
             indecies[i] = i;
             colors[i] = new Color(Random.Range(0.0f,1.0f),Random.Range (0.0f,1.0f),Random.Range(0.0f,1.0f),1.0f);
         }
  
         mesh.vertices = points;
         mesh.colors = colors;
         mesh.SetIndices(indecies, MeshTopology.Points,0);
  
     }
 }

Attach your shader using PSIZE to that game object as a Material.

Now you should have a point cloud.

Comment
Add comment · Show 4 · 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 davegirdy1 · Jul 15, 2015 at 11:04 PM 0
Share

I tested this also on Linux and got it working by using "GL" versus "libGL". Unity does not want you to use "lib" or ".so" when accessing your plugins.

avatar image codeguyross1 · Sep 07, 2016 at 08:45 PM 0
Share

What do you mean by attach the the shader using PSIZE as material?

avatar image Lovelock codeguyross1 · Sep 08, 2016 at 06:56 PM 0
Share

@codeguyross This answer is a few years old but the concept is still the same in Unity. All materials in unity use shaders to deter$$anonymous$$e how they render. Custom shaders like we have above can assigned to game objects as a material. As long as the material has the proper shader selected, the game object will render using that shader.

Short version: Create a new material -> Set the material to use the custom shader -> Assign material to game object.

avatar image doublemax codeguyross1 · Sep 08, 2016 at 07:13 PM 0
Share

I think the PSIZE refers to the shader, it's probably meant like this:

Attach (your shader using PSIZE) to that game object as a $$anonymous$$aterial

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Shader error invalid subscript 'grabPos' at line 35 (on d3d11)? 0 Answers

How to not require normals in a surface shader 1 Answer

How to apply refraction at marked texture pixels while the rest is being rendered normal? 1 Answer

Unity - Simple Water 3 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